Last update at :2023-12-27,Edit by888u
We often encounter other domain names being resolved to our own IP, especially if there are more domain names resolved on IP segments that are not brand new, we can prevent this from happening by prohibiting IP access. I have previously introduced how to set up a WEB server built with Apache and how to set it up to be accessed only through domain names and prohibit IP access. Today I will talk about how to build a WEB service with Nginx.
Add this line in the server settings:
listen 80 default;
The default parameter afterindicates that this is the default virtual host.
This setting is very useful.
For example, when someone accesses your website through IP or unknown domain name, and you want to prevent any valid content from being displayed, you can return 500 to them.
Website owners close empty host headers to prevent unregistered domain names from pointing to them and causing trouble. You can set it like this:
- server {
- listen 80 default;
- return 500;
- }
You can also collect this traffic and import it to your own website. Just make the following jump settings:
- server {
- listen 80 default;
- rewrite ^(.*) http://www.thissite.com permanent;
- }
After setting up as above, it is true that the server cannot be accessed through IP. However, when server_name
is followed by multiple domain names, one of the domain names cannot be accessed:
The settings are as follows:
Before the change, the server can be accessed through www.thissite.com
xxx.com
in server_name
, and IP access is prohibited. After setting up, the server cannot be accessed through xxx.com
, but can be accessed through www.thissite.com
Using nginx -t
to detect the configuration file will prompt a warning:
Finally, the solution is solved by adding server_name _;
after listen 80 default;
. The form is as follows:
- #Block IP access
- server{
- listen 80 default;
- server_name _;
- return 500;
- }
or
- server {
- listen 80 dufault;
- server_name _;
- rewrite ^(.*) http://www.thissite.com permanent;
- }
In this way, you can access the server through this website.com and the problem is solved.
This method is actually very simple. After reading this website, I feel pretty good. It is also very helpful to me. I am not sure who started it. After all, most of the tutorials now have a lot of tutorials, and some need Try to practice the feasibility yourself, some of them are no longer suitable!
Recommended site searches: second-level domain name application, domain name registration information query, free server permanent use, space website, US server website, foreign virtual space, Hong Kong cn2 server, php space application, mainland China agent IP, virtual host server,
发表评论