Question
How to setup sub uri based Rails apps with Nginx and Unicorn?
I am trying to use the instructions at http://jrochelly.com/post/2013/08/nginx-unicorn-multiple-rails-apps/ to setup the sub uri (www.mydomain.com/site1 www.mydomain.com/site2) based Rails applications using Nginx and Unicorn.
I started with Droplet that had Unicorn and Nginx pre-installed from DO and it works fine. Now I want to setup multiple sites/Rails apps based on Sub uri.
As per the instructions I have updated nginx.conf by adding follwoing to current nginx.conf
upstream unicorn_socket_for_site2 {
server unix:/home/navjeetc/site2/current/tmp/sockets/unicorn.sock fail_timeout=0;
}
Then I created a file site2 under sites-enabled as below:
server {
location /site2/ {
try_files $uri @unicorn_proxy;
}
location @unicorn_proxy {
proxy_pass http://unix:/home/navjeetc/site2/current/tmp/sockets/unicorn.sock;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
And then made changes to my site2 Rails app as suggested. I did not change unicorn conf file.
But the request to http:/subdomain.mydomain.com/site2 is still going to the old Rails app and I get a 404.
The nginx config file for the old Rails app is:
server {
listen 80;
root /home/rails/current/public;
server_name _;
index index.htm index.html;
location / {
try_files $uri/index.html $uri.html $uri @app;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
try_files $uri @app;
}
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
How can I fix this?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×