Nginx prevents direct access to Web servers using IP addresses

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.