In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1. Grammar
Location [= | ~ | ~ * | ^ ~ | @] / uri/ {...}
2. Description
From the above syntax, we can see that location can be divided into three parts, and then we will study it one by one.
1) [= | ~ | ~ * | ^ ~ | @]
=: indicates exact match followed by url~: regular match, but case-sensitive ~ *: regular match, case-insensitive ^ ~: ordinary character match. If this option matches, only this option matches, not other options. It is generally used to match the directory @: "@" to define a named location, such as error_page for internal orientation.
Several different symbols are defined above to represent different matching rules, so what about the order?
The instruction of the = prefix strictly matches this query. If found, stop the search; all remaining regular strings, the longest match. If the match uses the ^ ~ prefix, the search stops; the regular expression, the order defined in the configuration file; and if the third rule produces a match, the result is used. Otherwise, the result of rule 2 is used.
Test example 1:
Location = / world {return 600;} location = / hello {return 600;} location ~ / hellowo {return 602;} location ^ ~ / hello {return 601;}-request localhost/world return 600-request localhost/world2 localhost/test/world return other-request localhost/hello return 600-request localhost/hello/123 return 601-request localhost/hellow return 601-request localhost/test/hellowo return 601-request localhost/test/hello return other
So you can know:
= is an exact and complete match with the highest priority; in regular matching, if ~ and ^ ~ match the rule at the same time, ^ ~ will take priority; ^ ~ this rule will not match the later path in the request url, such as the above / test/hello does not match ^ ~ does not support regular, compared with =, the range is wider, hellowo can be matched by ^ ~, but = will not match ~ it can be matched as long as it is included in the path. For example, the / test/hellowo above returns 602.
Test example 2:
Location ~ / hello {return 602;} location ~ / helloworld {return 601;}-request localhost/world/helloworld return 602-request localhost/helloworld return 602
Adjust the above order
Location ~ / helloworld {return 601;} location ~ / hello {return 602;}-request localhost/helloworld return 601-request localhost/world/helloworld return 601-request localhost/helloWorld return 602
So when you match regularly at the same time
First priority match Note: if you are not case sensitive, use ~ * to put the exact match first as far as possible.
Test example 3:
Location ^ ~ / hello/ {return 601;} location / hello/world {return 602;}
In this scenario, there is a routing rule that does not match, so what is the actual test?
-http://localhost/hello/wor returns 601-http://localhost/hello/world returns 602-http://localhost/hello/world23 returns 602-http://localhost/hello/world/123 returns 602
As can be seen from the above example
When there is no match, full matching is priority ^ ~
2) [uri]
Here we mainly fill in the path path that needs to be matched. According to the previous symbol, we can fill in either the exact path path or the regular expression. The following is mainly about the regular.
. : match any character except newline character?: repeat 0 or 1 time +: repeat 1 or more times *: repeat 0 or more times\ d: match number ^: start of matching string $: introduction to matching string {n}: repeat n times {n Repeat n or more times [c]: match a single character c [a-z]: match any one of the parentheses () of the lowercase letters of amurz It can be referenced later by $1, and $2 represents the content in the previous second (). What is confusing in the rule is the escape of special characters.
Routing Forwardin
Requesting path matching is only the first step. After the matching is complete, how can the request be forwarded to other web services?
1. Reverse proxy
A common usage posture is to use the nginx proxy request to forward to other internal web services
Mainly realized through prixy_pass
Location ^ ~ / webs {proxy_pass http://127.0.0.1:8080/webs;}
The meaning of the above rule is to forward all requests that start with webs to the web service on port 8080.
The above is directly forwarded to an ip. If multiple machines provide services, you can configure it like this.
# # the following is placed in parentheses in http as the first layer upstream test.online {server 120.11.11.11 upstream test.online 8080 weight=1; server 120.11.11.12 upstream test.online 8080 weight=1;} location ^ ~ / webs {proxy_pass http://test.online; proxy_redirect default;}
2. Rewrite command
The function of rewrite is to use global variables provided by nginx or variables set by yourself, combined with regular expressions and flag bits to achieve url rewriting and redirection.
Rewrite can only be placed in server {}, location {}, if {}, and can only work on strings after the domain name except for the passed parameters, such as
Http://jb51.net/a/we/index.php?id=1&u=str
Rewrite / a/we/index.php only.
Syntax: rewrite regex replacement [flag]
Example:
Location ^ / hexo {root'/ Users/yihui/GitHub/';} location ~ / hello {rewrite ^ (/ hello). * $/ hexo/public/index.html last; return 603;}
Forward everything that begins with hello to / hexo/public/index.html
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.