Nginx Locations

This note contains some of the configuration rules I use on my web server at home. The web server servers content of three different websites.

Note. If you are using OSX and brew, the location of Nginx configuration file is /usr/local/etc/nginx/nginx.conf.


Locations are a way to link a URL to a specific file in your filesystem.

Multiple locations can be set per server and Nginx will use the rule with the longest match.

Consider the following:

server {
      listen       80;

      location / {
          root   html/spmbook;
          index  index.html index.htm;

      location /course {
          alias  html/spm;
          index  index.html index.htm;

tells nginx to serve all content of from the directory html/spmbook, with the exception of any URL containing /course, which will look for content in the html/spm directory.

Thus, for instance, serves the file html/spmbook/2014/index.html, while serves the file html/spm/2014/index.html.

Using multiple locations is useful, for instance, if you publish different websites under the same domain.

In my case, for instance, the html sources of and live in two different Jekyll repositories. Using different locations I can publish one independently from the other, while presenting them as belonging to the same domain.

Get in touch