'Nginx -- static file serving confusion with root & alias
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.
Solution 1:[1]
as say as @treecoder
In case of the
rootdirective, full path is appended to the root including the location part, whereas in case of thealiasdirective, 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:
for alias:
Solution 2:[2]
In your case, you can use root directive, because $uri part of the location directive is the same with last root directive part.
Nginx documentation advices it as well:
When location matches the last part of the directive’s value:location /images/ { alias /data/w3/images/; }it is better to use the root directive instead:
location /images/ { root /data/w3; }
and root directive will append $uri to the path.
Solution 3:[3]
Just a quick addendum to @good_computer's very helpful answer, I wanted to replace to root of the URL with a folder, but only if it matched a subfolder containing static files (which I wanted to retain as part of the path).
For example if file requested is in /app/js or /app/css, look in /app/location/public/[that folder].
I got this to work using a regex.
location ~ ^/app/((images/|stylesheets/|javascripts/).*)$ {
alias /home/user/sites/app/public/$1;
access_log off;
expires max;
}
Solution 4:[4]
alias is used to replace the location part path (LPP) in the request path, while the root is used to be prepended to the request path.
They are two ways to map the request path to the final file path.
alias could only be used in location block, and it will override the outside root.
alias and root cannot be used in location block together.
Solution 5:[5]
server {
server_name xyz.com;
root /home/ubuntu/project_folder/;
client_max_body_size 10M;
access_log /var/log/nginx/project.access.log;
error_log /var/log/nginx/project.error.log;
location /static {
index index.html;
}
location /media {
alias /home/ubuntu/project/media/;
}
}
Server block to live the static page on nginx.
Solution 6:[6]
In other words on keeping this brief: in case of root, location argument specified is part of filesystem's path and URI . On the other hand — for alias directive argument of location statement is part of URI only
So, alias is a different name that maps certain URI to certain path in the filesystem, whereas root appends location argument to the root path given as argument to root directive.
Solution 7:[7]
Though my answer is not needed, But I think It is necessary to add this, root and alias works differently when is come to regex.
location ~ /static/my.png$ {
alias /var/www/static/;
access_log off;
expires max;
}
In this case the regex match is not going to add with alias, nginx will search only /var/www/static/ not /var/www/static/my.png. You have to use regex capture.
location ~ /static/my.png$ {
root /var/www;
access_log off;
expires max;
}
In this case the matched url going to add with root, nginx will search /var/www/static/my.png.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mahmoud Al-Qudsi |
| Solution 2 | aioobe |
| Solution 3 | meloncholy |
| Solution 4 | |
| Solution 5 | Tapish |
| Solution 6 | |
| Solution 7 | Jean-Philippe Caruana |


