a p
a p

Reputation: 1271

Nginx says 404 Not Found when trying to serve an image from persistent storage

Following is my nginx config -

server {
    server_name example.com www.example.com;

    root /volume-bde-01/images;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://<server-ip>:3000;
    }

    location /images {

    }
}

When I call http://example.com/api, it works fine and returns me the response from my server running at port 3000. I need to upload images as a part of an api and need to directly display them later using the image src url (I was thinking). But some how the configuration doesn't work and gives mein 404 - Not Found.

/volume-bde-01/images; is a persistent storage block attached to the server.

I tried http://example.com/images/test.jpg

NOTE - I believe its not mandatory to put the images in the same directory as running server's root as most of the examples are around that? We can place them anywhere, Isn't it?

P.S. -

It works for me with -

location /images/ {
          alias /volume-blr1-01/images/;
      }

But I wonder what difference is between root and alias ?

Upvotes: 0

Views: 2586

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146620

The alias directive tells Nginx to replace what is defined in the location block with the path specified by the alias directive.

location ^~ /images {
   alias /var/www/static;
   try_files $uri $uri/ =404;
}
  • http://example.com/images/logo.png into the file path /var/www/static/logo.png
  • http://example.com/images/third-party/facebook-logo.png into the file path /var/www/static/third-party/facebook-logo.png

The root directive tells Nginx to take the request url and append it behind the specified directory.

location ^~ /images {
   root /var/www/static;
   try_files $uri $uri/ =404;
}
  • http://example.com/images/logo.png into the file path /var/www/static/images/logo.png.
  • http://example.com/contact.html into the file path /var/www/example.com/contact.html

Credits: https://www.techcoil.com/blog/understanding-the-difference-between-the-root-and-alias-directives-in-nginx/

Upvotes: 1

Related Questions