Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx -- static file serving confusion with root & alias

Tags:

nginx

I need to serve my app through my app server at 8080, and my static files from a directory without touching the app server. The nginx config I have is something like this...

    # app server on port 8080     # nginx listens on port 8123     server {             listen          8123;             access_log      off;              location /static/ {                     # root /var/www/app/static/;                     alias /var/www/app/static/;                     autoindex off;             }               location / {                     proxy_pass              http://127.0.0.1:8080;                     proxy_set_header        Host             $host;                     proxy_set_header        X-Real-IP        $remote_addr;                     proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;             }     } 

Now, with this config, everything is working fine. Note that the root directive is commented out.

If I activate root and deactivate the alias -- it stops working. However, when I remove the trailing /static/ from the root it starts working again.

Can someone explain what's going on. Also please explain clearly and verbosely what are the differences between root and alias, and their purposes.

like image 918
treecoder Avatar asked May 17 '12 08:05

treecoder


People also ask

Can nginx be used for serving static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

What is the difference between root and alias in a nginx configuration?

In case of root, the URL path after domain, including location, is appended to the root folder location. In case of Alias, only the URL part without the location, is appended to Alias folder.


2 Answers

There is a very important difference between the root and the alias directives. This difference exists in the way the path specified in the root or the alias is processed.

root

  • the location part is appended to root part
  • final path = root + location

alias

  • the location part is replaced by the alias part
  • final path = alias

To illustrate:

Let's say we have the config

location /static/ {     root /var/www/app/static/;     autoindex off; } 

In this case the final path that Nginx will derive will be

/var/www/app/static/static 

This is going to return 404 since there is no static/ within static/

This is because the location part is appended to the path specified in the root. Hence, with root, the correct way is

location /static/ {     root /var/www/app/;     autoindex off; } 

On the other hand, with alias, the location part gets dropped. So for the config

location /static/ {     alias /var/www/app/static/;     autoindex off;           ↑ }                            |                              pay attention to this trailing slash 

the final path will correctly be formed as

/var/www/app/static 

In a way this makes sense. The alias just let's you define a new path to represent an existing "real" path. The location part is that new path, and so it gets replaced with the real path. Think of it as a symlink.

Root, on the other hand is not a new path, it contains some information that has to be collated with some other info to make the final path. And so, the location part is used, not dropped.

The case for trailing slash in alias

There is no definitive guideline about whether a trailing slash is mandatory per Nginx documentation, but a common observation by people here and elsewhere seems to indicate that it is.

A few more places have discussed this, not conclusively though.

https://serverfault.com/questions/376162/how-can-i-create-a-location-in-nginx-that-works-with-and-without-a-trailing-slas

https://serverfault.com/questions/375602/why-is-my-nginx-alias-not-working

like image 122
treecoder Avatar answered Oct 06 '22 01:10

treecoder


as say as @treecoder

In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.

A picture is worth a thousand words

for root:

enter image description here

for alias:

enter image description here

like image 33
liuzhijun Avatar answered Oct 06 '22 01:10

liuzhijun