I ran into a situation at work today.
10.1.10.184 and shtech.edu.cn point to the same server, and the client requires all requests on 10.1.10.184 to be processed on the domain name.
The way I thought of at that time was to use nginx to transfer all requests to 10.1.10.184 to the domain name for processing.
--
Several problems arise when doing this.
1: What does the matching order of the server block look like? How does it match?
2: Is it possible to set the access ip as a domain name? Will it be DNS resolution or IP?
3: How to rewrite my URL?
--
1: What does the matching order of the server block look like? How does it match?
We know that there are multiple server blocks in an nginx, so what rules are the server blocks matched according to?
put it in order
Exact match > Wildcard first (*.test.com) > After (eg www.test.* ) > Regular match (eg ~^\.www\.test\.com$)
If none of these match
the first server block that matches the listening port number
Here is a configuration that I tested by myself. If you want to really understand it, you still have to practice it yourself.
server { listen 80; location / { default_type text/html; return 502 "default"; } } server { listen 80; server_name hong.li.cn location / { default_type text/html; return 502 "hong.li.cn"; } } server { listen 80; server_name 192.168.27.27; location / { default_type text/html; return 502 $server_name; } }
--
2: Is it possible to set the access ip as a domain name? Will it be DNS resolution or IP?
Here my virtual machine IP is 192.168.27.27. I configured my local domain name
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 10.1.11.15 skyyqgl.imau.edu.cn 255.255.255.255 broadcasthost ::1 localhost 192.168.57.57 keli.hong.cn 192.168.27.27 hong.li.cn 192.168.27.27 jin.jin.cn 220.181.57.216 192.168.27.27
When I visit http://192.168.27.27/
It is not resolved through the local domain name. It is accessed directly using the IP.
--
3: How to rewrite my URL?
Here's where to learn today.
Nginx URL rewrite (rewrite) configuration and information details
grammar:
rewrite <regex> <replacement> [flag]; keyword regex content flag tag Keyword: where the keyword error_log cannot be changed Regular: perl compatible regular expression statement for rule matching Replacement content: replace the regular matching content with replacement flag tag: the flag tag supported by rewrite Flag tag description: last #After this rule is matched, continue to match the new location URI rule down break #This rule will be terminated after matching, and will no longer match any subsequent rules redirect #Return 302 temporary redirection, the browser address will display the redirected URL address permanent #Return 301 permanent redirection, the browser address bar will display the redirected URL address
example:
rewrite ^/(.*) http://www.czlun.com/$1 permanent;
After experimenting a few times, you will understand how it works.
Field Description:
rewrite is a fixed keyword, which means to start the rewrite matching rule The regex part is ^/(.*), which is a regular expression that matches the full domain name and the path address that follows The replacement part is http://www.czlun.com/$1 $1, which is taken from the content of the regex part (). The URL to redirect to after a successful match. The flag part permanent indicates a permanent 301 redirect flag, that is, jump to the new http://www.czlun.com/$1 address