ngnix之筆記
#############################################################################
我們在使用的時候會遇到很多的惡意IP攻擊,這個時候就要用到Nginx 禁止IP訪問了。下面我們就先看看Nginx的默認虛擬主機在用戶通過IP訪問,或者通過未設置的域名訪問(比如有人把他自己的域名指向了你的ip)的時 候生效最關鍵的一點是,在server的設置里面添加這一行:
listen 80 default;
后面的default參數表示這個是默認虛擬主機。
Nginx 禁止IP訪問這個設置非常有用。
比如別人通過ip或者未知域名訪問你的網站的時候,你希望禁止顯示任何有效內容,可以給他返回500.目前國內很多機房都要求網站主關閉空主機頭,防止未備案的域名指向過來造成麻煩。就可以這樣設置:
server {
listen 80 default;
return 500;
}
也可以把這些流量收集起來,導入到自己的網站,只要做以下跳轉設置就可以:
server {
listen 80 default;
rewrite ^(.*) http://www.example.com permanent;
}
按照如上設置后,確實不能通過IP訪問服務器了,但是在應該用中出現當server_name后跟多個域名時,其中一個域名怎么都無法訪問,設置如下:
server {
listen 80;
server_name www.example.com example.com
}
沒更改之前,通過server_name 中的www.example.com example.com均可訪問服務器,加入Nginx 禁止IP訪問的設置后,通過example.com無法訪問服務器了,www.example.com可以訪問,用 Nginx -t 檢測配置文件會提示warning:
[warn]: conflicting server name “example.com” on 0.0.0.0:80,
ignored
the configuration file /usr/local/Nginx/conf/
Nginx.conf syntax is ok
configuration file /usr/local/Nginx/conf/Nginx.
conf test is successful
最后通過在listen 80 default;后再加server_name _;解決,形式如下:
#禁止IP訪問
server {
listen 80 default;
server_name _;
server_name www.example.com example.com
return 500;
}
這樣,通過example.com就能訪問服務器了。
#########################################################################################
nginx 的proxy_intercept_errors 和error_page的搭配使用,
proxy_intercept_errors
當上游服務器響應頭回來后,可以根據響應狀態碼的值進行攔截錯誤處理,與error_page 指令相互結合。用在訪問上游服務器出現錯誤,由nginx直接返回自定義的錯誤頁面
location /houtai {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/houtai;
proxy_intercept_errors on;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /404.html {
root /usr/share/nginx/html;
internal;
}
location = /50x.html {
root /usr/share/nginx/html;
internal;
http://blog.csdn.net/hellolingyun/article/details/37934815
#######################################################################
對于有些服務端接口返回是固定值的json,可通過配置nginx直接返回json,減少程序的加載對資源的占用,減少接口響應時間
通過return,指定頁面返回的內容
location ~* (request/update)$ {
default_type application/json;
return 200 '{"update":"no"}';
}
記得加default_type application/json ,不然瀏覽器打開時,是個文件。
#########################################################################
nginx 代理https后,應用redirect https變成http
原因分析:
瀏覽器到nginx是https,nginx到應用服務器變成http,
應用服務器,再做302 redirect的時候,返回的redirect 地址就好變成http的地址;
原因是spring mvc的servlet的secheme取值,request.getScheme()
是取請求里的一個scheme值,所有這個值在nginx代理時要設置成https
其中: request.getScheme() return http but not https.
解決方法:
目前是
proxy_redirect http:// $scheme://;
The same server needs to serve both HTTP and HTTPS, however, when the upstream issues a redirect (for instance, after a form is processed), all HTTPS requests are redirected to HTTP. The only thing I have found that will correct this issue is changing proxy_redirect to the following:
proxy_redirect http:// https://;
That works wonderfully for requests coming from HTTPS, but if a redirect is issued over HTTP it also redirects that to HTTPS, which is a problem.
Out of desperation, I tried:
if ($scheme = 'https') {
proxy_redirect http:// https://;
}
########################################################################
網站它可能不希望被網絡爬蟲抓取,例如測試環境不希望被抓取,以免對用戶造成誤導,那么需要在該網站中申明,本站不希望被抓取。有如下方法:
server {
listen 80;
server_name 127.0.0.1;
#添加如下內容即可防止爬蟲
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
{
return 403;
}
#########################################################################
Nginx設置HTTP認證:
1、安裝htpassword來創建和生成加密的用戶用于基礎認證(Basic Authentication)
sudo apt-get install apache2-utils
2、在Nginx托管的網站目錄下生成一個.htpasswd文件。如下的命令可以創建文件同步增加用戶和加密的密碼到文件中
浙公網安備 33010602011771號