Nginx防止直接用IP访问Web服务器的设置方法

官方文档中提供的方法: 

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; 


说白了就是只要是访客用ip访问就直接重置444错误。但是这样好像又不太友好,如果能直接给跳转到该web server的网址就好了。配置如下: 

复制代码代码如下:

server { 
listen 80 default_server; 
server_name _; 
rewrite ^ http://www.domain.com$request_uri?; 


这样还是有一点问题,某些特别的地址,我需要用ip访问,其他的都禁止,如何配置呢?比如说我想让监控宝直接用ip访问我的机器的nginx状态信息,其他的用ip访问的所有请求都跳转到域名上。 

复制代码代码如下:

server { 
listen 80 default_server; 
server_name _; 
location /xxxxx{ 
stub_status on; 
access_log off; 

location /{ 
rewrite ^ http://www.domain.com$request_uri?; 


这样就实现了我们想要的功能了。



相关文章
推荐文章
热门文章

微信公众号推荐

相关推荐