1

I would like to run multiple rails apps with just using different location blocks. Different unicorn workers are started and configured, they are working just fine.

Every app is under the same folder: /var/www/<app>, so I configured nginx like this:

root /var/www;

location  /app1/ {
  root /var/www/app1/public;
  try_files $uri/index.html $uri.html $uri @app1;
}

location  /app2/ {
  root /var/www/app2/public;
  try_files $uri/index.html $uri.html $uri @app2;
}

My issue is that w/ this ruleset a request (like mydomain/app1/check) comes into my app1 like this: Started GET "/app1/check" for ... I would like to have just Started GET "/check" for ...

What should i change on my config?

1

If you don't want to change your upstream parameters (whatever you are doing in your @app locations), a simple rewrite can help you:

location /app1/ {
    root /var/www/app1/public;
    rewrite ^/app1/(.*)$ /$1 break;
    try_files /app1/$uri/index.html /app1/$uri.html /app1/$uri @app1;
}

The break parameter to rewrite will cause nginx to rewrite the URI without actually redirecting or rerouting the request.

Don't forget to add the prefix /app1/ to your try_files names, because $uri will already be rewritten at the time try_files runs.

1

How did you setup your assets path in this case?

My assets path is

<host>/assets/<asset_name>.css 

but the correct path is:

<host>/<app_name>/assets/<asset_name>.css

My config is mostly the same as the one above.

upstream app1 {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.app1.sock fail_timeout=0;
}
upstream app2 {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.app2.sock fail_timeout=0;
}
location /app1/ {
    root /home/<user_app1>/<app1>;
    rewrite ^/app1/(.*)$ /$1 break;
    try_files $uri @app1;
}
location /app2/ {
    root /home/<user_app2>/<app2>;
    rewrite ^/app2/(.*)$ /$1 break;
    try_files $uri @app2;
}
location @app1 {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app1;
}
location @app2 {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app2;
}
0

I do this with nginx, uwsgi, and django/wsgi - the key is:

location ~* /tiny/(.+?)/ {
    include uwsgi_params;
    uwsgi_pass unix:/run/uwsgi/tiny_$1.sock;
    uwsgi_param SCRIPT_NAME /tiny/$1;
    uwsgi_modifier1 30;
}

I host the apps as "names"; i.e., /tiny/foobar/, but the app sees the path as /foobar/ because of the SCRIPT_NAME change above. modifier1 is peculiar to WSGI (IIRC), so may not apply to you setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.