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/01 Report--
This article shares with you the content of the sample analysis of the server and location configuration for forwarding requests in ngnix. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Like URL:www.mask_dev2.com:9999/login/.
The first half of the server tube, namely: www.mask_dev2.com:9999
Location is in charge of the second half, that is, / login/
A nginx can be configured with multiple server.
Each server can be configured with multiple location.
The first half of the URL controls which server is selected, and the second half controls which location is selected, and finally decides where to request.
Configuration of server
Server {listen 9999; server_name www.mask_dev2.cn; location / {default_type text/html; content_by_lua 'ngx.say ("
First
")';}} server {listen 9999; server_name www.mask_dev2.*; location / {default_type text/html; content_by_lua 'ngx.say ("
Second
")';}} server {listen 9998; server_name _; location / {default_type text/html; content_by_lua 'ngx.say ("
Third
")';}}
First of all, the address of the request nginx must be the server where the requested nginx is located, that is, the ip is fixed.
In other words, it doesn't matter what server_name is, it all refers to the current server.
So how does the current server correspond to multiple domain names? you only need to add this to the corresponding dns server, for example, temporarily treat this machine as a dns server and modify the hosts.
127.0.0.1 localhost
127.0.0.1 www.mask_dev2.cn
127.0.0.1 www.mask_dev2.com
Server matching order
The matching priorities of server_name and host are as follows:
1. Perfect match
2. Wildcards come first, such as * .test.com
3. The latter, such as www.test.*
4. Regular matching, such as ~ ^\ .www\ .test\ .com $
If none of them match,
1. Give priority to those with default or default_server after the listen configuration item
2. Find the first server block that matches the listen port
Location configuration
After you find the server, go to the specific location.
Server {listen 9998; server_name _ Location = / {# Rule A} location = / login {# Rule B} location ^ ~ / static/ {# Rule C} location ~\. (gif | jpg | png | js | css) ${# Rule D} location ~ *\. Png$ {# Rule E} location! ~. Xhtml$ {# Rule F} location! ~ *. Xhtml$ {# Rule G} location / {# Rule H}
Grammar rules:
Location [= | * | ^ ~] uri {… }
= the beginning indicates an exact match
The beginning of ^ ~ means that uri begins with a regular string, which can be understood as matching the url path. Nginx does not encode url, so the request is / static/20%/aa, which can be matched by the rule ^ ~ static/ aa (note spaces).
~ begins with a case-sensitive regular match
The beginning of ~ * indicates a case-insensitive regular match
! And! * are case-sensitive mismatches and case-insensitive mismatches, respectively.
/ Universal match, any request will be matched.
In the case of multiple location configurations, the matching order is as follows (for reference, it has not been verified yet, just try it, don't be rigid, just for reference):
First match =, then match ^ ~, then regular match in the order in the file, and finally give it to / universal match. When a match is successful, stop the match and process the request according to the current matching rule.
But generally not so complicated, there are three points.
Default request.
Page request.
Background logical request.
# directly match the root of the website, and visit the home page of the website more frequently through the domain name. Using this will speed up the processing, according to the official website. # this is forwarded directly to the backend application server, or it can be a static home page # the first required rule location = / {proxy_pass http://tomcat:8080/index} # the second required rule is to handle static file requests, which is the strength of nginx as a http server # there are two configuration modes Directory matching or suffix matching. Choose one or use location ~ *\. (gif | jpg | jpeg | png | css | js | ico) ${root/ webroot/res/ } # the third rule is the general rule, which is used to forward dynamic requests to the back-end application server # non-static file requests are dynamic requests by default, according to the actual grasp of # after all, the popularity of some current frameworks, with .php, .jsp suffixes are rare location / {proxy_pass http://127.0.0.1:8080/} Thank you for reading! This is the end of the article on "sample analysis of server and location configuration for forwarding requests in ngnix". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.