pomff
By:
pomff

Nginx location alias with Rails

October 3, 2015 1.2k views
Nginx Ruby on Rails Deployment Ubuntu

Hey, I am trying to configure nginx to serve image files requested from http://root/imagename.jpg from the public directory inside my rails app. However, trying to access a image file that exists from this url leads only to a rails error message saying the page I'm looking for doesn't exist. It's not an nginx error, and nginx is not logging an error, so I'm assuming rails is handling the error. I've tried changing the position of the location rule in the sites-available config to no avail.

My server is running Ubuntu 14.04, and nginx, rails, unicorn and postgres are all set up, and my rails application is accessible.

My access.log:

85.76.129.5 - - [03/Oct/2015:08:39:09 -0400] "GET /2b275df4.png/ HTTP/1.1" 404 715 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0"
85.76.129.5 - - [03/Oct/2015:08:42:17 -0400] "GET /2b275df4.png/ HTTP/1.1" 404 715 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0"
code

My /etc/nginx/sites-available/default:

upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/var/www/nee/shared/sockets/unicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name localhost;

    root /var/www/nee/public;
    # The rule I'm trying to get to work
    location ~* \.(jpg|jpeg|png)$ {
        alias /var/www/nee/public/uploads/images;
    }

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

1 comment
  • SOLVED: Created a symlink for the public/uploads/images folder in /var/www/images and set the loation rule to the following:

    location ~* \.(jpg|jpeg|png)$ {
            root /var/www/images;
        }
    
1 Answer

Glad to hear you were able to find a solution!

Have another answer? Share your knowledge.