Nginx Location Directive

Nginx Location Directive is used to route request to correct files.

Match

Exact match is used to match an exact URL.

server {
    listen 80 default_server;
    root /var/www/html;
    index index.html;
    server_name _;

    location /ok/ {
        root /home/;
    }
}

When location is used with no modifiers, then beginning of the URL is matched. In this case, any url http://domain/ok/FILE_NAME will be served from /home/ok/FILE_NAME

Exact Match (=)

Exact match is used to match an exact URL.

server {
    listen 80 default_server;
    root /var/www/html;
    index index.html;
    server_name _;

    location = /ok/index.html {
        root /home/;
    }
}

In this example http://domain/ok/index.html get served from /home/ok/index.html. Only this specific file will be matched.

Cause Insensitive Regular Expression Match (~*)

server {
    listen 80 default_server;
    root /var/www/html;
    index index.html;
    server_name _;

    location /ok/ {
        root /home/;
    }
}

Above code routes URL http://domain/ok/ to /home/ok/index.html. But won’t match http://domain/OK/.

If you need both /ok and /OK work, you need to use

    location ~* /ok/ {
        root /home/;
    }

With this config, http://domain/OK/FILE will be served from /home/OK/FILE.

See Nginx

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *