Skip to content

Resolved Path

Sanne Albreghs edited this page Sep 19, 2022 · 1 revision

Getting The Resolved Path

After selecting a server block and a location block, we now have to define with paths are served to find the requested files and how to set up index files.

server {
    root /www/data;

    location / {
        alias /var/www;
    }

    location /images/ {
        root /var/picuters;
        autoindex on;
        index index.htm index.html index.php;
    }
}

Return Directive

When a redirect (return directive) is found in the server block, the path defined by this directory will be returned. The return directive can be within the serverblock or location block. This is usefull for when a whole website or page changed location.

The HTTP response status code 301 Moved Permanently is used for permanent redirecting, meaning that links or records returning this response should be updated.

return 301 /path/its/redirected/to

Root Directive

The root directive specifies the root directory that will be used to search for a file. Nginx appends the request URI to the path specified by the root directive. This root can be declared in any level. The root that is declared in the server block will apply to all location blocks, unless a location block declares its own root directive.

Alias Directive

The alias directive specifies a replacement for the specified location. An example of this:

Requested file: /alias/test

location /alias {
    alias /this/is/the/alias;
}

Will turn into the path: /this/is/the/alias/test/

Alias can only be defined within a location block, alias and root can not both be defined in a location block. If root is defined in the server block, an alias in the location block will override this.

Autoindex and Index Directive

If a request ends with a slash, and therefore is a directory we will look is the autoindex directive is on.

The autoindex directive enables or disables the directory listing output: autoindex on|off.

We will look for a index file in the directory. The index directive defines the index file's name (default is index.html). If the request URI is /images/some/path/ , we deliver the file /www/data/images/same/path/index.html if it exists. If not we return HTTP code 404.

You can list more than one filename in the index directive. Nginx searches for files in the specified order and will return the first one it finds.

location / {
    index index.html index.htm index;
}