今天要在Nginx上設置禁止通過IP訪問服務器,只能通過域名訪問,這樣做是為了避免別人把未備案的域名解析到自己的服務器IP而導致服務器被斷網,從網絡上搜到以下解決方案:
Nginx的默認虛擬主機在用戶通過IP訪問,或者通過未設置的域名訪問(比如有人把他自己的域名指向了你的ip)的時候生效
最關鍵的一點是,在server的設置里面添加這一行:
listen 80 default;
后面的default參數表示這個是默認虛擬主機。
這個設置非常有用。
比如別人通過ip或者未知域名訪問你的網站的時候,你希望禁止顯示任何有效內容,可以給他返回500.
目前國內很多機房都要求網站主關閉空主機頭,防止未備案的域名指向過來造成麻煩。就可以這樣設置:
server {
listen 80 default;
return 500;
}
也可以把這些流量收集起來,導入到自己的網站,只要做以下跳轉設置就可以:
server {
listen 80 default;
rewrite ^(.*) http://www.linuxidc.com permanent;
}
==============================
按照如上設置后,確實不能通過IP訪問服務器了,但是在應該用中出現當server_name后跟多個域名時,其中一個域名怎么都無法訪問:
設置如下:
server
{
listen 80;
server_name www.linuxidc.com linuxidc.com
沒更改之前,通過server_name 中的www.linuxidc.com linuxidc.com均可訪問服務器,加入禁止IP訪問的設置后,通過linuxidc.com無法訪問服務器了,www.linuxidc.com可以訪問
用 nginx -t 檢測配置文件會提示warning:
[warn]: conflicting server name “linuxidc.com” on 0.0.0.0:80, ignored
the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
最后通過在listen 80 default;后再加server_name _;解決,形式如下:
#禁止IP訪問
server
{
listen 80 default;
server_name _;
return 500;
}
或者
server {
listen 80 dufault;
server_name _;
rewrite ^(.*) http://www.linuxidc.net permanent;
}
這樣,通過linuxidc.com就能訪問服務器了,問題解決了,但具體原因還是不清楚。
分類:Nginx 成功分享標簽:301, ipNginx 設置301重定向2010年3月2日iNginx沒有評論
第一種情況:訪問A站定向到B站
server {
server_name www.linuxidc.net ;
rewrite ^(.*) http://www.linuxidc.com$1 permanent;
}
第二種情況:不是訪問A站的全部重定向到指定頁面
server {
server_name www.linuxidc.net;
if ($host != ‘linuxidc.net’ ) {
rewrite ^/(.*)$ http://www.linuxidc.com/$1 permanent;
}
}
如果寫在第一個server段
使用IP訪問時也將被重定向
億恩科技地址(ADD):鄭州市黃河路129號天一大廈608室 郵編(ZIP):450008 傳真(FAX):0371-60123888
聯系:億恩小凡
QQ:89317007
電話:0371-63322206 本文出自:億恩科技【www.vbseamall.com】
服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]
|