http proxy模塊參數
nginx功能的代理功能是是通過http proxy模塊來實現的。默認在安裝Nginx是已經安裝了http proxy模塊,可以直接使用。
http模塊相關參數 |
說明 |
proxy_set_header |
設置http請求header項傳給后端服務節點,例如:可實現讓代理后端的服務節點獲取訪問客戶端用戶的真實IP地址 |
client_body_buffer_size |
用於指定客戶端請求主體緩沖區大小,此處如果了解前面的http請求包的原理就好理解了 |
proxy_connect_timeout |
表示反向代理與后端節點服務器連接的超時時間,即發起握手等候相應超時時間 |
proxy_send_timeout |
表示代理后端服務器的數據回傳時間,即在規定時間之內后端服務器必須傳完所有的數據,否則Nginx將斷開這個連接 |
proxy_read_timeout |
設置Nginx從代理的 后端服務器獲取信息的時間,表示連接建立成功后,Nginx等待后端服務器的相應時間,其實是Nginx已經進入后端的排隊之中等候處理的時間 |
proxy_buffer_size |
設置緩存區大小,默認該緩存區等於指令proxy_buffer設置的大小 |
proxy_buffer |
設置緩存區的數量和大小,Nginx從代理的后端服務器獲取的響應信息,會放置到緩存區 |
proxy_busy_buffer_size |
用於設置系統很忙是可以使用的proxy_buffer大小,官方推薦大小為proxy_buffers*2 |
proxy_temp_file_write_size |
指定proxy緩沖臨時文件的大小 |
相關重要參數
相關重要參數 |
參數說明 |
proxy_psss http://server_pools |
通過proxy_pass功能把用戶的請求轉向到反向代理定義的upstream服務器池 |
proxy_set_header Host $host |
在代理后端服務器發送的http請求頭中加入host字段信息,用於后端服務器配置有多個虛擬主機主機是可以識別是那個虛擬主機。這是節點服務器多虛擬主機時的關鍵配置 |
proxy_set_header X-Forwarded-For $remote_addr;
|
在反向代理服務器發送http請求頭加入X-Forwarded-For字段信息,用於后端服務程序、日志等接受記錄真實的IP,而不是代理服務器的IP這是反向代理時,節點服務器獲取用戶真實IP的必要功能配置 后面服務器,記錄日志格式,main |
根據URL中的目錄地址實現代理轉發說明
1.1.1 案例背景:通過Nginx實現動靜分離,即通過Nginx反向代理規則實現讓動態資源和靜態資源及其他業務分別由不同的服務器解析,已解決網站性能、安全、用戶體驗等重要問題。
下面圖適合網站前端只使用同一個域名提供服務的場景,例如,用戶訪問的域名是www.etiantian.org,然后,當用戶請求求www.etiantian.org/upload/xx地址時,代理會分配請求到上傳服務器池處理數據;當用戶請求
www.etiantian.org/static/xx地址時,代理會分配請求到靜態服務器池請求數據;當用戶請求
www.etiantian.org/xx地址時候,即不包含上述指定的目錄地址路徑時,代理會分配請求到默認的動態服務器池請求數據(注意:上面的xx表示任意路徑)
1.1 案例配置實戰
當用戶請求www.etiantian.org/upload/xx地址時,實現由upload上傳服務器池處理請求。
當用戶請求www.etiantian.org/static/xx地址時,實現由靜態服務器池處理請求
除此之外,對於其他訪問請求,全部默認的動態服務器池處理請求
負載均衡器上實現/配置:
用戶請求的URI |
負責處理的服務器 |
主機名 |
站點目錄 |
功能 |
/upload |
10.0.0.8:80 |
web01 |
html/www/upload |
upload服務器 |
/static |
10.0.0.7:80 |
web02 |
html/www/static |
static靜態服務器 |
/ |
10.0.0.9:80 |
web03 |
html/bbs |
默認 |
www.etiantian.org/bingbiang.html |
#測試proxy_set_header X_Forwarded_For 記錄客戶端IP地址
#讓web服務器員記錄客戶端IP

worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools { server 10.0.0.7:80 weight=4 max_fails=3 fail_timeout=30s; server 10.0.0.8:80 weight=4 max_fails=3 fail_timeout=30s; server 10.0.0.9:80 weight=4 max_fails=3 fail_timeout=30s; } server { listen 80; server_name www.etiantian.org; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } server { listen 80; server_name bbs.etiantian.org; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } } #在web服務器服務員監測日志信息 #[root@web01 ~]# tailf /application/nginx/logs/access.log #測試結果 #10.0.0.5 - - [25/May/2017:16:55:14 +0800] "GET /bingbing.html HTTP/1.0" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36" "10.0.0.253" #重啟Nginx,語法檢測。 #[root@lb01 conf]# /application/nginx/sbin/nginx -t #nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok #nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful #[root@lb01 conf]# /application/nginx/sbin/nginx -s reload ###根據用戶請求url目錄(URI )進行轉發 用戶請求 第一個里程碑 #配置文件 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream upload_pools { server 10.0.0.8:80; } upstream static_pools { server 10.0.0.7:80; } upstream default_pools { server 10.0.0.9:80; } server { listen 80; server_name www.etiantian.org; location /upload { proxy_pass http://upload_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location /static { proxy_pass http://static_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location / { proxy_pass http://default_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } } ########nginx 給予uri 目錄的轉發 #創建環境 ##web01 mkdir -p /application/nginx/html/www/upload echo "web01 upload" >/application/nginx/html/www/upload/bingbing.html ##web02 mkdir -p /application/nginx/html/www/static echo "web02 static" >/application/nginx/html/www/static/bingbing.html ##web03 echo "web03 default" >/application/nginx/html/www/bingbing.html ##測試結果 #[root@lb01 conf]# curl 10.0.0.5/bingbing.html #web03 default #[root@lb01 conf]# curl 10.0.0.5/static/bingbing.html #web02 static #[root@lb01 conf]# curl 10.0.0.5/upload/bingbing.html #web01 upload
##根據用戶請求的url目錄(URI)進行轉發 用戶的請求總的

####第一個里程碑-規划 分類 /upload 10.0.0.8:80 upload服務器 /static 10.0.0.7:80 static靜態服務器 / 10.0.0.9:80 默認 ####第二個里程碑-創建澡堂子 upstream upload_pools { server 10.0.0.8:80; } upstream static_pools { server 10.0.0.7:80; } upstream default_pools { server 10.0.0.9:80; } ##第三個里程碑-什么時候去某一個澡堂子(條件) location ====== 專門用來匹配 判斷 uri if ($uri ~ xxxx) location /static/ { proxy_pass http://static_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } #將符合upload的請求交給上傳服務器池upload_pools,配置如下: location /upload/ { proxy_pass http://upload_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } #不符合上述規則的請求,默認全部交給動態服務器池default_pools,配置如下: location / { proxy_pass http://default_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } ###第四個里程碑-配置lb負載均衡 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; upstream upload_pools { server 10.0.0.8:80; } upstream static_pools { server 10.0.0.7:80; } upstream default_pools { server 10.0.0.9:80; } server { listen 80; server_name www.etiantian.org; location /static/ { proxy_pass http://static_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location /upload/ { proxy_pass http://upload_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location / { proxy_pass http://default_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log logs/access_www.log main; } } ###第五個里程碑-創建環境 www.etiantian.org/bingbing.html www.etiantian.org/upload/bingbing.html www.etiantian.org/static/bingbing.html ##web01 mkdir -p /application/nginx/html/www/upload echo "web01 upload" >/application/nginx/html/www/upload/bingbing.html ##web02 mkdir -p /application/nginx/html/www/static echo "web02 static" >/application/nginx/html/www/static/bingbing.html ##web03 echo "web03 default" >/application/nginx/html/www/bingbing.html ###第五個里程碑-進行測試 #[root@lb01 conf]# curl www.etiantian.org/bingbing.html #web03 default #[root@lb01 conf]# curl www.etiantian.org/static/bingbing.html #web02 static #[root@lb01 conf]# curl www.etiantian.org/upload/bingbing.html #web01 upload ####下面三條要解析否則會報404 curl www.etiantian.org/bingbing.html curl www.etiantian.org/static/bingbing.html curl www.etiantian.org/upload/bingbing.html curl 10.0.0.5/bingbing.html curl 10.0.0.5/static/bingbing.html curl 10.0.0.5/upload/bingbing.html
根據用戶客戶端的設備(user_agent)轉發實踐
###nginx.conf lb01 基於 用戶的客戶端瀏覽器

worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream upload_pools { server 10.0.0.8:80; } upstream static_pools { server 10.0.0.7:80; } upstream default_pools { server 10.0.0.9:80; } server { listen 80; server_name www.etiantian.org; location / { if ($http_user_agent ~* "MSIE") { proxy_pass http://static_pools; } if ($http_user_agent ~* "Chrome") { proxy_pass http://upload_pools; } proxy_pass http://default_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } } ##因為10.0.0.5下沒有動態或靜態的東西 [root@lb01 conf]# curl 10.0.0.5/bingbing.html web03 default [root@lb01 conf]# curl 10.0.0.5/static/bingbing.html <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.10.2</center> </body> </html> [root@lb01 conf]# curl 10.0.0.5/upload/bingbing.html <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.10.2</center> </body> </html> #加A + 參數指定瀏覽器名字 [root@lb01 conf]# curl -A chrome 10.0.0.5/upload/bingbing.html web01 upload [root@lb01 conf]# curl 10.0.0.5/bingbing.html web03 default [root@lb01 conf]# curl -A msie 10.0.0.5/static/bingbing.html web02 static [root@lb01 conf]# ####訪問一個目錄 nginx 默認找的文件是 index.html index.htm [root@lb01 conf]# #####如果這些文件不存在 nginx報錯 403 [root@lb01 conf]# curl -A chrome 10.0.0.5/upload/oldboy.txt <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.10.2</center> </body> </html> ################## ###報404 [root@lb01 conf]# ####1.10.0.0.5 訪問的反向代理 lb01 [root@lb01 conf]# ####2.10.0.0.5 默認訪問第一個虛擬主機 server [root@lb01 conf]# ####3.查看對應的條件 uri 目錄 [root@lb01 conf]# # if ($http_user_agent ~* "MSIE") [root@lb01 conf]# # [root@lb01 conf]# # { [root@lb01 conf]# # proxy_pass http://static_pools; [root@lb01 conf]# # } [root@lb01 conf]# # if ($http_user_agent ~* "Chrome") [root@lb01 conf]# # [root@lb01 conf]# # { [root@lb01 conf]# # proxy_pass http://upload_pools; [root@lb01 conf]# # } [root@lb01 conf]# #proxy_pass http://default_pools; [root@lb01 conf]# ####4.最后找到的是 默認的池塘 里面沒有 upload 目錄 [root@lb01 conf]# ####5. 沒有這個目錄 404 找不到 [root@lb01 conf]# curl 10.0.0.5/upload/ <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.10.2</center> </body> </html> ############403錯誤 [root@lb01 conf]# curl -A msie 10.0.0.5/static/ <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.10.2</center> </body> </html> [root@lb01 conf]# ####1.lb01 [root@lb01 conf]# ####2.匹配的是第一個虛擬主機 10.0.0.5 www.etiantian.org [root@lb01 conf]# ####3.進入location [root@lb01 conf]# ####4.進入location / [root@lb01 conf]# ####5.判斷 [root@lb01 conf]# ####-A 裝作是msie [root@lb01 conf]# ####7.10.0.0.7 這個機器上面 有/static 目錄 [root@lb01 conf]# ####8.10.0.0.5/static/ ===== 10.0.0.5/static/index.html [root@lb01 conf]# ####9.找首頁文件 但是 首頁文件不存在就顯示403 默認去找index.html [root@lb01 conf]# [root@lb01 conf]# ####10.找不到就匯報403 錯誤 。 ##1.瀏覽器緩存 ctrl+F5 ##2.域名沒有解析 ##3.修改了配置文件,沒重啟 配置文件沒生效 [root@lb01 conf]# cat nginx.conf ####lb01負載均衡 worker_processes 1; events { worker_connections 1024; } http { include mime.types; sendfile on; keepalive_timeout 65; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; upstream uplaod_pools { server 10.0.0.8:80; } upstream static_pools { server 10.0.0.7:80; } upstream default_pools { server 10.0.0.9:80; } server { listen 80; server_name www.etiantian.org; location / { if ($http_user_agent ~* "MSIE") { proxy_pass http://static_pools; } if ($http_user_agent ~* "Chrome") { proxy_pass http://uplaod_pools; } proxy_pass http://default_pools; } access_log logs/access_www.log main; } }