Wednesday, 11 December 2019

NGINX: Named Locations


NGINX support named location. You can define named location using @ symbol.

Example
location @not_found{
    return 404 "File can't be found";
}

Named location is not used for a regular request processing, but instead used for request redirection.

Example
try_files hello123.txt data.png @not_found;

nginx.conf
events {
    
}


http {
    include mime.types;

    server {
        listen 9090;

        server_name localhost;

        root /welcome;

        try_files $uri /greet123.txt /hello.txt /not_found;

        location = /about{
            return 200 "Hello Welcome to NGINX";
        }

        location = /not_found{
            return 404 "File can't be found";
        }
    }
}

Validate and reload the nginx configuration.
$sudo nginx -t
Password:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$
$sudo nginx -s reload

Open the url ‘http://localhost:9090/1234’, you can see below kind of screen.


Previous                                                    Next                                                    Home

No comments:

Post a Comment