nickhall
By:
nickhall

How do I change nginx routing on the rails/nginx preconfigured droplet on Ubuntu?

November 4, 2014 1.9k views

I just installed the nginx/rails configuration with Ubuntu 14.10 and I'm trying to have nginx point to various folders that are separate from rails, but I can't find any sites-available information, and it seems that /opt/nginx/conf/nginx.conf is also empty.

How can I add a simple route from "domain/stuff" to /var/www/stuff with this premade install?

1 Answer

Edit your server block (it should be in /etc/nginx/sites-enabled) and add the following location block:

location /stuff {
    alias /var/www/stuff; # map /stuff to /var/www/stuff
    index index.html; # assuming /var/www/stuff contain static files only
    try_files $uri $uri/ =404; # requests under /stuff shouldn't be passed to rails
}

Then restart nginx:

sudo service nginx restart
  • This does work for some files, thank you. But I'm having an issue where if I try to access an image from that folder, it will still try to go to the rails target.

    This is what it looks like:

    server {
            listen   80;
            root /home/rails/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|p$
                            try_files $uri @app;
                    }
    
            location /stuff {
                    alias /var/www/stuff;
                    index index.html;
                    try_files $uri $uri/ =404;
                    autoindex on;
            }
    
    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;
            }
    }
    
  • Hmm, I wasn't able to replicate that. Can you post the exact contents of your server block? It looks like it was cut off. Run the following command to print it:

    cat /etc/nginx/sites-enabled/filename
    
  • Those are the exact contents. This is /etc/nginx/sites-enabled/default

    server {
            listen   80;
            root /home/rails/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 /stuff {
                    alias /var/www/stuff;
                    index index.html;
                    try_files $uri $uri/ =404;
                    autoindex on;
            }
    
    
             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;
            }
    
    }
    
    

    I'll also note that if I comment out that file handling section, I can access the file as expected. I'm just not sure why the try_files $uri part wouldn't catch it in the /var/www/stuff folder?

Have another answer? Share your knowledge.