官方文檔中提供的方法:
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;
}
說(shuō)白了就是只要是訪(fǎng)客用ip訪(fǎng)問(wèn)就直接重置444錯(cuò)誤。但是這樣好像又不太友好,如果能直接給跳轉(zhuǎn)到該web server的網(wǎng)址就好了。配置如下:
server {
listen 80 default_server;
server_name _;
rewrite ^ http://www.domain.com$request_uri?;
}
這樣還是有一點(diǎn)問(wèn)題,某些特別的地址,我需要用ip訪(fǎng)問(wèn),其他的都禁止,如何配置呢?比如說(shuō)我想讓監(jiān)控寶直接用ip訪(fǎng)問(wèn)我的機(jī)器的nginx狀態(tài)信息,其他的用ip訪(fǎng)問(wèn)的所有請(qǐng)求都跳轉(zhuǎn)到域名上。
server {
listen 80 default_server;
server_name _;
location /xxxxx{
stub_status on;
access_log off;
}
location /{
rewrite ^ http://www.domain.com$request_uri?;
}
}
這樣就實(shí)現(xiàn)了我們想要的功能了。