Hi I want to make a nginx rewrite as fllowing
http://my-domain/Canada
to
http://my-domain/rates/callrates.php?c=Canada
user will see the url as http://my-domain/Canada
and the php will execute the page in the following url
http://my-domain/rates/callrates.php?c=Canada
how to enable this in nginx.conf ?
server
{
listen 80;
server_name my-domain.com;
access_log /home/www/my-domain/logs/access.log;
error_log /home/www/my-domain/logs/error.log;
root /home/www/my-domain/public_html;
location / {
index index.php login.php;
try_files $uri $uri/ $uri.php?$query_string #extensionless-php;
error_page 404 /404.php;
}
location ~* ^/(?<country>\w+)$ {
rewrite ^ /testring/callrates.php?c=$country last;
}
location ~ \.html$
{
if (!-f $request_filename)
{
rewrite ^(.*)\.html$ $1.php last;
}
}
location ~ .*\.php$
{ include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/r2wconnect.com/public_html$fastcgi_script_name;
}
location #extensionless-php
{
rewrite ^(.*)$ $1.php last;
}
}
Assuming that you already have a working PHP configuration...
If your domain does nothing else, the default action could be to invoke the script with:
location / {
try_files $uri /rates/callrates.php?c=$uri;
}
The above has the side effect of leaving in the leading /.
If you want to make the rule more specific, you could protect it with a regular expression (which also extracts the country name correctly):
location ~* ^/(?<country>\w+)$ {
rewrite ^ /rates/callrates.php?q=$country last;
}
Related
I've got a problem with proxying of subdomain's name from nginx to rails server. In my rails app I have links like tenant1.localhost:3000, tenant2.localhost:3000, etc. and it works fine. On production I use Nginx + Puma and nginx doesn't proxy to puma any request if I open link with subdomain.
nginx.conf
upstream puma_muninn {
server app:3000;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 #503;
server_name localhost puma_muninn;
server_name ~^(?<subdomain>.+)localhost$;
root /var/www/muninn/public;
try_files $uri/index.html $uri #puma_muninn;
location #puma_muninn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://$subdomain.puma_muninn;
# limit_req zone=one;
access_log /var/www/muninn/log/nginx.access.log;
error_log /var/www/muninn/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ \.(php|html)$ {
return 405;
}
}
production.rb
config.action_dispatch.tld_length = 2
But as I said puma doesn't even get requests from nginx.
Any ideas?
A domain in nginx.conf has to be specific instead of localhost.
Running a ruby on rails application but have wordpress integrated under the /blog on the domain.
The problem I'm having is that none of the asset files are served correctly under the /blog url.
The wordpress php files are routed correctly and work. The issue is that I'm trying to route the wordpress theme and plugin files, namely css and js files to the /blog folder. However I'm getting 404 for the static files served under /blog so I think I have a misconfiguration in my nginx conf file.
Current nginx configuration:
server {
listen 3000;
server_name myapp.com;
access_log off;
location /blog {
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
expires max;
access_log off;
add_header Cache-Control public;
root /var/www/wordpress/current/blog;
break;
}
root /var/www/wordpress/current/blog;
index index.php index.html index.htm;
rewrite ^/blog/(.*)$ /blog/$1 break;
try_files $uri $uri/ /index.php?$args;
}
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
root /u/apps/myapp/current/public;
expires max;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires max;
access_log off;
add_header Cache-Control public;
root /u/apps/myapp/current/public;
break;
}
client_max_body_size 50M;
root /u/apps/myapp/current/public;
access_log off;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.3#myapp/wrappers/ruby;
passenger_enabled on;
passenger_max_request_queue_size 200;
rails_env production;
if ($host != 'myapp.com') {
rewrite ^/(.*)$ http://myapp.com/$1 permanent;
}
location ~* ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
error_page 500 504 /500.html;
location = /500.html {
root /u/apps/myapp/current/public;
}
error_page 502 503 /503.html;
location = /503.html {
root /u/apps/myapp/current/public;
}
error_page 404 /404.html;
location = /50x.html {
root html;
}
location ~ .*\.php$ {
root /var/www/wordpress/current;
#fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param HTTPS 'on';
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
location ~* "^.*?\.(eot)|(ttf)|(woff)$" {
add_header Access-Control-Allow-Origin *;
}
}
There is a difference between root and alias, I think you're looking for alias in this situation.
When you use root nginx appends the URI to the path, so using root /var/www/wordpress/current/blog; will cause this to be the root directory for the request, which means navigating to /blog/css/style.css will cause nginx to look for /var/www/wordpress/current/blog/blog/css/style.css.
If you use an alias instead, then nginx will map the uri to the directory:
alias /var/www/wordpress/current/blog;
When you navigate to /blog/css/style.css nginx will remove the prefix and serve the file from /var/www/wordpress/current/blog/css/style.css, it seems you're attempting to do this with a rewrite however your rewrite is rewriting the request to the same uri.
In the situation the URL doesn't work your error_log should be your friend, it'll tell you exactly where it's looking:
2017/06/15 13:04:19 [error] 43391#0: *1786 open()
"/var/www/wordpress/current/blog/blog/css/styles.css" failed
(2: No such file or directory), client: 127.0.0.1, server: myapp.com,
request: "GET /blog/css/styles.css HTTP/1.1", host: "myapp.com:3000"
Changing this to alias throws an error for me (because I don't have your directory structure) but it shows how the location changes:
2017/06/15 13:06:12 [error] 43582#0: *1787 open()
"/var/www/wordpress/current/blog/css/styles.css" failed
(2: No such file or directory), client: 127.0.0.1, server: myapp.com,
request: "GET /blog/css/styles.css HTTP/1.1", host: "myapp.com:3000"
You also don't have a lot of duplicate directives, you only need to define the them once as they are inherited by children, this can clean up your configuration file a lot making it easier to switch things around in the future:
server {
client_max_body_size 50M;
listen 3000;
server_name myapp.com;
access_log off;
root /u/apps/myapp/current/public; # default root, use this unless specified otherwise
error_page 500 504 /500.html;
error_page 502 503 /503.html;
error_page 404 /404.html;
location /blog {
alias /var/www/wordpress/current/blog; # overwrite the default root for this entire block
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
expires max;
add_header Cache-Control public;
break;
}
}
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
expires max;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires max;
add_header Cache-Control public;
break;
}
location ~* "^.*?\.(eot)|(ttf)|(woff)$" {
add_header Access-Control-Allow-Origin *;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if ($host != 'myapp.com') {
rewrite ^/(.*)$ http://myapp.com/$1 permanent;
}
location ~* ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
location = /50x.html {
root html; # overwrite the default root for this
}
location ~ .*\.php$ {
root /var/www/wordpress/current; # overwrite the default root, because this doesn't have /blog on the end it will properly map to /var/www/wordpress/current/blog when /blog is accessed
#fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param HTTPS 'on';
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
# this block is only processed if nothing else matches
location / {
passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.3#myapp/wrappers/ruby;
passenger_enabled on;
passenger_max_request_queue_size 200;
rails_env production;
}
}
Using Rails 4.3. I have the following line in production.rb:
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
config.action_controller.asset_host = "https://assets.example.com"
And the following in example.conf nginx:
upstream example {
server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 503 504 /500;
root /home/deployer/example/current/public;
try_files $uri/index.html $uri.html $uri #example;
location #example {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
I notice that my subdomain http://assets.example.com is the mirror of my main domain https://example.com, instead of just serving the assets. For example, https://assets.example.com/blog/1 is the same for https://www.example.com/blog/1.
How do I prevent that? I just want https://assets.example.com to serve static assets.
I think that you should split your nginx server section into two sections, one for the web, the other one for the static assets site.
The server section for the web should remain essentially the same as in your original post, it should perhaps just react on the full hostname:
server_name www.example.com;
The static site server section should be an amended copy of the main site section, with the following principal differences:
the server_name should contain assets.example.com
the section should contain NO proxy_pass directive, static assets are meant to be served statically, i.e. directly by nginx, without having to go through Rails
the root will be the same as in the main site
the asset files will be served directly from the root, relatively, so if there is /assets/ in your URLs (which there probably is), then the assets should be found and served directly from your public/assets physical directory.
Update: sample nginx configuration:
upstream example {
server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}
# main site config
server {
listen 80;
server_name www.example.com;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 503 504 /500;
root /home/deployer/example/current/public;
try_files $uri/index.html $uri.html $uri #example;
location #example {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
server {
listen 80;
server_name assets.example.com;
client_max_body_size 4G;
keepalive_timeout 10;
root /home/deployer/example/current/public;
location = /404.html {
root html;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
my rewrite URL is not working, here is part of nginx config with rewrite. Thanks for any advice.
location / {
try_files $uri $uri/ #mylocation;
}
location #mylocation {
rewrite ^index.php?p=/profile(.*)$ /profile/ redirect;
rewrite ^.*$ /index.php last;
}
Requests never reach #mylocation, as I assume index.php exists ...
Try something like this
location / {
try_files $uri $uri/ /index.php;
}
location /index.php {
if ($args ~* "^p=/profile.*") {
rewrite ^ /profile/? redirect;
}
}
simple question, How can I deploy my Rails Application into a port of my website? I know i can specify the port when running using Mongrel or Webrick, but this time, I have to deploy it into production. I think passenger can manage this but I dont know how. I tried search but still I can't find the solution. Please help :)
Thanks!
Follow-up:
I am using Ubuntu 10.04 LTS and my Passenger runs with Apache.
If you're using Passenger with Apache or nginx. It will use the default port, 80. You can change this in the config file based on which web server you use.
# HTTPS server
server {
listen 80;
listen 8080;
server_name *.host.com;
root /home/app/public_html/host_production/current/public;
error_page 500 502 504 /500.html;
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
error_page 503 #503;
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
try_files $uri /system/maintenance.html #passenger;
location #passenger {
passenger_enabled on;
passenger_min_instances 5;
rails_env production;
}
if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location ~ \.php$ {
deny all;
}
access_log /dev/null;
error_log /dev/null;
}
nginx+passenger config