使用 limit_rate 限制客戶端傳輸數據的速度**
1、編輯/etc/nginx/nginx.conf
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k; #對每個連接的限速爲2k/s
}
2 啓用 nginx proxy 代理
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.62.157:80;
# proxy_redirect default;
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_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
}
}
proxy_pass :真實服務器的地址,可以是ip也可以是域名和url地址
#proxy_redirect :如果真實服務器使用的是的真實IP:非默認端口。則改成IP:默認端口。(可選)
proxy_set_header:重新定義或者添加發往後端服務器的請求頭
proxy_set_header X-Real-IP :啓用客戶端真實地址(否則日誌中顯示的是代理在訪問網站)
proxy_set_header X-Forwarded-For:記錄代理地址
proxy_connect_timeout:後端服務器連接的超時時間發起三次握手等候響應超時時間
proxy_send_timeout:後端服務器數據回傳時間就是在規定時間之內後端服務器必須傳完所有的數據
proxy_read_timeout :nginx接收upstream(上游/真實) server數據超時, 默認60s, 如果連續的60s內沒有收到1個字節, 連接關閉。像長連接
2、負載均衡 upstream配置
upstream youngfitapp {
server 192.168.62.157:8080; #也可以是域名
server 192.168.62.158:8080 backup; #熱備
ip_hash; ip_hash:nginx會讓相同的客戶端ip請求相同的服務器。
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://youngfitapp;
}
}
server 192.168.62.157:8080 weight=2 max_fails=2 fail_timeout=2;
- down,表示當前的server暫時不參與負載均衡。
- backup,預留的備份機器。當其他所有的非backup機器出現故障或者忙的時候,纔會請求backup機器,因此這臺機器的壓力最輕。
- max_fails,允許請求失敗的次數,默認爲1。當超過最大次數時,返回錯誤。
- fail_timeout,在經歷了max_fails次失敗後,暫停服務的時間單位秒。max_fails可以和fail_timeout一起使用。
nginx在1.9.0的時候,增加了一個 stream 模塊,用來實現四層協議(網絡層和傳輸層)的轉發、代理、負載均衡等。stream模塊的用法跟http的用法類似,允許我們配置一組TCP或者UDP等協議的監聽,然後通過proxy_pass來轉發我們的請求,通過upstream添加多個後端服務,實現負載均衡。
#4層tcp負載
stream {
upstream myweb {
hash $remote_addr consistent;
server 172.17.14.2:8080;
server 172.17.14.3:8080;
}
server {
listen 80;
proxy_connect_timeout 10s;
proxy_timeout 30s;
proxy_pass myweb;
}
}
1、ip_hash
ip_hash使用源地址哈希算法,將同一客戶端的請求總是發往同一個後端服務器,除非該服務器不可用。
ip_hash語法:
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down;
}
2、sticky_cookie_insert
使用sticky_cookie_insert啓用會話親緣關係,這會導致來自同一客戶端的請求被傳遞到一組服務器的同一臺服務器。與ip_hash不同之處在於,它不是基於IP來判斷客戶端的,而是基於cookie來判斷。因此可以避免上述ip_hash中來自同一客戶端導致負載失衡的情況。(需要引入第三方模塊才能實現)
sticky模塊 (也可以理解基於域名的訪問)
upstream backend {
server backend1.example.com;
server backend2.example.com;
sticky_cookie_insert srv_id expires=1h domain=3evip.cn path=/;
} #訪問域名 3evip.cn 會轉到上面兩臺服務器上
server {
listen 80;
server_name 3evip.cn;
location / {
proxy_pass http://backen;
}
}
expires:設置瀏覽器中保持cookie的時間
domain:定義cookie的域
path:爲cookie定義路徑
防盜鏈配置
[root@nginx-server ~]# vim /etc/nginx/nginx.conf
# 日誌格式添加"$http_referer"
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# valid_referers 使用方式
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location
server {
listen 80;
server_name localhost;
location ~ .*\.(gif|jpg|png|jpeg)$ {
root /usr/share/nginx/html;
valid_referers none blocked *.qf.com 192.168.1.10;
if ($invalid_referer) {
return 403;
}
}
}
因爲none允許爲空值訪問,所以加不加ip都可以訪問,如果把none擦除,就不可以了
重載nginx服務
- none : 允許沒有http_refer的請求訪問資源;
- blocked : 允許不是http://開頭的,不帶協議的請求訪問資源---被防火牆過濾掉的;
- server_names : 只允許指定ip/域名來的請求訪問資源(白名單);
準備兩臺機器,一張圖片網站服務器
2.2、Rewrite flag
rewrite 指令根據表達式來重定向URI,或者修改字符串。可以應用於server,location, if環境下每行rewrite指令最後跟一個flag標記,支持的flag標記有:
last 相當於Apache裏的[L]標記,表示完成rewrite。默認爲last。 還繼續匹配
break 本條規則匹配完成後,終止匹配,不再匹配後面的規則
redirect 返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址
permanent 返回301永久重定向,瀏覽器地址會顯示跳轉後URL地址
http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.html
server {
listen 80;
server_name www.testpm.com;
location /a {
root /html;
index 1.html index.htm;
rewrite .* /b/2.html permanent;
}
location /b {
root /html;
index 2.html index.htm;
}
}
# http://www.youngfit.com/a/1.html ==> http://jd.com/a/1.html
location /a {
root /html;
if ( $host ~* youngfit.com ){
rewrite .* http://jd.com$request_uri permanent;
}
}
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/last.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /break/ {
root /usr/share/nginx/html;
rewrite .* /test/break.html break;
}
location /last/ {
root /usr/share/nginx/html;
rewrite .* /test/last.html last;
}
location /test/ {
root /usr/share/nginx/html;
rewrite .* /test/test.html break;
}
}
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo "last" > test/last.html
[root@localhost html]# echo "break" > test/break.html
[root@localhost html]# echo "test" > test/test.html
http://192.168.1.247/break/break.html break
http://192.168.62.159/last/last.html test
- last 標記在本條 rewrite 規則執行完後,會對其所在的 server { … } 標籤重新發起請求;
- break 標記則在本條規則匹配完成後,停止匹配,不再做後續的匹配;
root 、alias 指令區別**
location /img/ {
alias /var/www/image/;
}
#若按照上述配置的話,則訪問/img/目錄裏面的文件時,ningx會自動去/var/www/image/目錄找文件
location /img/ {
root /var/www/image;
}
#若按照這種配置的話,則訪問/img/目錄下的文件時,nginx會去/var/www/image/img/目錄下找文件。
- alias 是一個目錄別名的定義,
- root 則是最上層目錄的定義。