current position:Home>Nginx multiple servers_ How does name match

Nginx multiple servers_ How does name match

2022-04-29 06:55:48lgq2016

nginx Multiple server_name How to match ?
Simply look at this problem , I think this question is stupid , How to match ? Is not based on server_name Does the name match ?

Yes , I thought so at first , And in the previous notes :Nginx Study log ( Two ) Mapping different domain names to different ports through reverse proxy The records are also matched according to the name .

But when I resolve a new domain name :blog.hjljy.cn When you get to the server , nginx server_name There's no time to configure this domain name, just configure :www.hjljy.cn This domain name , In the browser, type blog.hjljy.cn This domain name , According to the above idea, the server should not be found . The fact is that although the visit is blog.hjljy.cn But it finally jumped to www.hjljy.cn .

And then in nginx Multiple configurations server_name , Access to the corresponding domain name can jump to the corresponding domain name service , The situation is consistent with the notes written before :Nginx Study log ( Two ) Mapping different domain names to different ports through reverse proxy .

But when I resolve another domain name :api.hjljy.cn When you get to the server , Find access to this api.hjljy.cn, Or jump to www.hjljy.cn The above to .

Finally, after many attempts and consulting materials, we get the following conclusions :

1 It's really through server_name Match and forward the requested
2 If there is no match server_name, Will default to default_server Go to Then from default_server Process this request .
3 If there is a match server_name But here server If there is no processing method for this request , It will also jump to... By default default_server Go to .
for example : blog.hjljy.cn Only the following request processing methods are configured . When my request is : blog.hjljy.cn/501.html And if the corresponding request can be forwarded normally, it can enter But if it is blog.hjljy.cn/401.html It will jump to www.hjljy.cn/401.html

location = /50x.html {
root /usr/local/nginx/html/myerror;
}

default_server The problem of
According to specified

listen 80 default_server;
1
Do not display the specified words , Default first server_name

Reference resources :https://www.oschina.net/question/12_3565 It's a way to deal with mismatched requests , Although I'm useless now , However, it can be recorded for future use .

server {
listen 80 default_server;
server_name _;
return 404;
}

Link to the original text :https://blog.csdn.net/ycf921244819/java/article/details/104452281

copyright notice
author[lgq2016],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/116/202204260559165428.html

Random recommended