Using nginx to display rail applications

I would like to use nginx in order to display all my rail applications on port 80.

I currently have 3 rail applications running on ports 3000 3001 and 3002, and I would like to use nginx on port 80 to display them like this:

http://127.0.0.1/app1 => 127.0.0.1:3000 http://127.0.0.1/app2 => 127.0.0.1:3001 http://127.0.0.1/app3 => 127.0.0.1:3002 

Here is what I did:

 server { listen 80; location /app1/ { proxy_pass http://127.0.0.1:3000/; } location /app2/ { proxy_pass http://127.0.0.1:3001/; } location /app3/ { proxy_pass http://127.0.0.1:3002/; } } 

However, when I try to access http://127.0.0.1/app1 , I only get HTML content, without assets / js / css, when the browser tries to get them from http://127.0.0.1/assets instead of http: //127.0.0.1/app1/assets .

Is there any way to fix this?

+4
source share
2 answers

Add ActionController::Base.relative_url_root = "/app1" at the end of your config/environment.rb 1 ActionController::Base.relative_url_root = "/app1" application 1 (similar for the other two applications). This will force Rails to add the correct URL prefix.

If you do not want to ruin the Rails configuration, you could probably force Nginx to go through your entire resource folder until it finds the one you need, if I'm not mistaken, it could be archived as follows:

 location /assets/ { try_files /app1/$uri /app2/$uri /app3/$uri; } 

Note that you must have different file names for different applications. This is already the case if you use the resource pipeline everywhere, as it hashes file names.

UPD

You can also try Referer based routing:

 location /assets/ { if ($http_referer ~* /app1) { rewrite ^(.*)$ app1/$1 break; } if ($http_referer ~* /app2) { rewrite ^(.*)$ app2/$1 break; } if ($http_referer ~* /app3) { rewrite ^(.*)$ app3/$1 break; } } 
+1
source

This is a good default configuration for what you want to achieve:

 server { listen 80; location /app1/ { root /srv/rails/app1/public; try_files $uri $uri/index.html $uri.html @app1_forward; } location @app1_forward { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app1_backend; } location /app2/ { root /srv/rails/app2/public; try_files $uri $uri/index.html $uri.html @app2_forward; } location @app2_forward { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app2_backend; } location /app3/ { root /srv/rails/app3/public; try_files $uri $uri/index.html $uri.html @app3_forward; } location @app3_forward { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app3_backend; } } upstream app1_backend { server 127.0.0.1:3000 fail_timeout=0; } upstream app2_backend { server 127.0.0.1:3001 fail_timeout=0; } upstream app3_backend { server 127.0.0.1:3002 fail_timeout=0; } 

Also check out this article where I refer to this nginx config example , for Rails.

+1
source

All Articles