Nginx — static file serving confusion with root & alias

前端 未结 7 1099
[愿得一人]
[愿得一人] 2020-11-22 15:29

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

相关标签:
7条回答
  • 2020-11-22 16:02

    I have found answers to my confusions.

    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.

    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.

    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
    

    The case of trailing slash for alias directive

    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

    0 讨论(0)
  • 2020-11-22 16:06

    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;
     }
    
    0 讨论(0)
  • 2020-11-22 16:08

    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:

    for alias:

    0 讨论(0)
  • 2020-11-22 16:13

    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.

    0 讨论(0)
  • 2020-11-22 16:18

    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.

    0 讨论(0)
  • 2020-11-22 16:22
    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.

    0 讨论(0)
提交回复
热议问题