Nginx prevents direct access to Web servers using IP addresses
After reading a lot of Nginx configurations, it seems that the problem of direct access to the Web by ip address is ignored, which is not conducive to SEO optimization theoretically. Therefore, we hope to avoid direct access to the website by IP address, but access by domain name, for more information, see the following.
Methods provided in the official documentation:
If you do not want to process requests with undefined "Host" header lines, you may define a default server that just drops the requests:
Server {listen 80 default_server; server_name _; return 444 ;}
To put it bluntly, as long as the visitor accesses the website using an ip address, the 444 error is reset directly. However, this seems unfriendly. If you can directly jump to the web server's URL, it would be nice. The configuration is as follows:
Server {listen 80 default_server; server_name _; rewrite ^ http://www.centoscn.com $ request_uri ?;}
In this case, there is still a problem. For some special addresses, I need to use ip addresses to access them. Others are not allowed. How can I configure them? For example, if I want the monitoring site to directly access the nginx status information of my machine using an ip address, all other requests accessed using an ip address will jump to the domain name.
Server {listen 80 default_server; server_name _; location/xxxxx {stub_status on; access_log off;} location/{rewrite ^ http://www.centoscn.com $ request_uri ?;}}
In this way, the functions we want are implemented.