Nginx location alias with Rails

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: - - [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" - - [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"

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;

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Glad to hear you were able to find a solution!