1.Nginx解決服務器宕機問題,Nginx配置服務器宕機策略,如果服務器宕機,會找下一台機器進行訪問
配置nginx.cfg配置文件,在映射攔截地址中加入代理地址響應方案
location / {
proxy_connect_timeout 1;
proxy_send_timeout 1;
proxy_read_timeout 1;
proxy_pass http://backserver;
index index.html index.htm;
}
2.解決網站跨域問題
Nginx解決跨域問題,實現方案:
www.a.com:8080/a
www.b.com:8081/b
如果在b工程的頁面直接發送ajax請求a時會發生跨域問題,那么解決方案為:將A和B同時代理到Nginx,由Nginx做請求路由,直接在B工程頁面中直接訪問Nginx即可
server {
listen 80;
server_name www.wdksoft.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location /a {
#proxy_connect_timeout 1;
#proxy_send_timeout 1;
#proxy_read_timeout 1;
proxy_pass http://www.a.com:8080/a/;
index index.html index.htm;
}
location /b {
#proxy_connect_timeout 1;
#proxy_send_timeout 1;
#proxy_read_timeout 1;
proxy_pass http://www.b.com:8081/b/;
index index.html index.htm;
}
}
B頁面請求:
$("#button").click(function () {
$.ajax({
url:"http://www.wdksoft.com/a/AServlet?username="+$("#username").val(),
type:"GET",
success:function (result) {
alert(result);
}
})
});
3.Nginx配置防盜鏈
利用Nginx進行來源地址攔截,只要來源地址符合原資源地址,則可以訪問,否則返回4.3狀態碼
server {
listen 80;
server_name fdl.wdksoft.com;
#charset koi8-r;
#access_log logs/host.access.log main;
#攔截所有關於jpg|jpeg|JPG|png|gif|icon格式的請求
location ~ .*\.(jpg|jpeg|JPG|png|gif|icon)$ {
#驗證blocked來源地址不為空並且符合referers配置的地址
#none允許來源地址為空
valid_referers blocked http://fdl.wdksoft.com/a fdl.wdksoft.com/a;
#如果不符合則會return 403
if ($invalid_referer) {
rewrite ^/ http://www.a.com:8080/a/img/zysx.png;
#return 403;
}
}
location /a {
proxy_pass http://www.a.com:8080/a/;
index index.html index.htm;
}
}
4.Nginx防止DDOS流量攻擊
DDOS流量攻擊:頻繁的發送請求,造成寬帶占用,其他客戶端無法訪問
Nginx解決DDOS流量攻擊,利用limit_req_zone限制請求次數 limit_conn_zone限制連接次數
#限制IP的每秒請求次數
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
#限制同一個IP同一時間內創建連接次數
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
server_name ddos.wdksoft.com;
location /a {
limit_conn addr 1; #同一時間內只能建立一次連接
limit_req zone=one;
proxy_pass http://www.a.com:8080/a/;
index index.html index.htm;
}
}