Nginx location syntax description

  

nginx location introduction

The Location directive in Nginx is an important directive in NginxHttpCoreModule. The Location directive is used to configure the matching URI. The URI is the syntax of ”/uri/”, which can be a string or a regular expression. But if you want to use a regular expression, you must specify a prefix.

nginx location syntax

Basic syntax: location [=| ~| ~*| ^~] /uri/{ … }

= Strict match. If this query matches, the search will stop and the request will be processed immediately. ~ for case-sensitive matching (available regular expressions) ~* for case-insensitive matching (can be used with regular expressions) !~ and !~* are case-insensitive and case-insensitive respectively ^~ This prefix is ​​used for a regular string, then tells nginx not to test the regular expression if the path matches.

Location syntax: location [=| ~| ~*| ^~] /uri/{ … } Note: 1, ~ for case-sensitive matching 2, ~ * for case-insensitive matching 3, !~ and !~* are case-insensitive and case-insensitive Mismatch

Example 1:

location /{ }

Matches any query because all requests start with /. But the regular expression rules will be matched first with the query.

Example 2:
Code is as follows location =/{}

Just match /

Example 3:
Code is as follows location ~* .(gif| Jpg| Jpeg)$ { rewrite .(gif| Jpg) $ /logo.png; }

Note: Case-insensitive matching of any file ending with gif, jpg, jpeg

nginx location application example
Code is as follows

location = /{ # Only match /query. }location /{ # Matches any query because all requests have been /started. But regular expression rules and long block rules will be matched first with the query. }location ^~ /images/{ # Matches any queries that start with /images/and stops searching. Any regular expression will not be tested. }location ~* .(gif| Jpg| Jpeg)$ { # Matches any request that ends with gif, jpg, or jpeg. }location ~* .(gif| Jpg| Swf)$ { valid_referers none blocked start.igrow.cn sta.igrow.cn; if ($invalid_referer) { #防盗链 rewrite ^/http://$host/logo.png; } } location ~* .(js| Css| Jpg| Jpeg| Gif| Png| Swf)$ { if (-f $request_filename) { #Set expiration time according to file type expires 1h; break; } } location ~* .(txt| Doc)${ #Disallow access to a directory root /data/www/wwwroot/linuxtone/test; deny all; }

++ Some available global variables $args $content_length $content_type $document_root $document_uri $ Host $http_user_agent $http_cookie $limit_rate $request_body_file $request_method $remote_addr $remote_port $remote_user $request_filename $request_uri $query_string $scheme $server_protocol $server_addr $server_name $server_port $uri

Copyright © Windows knowledge All Rights Reserved