nginx功能的代理功能是是經過http proxy模塊來實現的。默認在安裝Nginx是已經安裝了http proxy模塊,能夠直接使用。html
http模塊相關參數前端 |
說明nginx |
proxy_set_headerweb |
設置http請求header項傳給後端服務節點,例如:可實現讓代理後端的服務節點獲取訪問客戶端用戶的真實IP地址chrome |
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 |
下面圖適合網站前端只使用同一個域名提供服務的場景,例如,用戶訪問的域名是www.etiantian.org,而後,當用戶請求求www.etiantian.org/upload/xx地址時,代理會分配請求到上傳服務器池處理數據;當用戶請求
www.etiantian.org/static/xx地址時,代理會分配請求到靜態服務器池請求數據;當用戶請求
www.etiantian.org/xx地址時候,即不包含上述指定的目錄地址路徑時,代理會分配請求到默認的動態服務器池請求數據(注意:上面的xx表示任意路徑)
當用戶請求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 |
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
####第一個里程碑-規劃 分類 /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
###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; } }