By treecoder


2012-05-17 08:03:01 8 Comments

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.

7 comments

@Yao Zhao 2019-05-25 09:33:02

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.

@Twissell 2019-05-11 16:29:59

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.

@treecoder 2012-05-18 05:18:29

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;
}

the final path will correctly be formed as

/var/www/app/static

See the documentation here: http://wiki.nginx.org/HttpCoreModule#alias

@Cyril N. 2015-02-05 08:51:11

Your detailled has been of a great great help! Thank you very much !

@mafrosis 2015-12-16 07:30:29

The trailing slash on the alias path is essential!

@notalentgeek 2017-09-27 23:18:00

OMG, thank you so much that last slash costed me half a day.

@sean 2018-04-07 15:23:38

Thank you so much! This silly error had wasted me two hours.

@Pistos 2018-06-05 17:18:02

This is all great (it helped me fix my config issues), but I wonder what logging settings people could use to help diagnose these kinds of problems? Like, anything that would print to logs stuff like "received request for [...], matched by "location [...]" config block, searching directory [...]"

@helvete 2018-06-06 13:24:57

@Pistos: put log_format scripts '$document_root | $uri | > $request'; into http section and access_log /var/log/nginx/scripts.log scripts; into server section of nginx config..

@FotisK 2018-07-18 21:36:42

Thanks! Indeed the trailing slash is essential on the alias, otherwise i got nginx: [emerg] invalid number of arguments in "alias" directive, and the server went down during its restart.

@indospace.io 2019-05-02 14:46:57

that last slash does not follow protocol in regards to directory referencing, damnit NGINX, why?? why?????? tell me!! why?????????

@antonbormotov 2015-07-01 06:03:02

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.

@HostedMetrics.com 2017-12-26 17:35:47

Why is it better? Docs don't say either.

@antonbormotov 2017-12-26 17:42:16

The benefit that I see is to avoid duplication of the $uri, /images in given example, when using alias

@Tapish 2017-09-16 08:15:29

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.

@liuzhijun 2016-10-09 03:03:54

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

@aioobe 2017-10-21 09:32:06

Should the first arrow in the second image be a "+"?

@meloncholy 2014-10-05 16:38:31

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;
 }

@Mina 2017-05-28 15:33:17

Thanks for this answer. I know this is 3 years later, but could anyone explain if there is a performance and/or security tradeoff between using alias versus root?

@Matthew Wilcoxson 2018-08-16 17:35:59

@Mina It's better to use root if you can. (There's a comment in the docs wiki.nginx.org/HttpCoreModule#alias )

@alienfromouterspace 2019-07-07 06:17:52

This is exactly what I came here for 👍👏

Related Questions

Sponsored Content

2 Answered Questions

Keycloak Redirect url with nginx is going to http rather than https

  • 2018-04-03 12:02:15
  • Atulya Nair
  • 1184 View
  • 3 Score
  • 2 Answer
  • Tags:   nginx jboss

0 Answered Questions

nginx docker compose redirect delay

2 Answered Questions

2 Answered Questions

[SOLVED] Express - req.ip returns 127.0.0.1

1 Answered Questions

[SOLVED] nginx serving static files from root and uploaded files from alias

  • 2017-01-08 21:23:40
  • sorin
  • 601 View
  • 1 Score
  • 1 Answer
  • Tags:   nginx

1 Answered Questions

[SOLVED] Ngnix Jsession changed redirection issue

  • 2015-06-30 11:09:34
  • Mukesh Kumar
  • 437 View
  • 2 Score
  • 1 Answer
  • Tags:   nginx

1 Answered Questions

Can multi laravel based sites be accessed by folder not by port?

2 Answered Questions

[SOLVED] How to serve pages from another domain using Nginx

  • 2013-11-05 08:58:32
  • ippomakunochi
  • 369 View
  • 0 Score
  • 2 Answer
  • Tags:   nginx rewrite

1 Answered Questions

Configure Nginx to serve "/" from a directory already inside root

  • 2013-08-14 17:22:26
  • cbron
  • 101 View
  • 0 Score
  • 1 Answer
  • Tags:   nginx

3 Answered Questions

[SOLVED] Nginx location, alias, rewrite, root

Sponsored Content