alias

The 'alias' directive defines an alternate path for NGINX to serve specific requests, effectively mapping the request URI to a local file system path.

Syntaxalias path;
Defaultnone
Contextlocation
Arguments1

Description

The 'alias' directive is used within a location context to specify an alternate local file system path that a URI should map to. For instance, when a request is made to a URI that matches the specified location block, NGINX uses the 'alias' value for further processing and file retrieval instead of the default behavior. This allows for flexible configuration of static file locations while maintaining meaningful URLs in client requests.

The 'alias' directive takes a single argument, which is the path to the directory or file that should be served in response to matching requests. It is important to note that when using 'alias', the matching part of the URI is replaced by the alias path. This is different from the 'root' directive, where the request URI is appended to the path specified by 'root'. Therefore, care must be taken in configuring 'alias' to ensure that it effectively maps the desired URI structure to the correct filesystem structure.

One common usage of 'alias' is when serving images or static files from a directory that does not follow the document root structure. For example, if you have images stored in '/var/www/images' but want to serve them under '/images' in the URI, you can use the 'alias' directive to achieve this mapping without changing the structure of the backend file system.

Config Example

location /images {
    alias /var/www/images/;
}

If the request URI does not end with a trailing slash, it may lead to unexpected behavior and result in a 404 error if not handled correctly.

Remember to add a trailing slash to the alias path if serving directories to ensure it functions correctly with NGINX's filesystem lookups.

Failure to properly match the location block can result in misconfigured paths and unexpected serve behaviors.

← Back to all directives