Get the App
SLTechnology News&Howtos  ›  Development  › 

How to configure the location function of Nginx

Shulou Source: shulou.com Published: 2022-06-01 04:50:45 09月26日 Update

This article introduces the relevant knowledge of "how to configure the location function of Nginx". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1.Nginx location 1.1.location action

The function of the location instruction is to execute different applications according to the URI requested by the user.

1.2.location syntax

Location [= | ~ | ~ * | ^ ~] uri {.}

The above syntax is divided into four parts:

Location: directive [= | ~ | ~ * | ^ ~]: matching identity uri: matching website address {.}: configuration segment to be executed after matching uri

Note:

The difference between ~ and ~ * is: ~ case-sensitive, ~ * case-insensitive ^ ~: regular expressions are not checked after regular string matching

1.3.location matching example

Location = / {[configuration A]} location / {[configuration B]} location / documents {[configuration C]} location ^ ~ / images/ {[configuration D]} location ~ *\. (gif | jpg | jpeg) ${[configuration E]} the above configuration, when request "/", match configuration A request "/ index.html", match configuration B request "/ documents/docunment.html", match configuration C request "images/1.gif", match configuration D request "/ documents/1.jpg" Match configuration E

1.4.location configuration instance

Server {listen 80; server_name bbs.yygg.com; root html/bbs; location / {return 401;} location = / {return 402;} location / documents/ {return 403;} location ^ ~ / images/ {return 405 } location *\. (gif | jpg | jpeg) ${return 406;}

Test result

[root@nginx-01] # curl-s-o / dev/null-I-w "% {http_code}\ n" bbs.yygg.com402 [root @ nginx-01 ~] # curl-s-o / dev/null-I-w "% {http_code}\ n" bbs.yygg.com/ 402 [root @ nginx-01 ~] # curl-s-o / dev/null-I-w "% {http_code}\ n" bbs.yygg.com/index.html401 [root@ Nginx-01 ~] # curl-s-o / dev/null-I-w "% {http_code}\ n" bbs.yygg.com/documents/ documents. Html403 [root @ nginx-01 ~] # curl-s-o / dev/null-I-w "% {http_code}\ n" bbs.yygg.com/images/1.gif405 [root@nginx-01 ~] # curl-s-o / dev/null-I-w "% {http_code}\ n" bbs.yygg.com / documents/ 1.jpg406 [root @ nginx-01 ~] # curl-s-o / dev/null-I-w "% {http_code}\ n" bbs.yygg.com/yyang/401

The returned status code also matches the rules in the configuration.

1.5. The sequence description of matching without uri and special character combinations

Location = / {}: exact matching location ^ ~ / images/ {}: regular string matching, no regular matching location ~ *\. (gif | jpg | jpeg) ${}: regular matching location / documents/ {}: regular string matching, if there is regular matching, priority matching regular location / {}: after all location mismatches, the default matching "how to configure the location function of Nginx" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Configuration regular character string general function function content size instruction more knowledge website grammar different practical special accurate learned next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker NVidia macOS Apple Xiaomi