參考資料:real_ip和X-Forwarded-for詳解
user nginx nginx; worker_processes auto; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; worker_rlimit_nofile 102400; # worker進程最大打開文件句柄數 events { use epoll; multi_accept on; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; charset UTF-8; log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$remote_addr" "$upstream_addr" "$request_time" "$request_body"'; sendfile on; # 傳統文件傳輸方式:硬盤—>內核緩沖區—>用戶緩沖區—>內核socket緩沖區—>協議引擎 # sendfile則少了內核到用戶和用戶到內核兩次拷貝,大大提升了傳輸性能。 tcp_nopush on; keepalive_timeout 65; send_timeout 20; # 兩個連續的寫操作超時時間 server_tokens off; # 禁止錯誤頁面顯示nginx版本信息 open_file_cache max=102400 inactive=20s; # 設置緩存中元素的最大數量,超過則刪除最近最少使用的 # inactive期間未被訪問則刪除 open_file_cache_min_uses 2; # inactive期間最小訪問次數,未到這個數則刪除 open_file_cache_valid 60s; # 每60s檢查一次 open_file_cache_errors on; # fix CVE-2017-7529 bug max_ranges 1; # 禁用multipart range分片功能,漏洞修復措施 gzip on; # 啟用GZIP壓縮響應 gzip_proxied any; # 為所有代理請求啟動GZIP gzip_comp_level 4; # 壓縮比,1壓縮比最小速度最快,9壓縮比最大速度最慢 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_min_length 1k; # 設置允許壓縮的響應最小字節數,從header頭中的Content-Length中進行獲取。 gzip_buffers 4 16k; # 設置緩沖內存大小,以16k為單位原數據大小的4倍 gzip_http_version 1.1; # 設置壓縮響應所需的請求的最小HTTP版本 gzip_vary on; # 添加“Vary: Accept-Encoding”響應頭 client_max_body_size 300m; # 設置clent請求體的大小,在請求頭Content-Length中規定,超過返回413 client_body_buffer_size 128k; # 設置讀取客戶端請求體的緩沖區大小,如果body大於這個數,則寫入臨時文件 proxy_headers_hash_bucket_size 1024; # 設置頭部哈希表大小 proxy_headers_hash_max_size 512; # #設置頭部哈希表的最大值,不能小於你后端服務器設置的頭部總數。 proxy_connect_timeout 600; # 與后端服務器建立連接的超時時間,應該不超過75秒 proxy_read_timeout 600; # 從后端服務器讀取響應超時時間 proxy_send_timeout 600; # 發送請求到后端服務器超時時間 proxy_next_upstream error http_404; # 當返回響應出現error 404時,可以將請求分發下一個服務器繼續處理,提高平台訪問成功率 proxy_buffers 4 32k; # 設置緩沖區的大小和數量,從后端服務器取得的響應會放置這里 # 如果響應過大,超過緩沖區的會緩沖到磁盤(目錄由proxy_temp_path設定) proxy_buffer_size 16k; # 用於讀取從后端服務器接收響應的第一部分的緩沖區,通常包含一個小的響應頭 proxy_busy_buffers_size 64k; # nginx在后端響應尚未完全讀取時,這塊緩沖區開始向客戶端發送響應,其余繼續讀響應 # 一般為為proxy_buffers中單個緩沖區大小的2倍 proxy_temp_file_write_size 128k; # 當響應寫入臨時文件時,一次訪問寫入臨時文件的數據大小 proxy_set_header X-Forwarded-For $remote_addr; # 重新定義發往后端的請求頭,$remote_addr 客戶端ip地址 limit_conn_zone $binary_remote_addr zone=addr:10m; #限制單個IP的並發連接數,防止ddos攻擊,$binary_remote_addr二進制IP地址,addr設置IP池名稱,1m可以儲存32000個並發會話 upstream testapp_pool { # 定義一組后端服務器,默認使用加權輪詢法分配請求;如果請求過程發生錯誤,就會被分配下一個服務器 ip_hash; # 實現session保持 server 172.30.20.11:8080; server 172.30.20.21:8080; server 172.30.20.12:8080; server 172.30.20.22:8080; } server { listen 80; real_ip_header X-Forwarded-For; # 定義真實客戶端IP所在的請求頭 set_real_ip_from 172.30.100.111; # 定義可信IP,XFF中不可信即為客戶端真實IP real_ip_recursive on # 當配置為off時,Nginx會把real_ip_header指定的請求頭中的最后一個IP作為真實客戶端IP; # 當配置為on時,Nginx會遞歸解析real_ip_header指定的請求頭,最后一個不匹配set_real_ip_from的IP作為真實客戶端IP。 server_name www.webnode1.com 172.30.11.12; add_header X-Frame-Options SAMEORIGIN; # 防止該網站頁面被其他網站嵌套,SAMEORIGIN:表示該頁面可以在相同域名頁面的 frame 中展示 access_log /app/nginx/logs/www.webnode1.com_access_80.log main; error_log /app/nginx/logs/www.webnode1.com_error_80.log; root /app/nginx/html/default; # 請求的根目錄 error_page 500 502 503 504 404 400 403 http://www.webnode1.com/50x.html; # 將以上響應類型重定向到URL location ~* .*\.(tar|gz|zip|rar|msi|iso|gho|mp3|rmvb|mp4|wma|wmv|rm|war|bak)?$ { deny all; } location = / { access_log /app/nginx/logs/www.webnode1.com_access_80.log main; error_log /app/nginx/logs/www.webnode1.com_error_80.log; rewrite ^/$ http://www.webnode2.com permanent; # 重定向到webnode2,permanent返回永久的301重定向 } location ^~ /testapp { # 根據URL配置參數,~*不區分大小寫 access_log /app/nginx/logs/access_testapp_80.log main; error_log /app/nginx/logs/error_testapp_80.log; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header Host $host; # Host的含義是表明請求的主機名,重寫請求頭中Host值 proxy_set_header X-Forwarded-For $http_x_forwarded_for; # $http_x_forwarded_for變量是請求中的X-Forwarded-For信息, proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 把真實客戶端IP和反向代理IP通過逗號分隔,添加到請求頭中,如果請求頭不存在XFF,則$proxy_add_x_forwarded_for就等於$remote_addr proxy_pass http://testapp_pool; # 設置后端服務器的協議、地址和映射目錄,這里采用的為服務器組方式指定 limit_rate 1m; # 限制下載速度1m/s limit_conn addr 2000; # 限制每個IP並發2000連接 } } server { listen 443 ssl; server_name test.com; ssl_certificate certs/epcc_server_2017.cer; ssl_certificate_key certs/epcc_server.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_session_cache shared:SSL:1m; # 設置會話重用的方式。shared:所有worker進程共享緩沖;SSL:緩沖name;1m:大小,1m大約存儲4000個會話 # 這是通過Session ID重用,客戶端和服務器端都保存了會話key,通過每個連接的唯一標識,可以實現重用會話 ssl_session_timeout 5m; # 設置緩存超時時間為5分鍾 ssl_ciphers HIGH:!aNULL:!MD5; # ssl加密格式 ssl_prefer_server_ciphers on; # 指定在使用SSLv3和TLS協議時,服務器密碼應優先於客戶端密碼 location / { return 404; #root html; #index index.html index.htm; } } }