差異處
這裏顯示兩個版本的差異處。
下次修改 | 前次修改 | ||
tech:nginx_proxy_real_ip [2019/02/11 19:01] – 建立 Jonathan Tsai | tech:nginx_proxy_real_ip [2021/04/09 10:29] (目前版本) – [Nginx Proxy (192.168.11.234)] jonathan | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== Nginx 擔任 Web Proxy 傳遞真實 IP 的設定方式 ====== | ||
+ | 困擾很久經過 Nginx 擔任的 Proxy 無法傳遞 Real IP 到 Web Server 的問題終於找到解法. | ||
+ | * 首先要確認 Nginx 編譯時是否有 --with-http_realip_module 確認方式如下 <code sh> | ||
+ | nginx -V | ||
+ | </ | ||
+ | [root@ct-wiki ~]# nginx -V | ||
+ | nginx version: nginx/ | ||
+ | built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) | ||
+ | built with OpenSSL 1.0.2k-fips | ||
+ | TLS SNI support enabled | ||
+ | configure arguments: --prefix=/ | ||
+ | </ | ||
+ | * 官網提供給 CentOS 7 的版本預設就有納入 --with-http_realip_module | ||
+ | * 實際設定驗證環境: | ||
+ | * CloudFlare CDN -> Nginx Proxy (192.168.11.234) -> Nginx Web Server(192.168.11.233) | ||
+ | |||
+ | ===== 設定方式 ===== | ||
+ | ==== Nginx Proxy (192.168.11.234) ==== | ||
+ | * 編輯 Proxy 內轉給 www.ichiayi.com 的設定檔 www_ichiayi.conf <code sh> | ||
+ | vi / | ||
+ | server { | ||
+ | server_name | ||
+ | |||
+ | access_log / | ||
+ | error_log / | ||
+ | |||
+ | # Cloudflare IP List | ||
+ | set_real_ip_from 103.21.244.0/ | ||
+ | set_real_ip_from 103.22.200.0/ | ||
+ | set_real_ip_from 103.31.4.0/ | ||
+ | set_real_ip_from 104.16.0.0/ | ||
+ | set_real_ip_from 104.24.0.0/ | ||
+ | set_real_ip_from 108.162.192.0/ | ||
+ | set_real_ip_from 131.0.72.0/ | ||
+ | set_real_ip_from 141.101.64.0/ | ||
+ | set_real_ip_from 162.158.0.0/ | ||
+ | set_real_ip_from 172.64.0.0/ | ||
+ | set_real_ip_from 173.245.48.0/ | ||
+ | set_real_ip_from 188.114.96.0/ | ||
+ | set_real_ip_from 190.93.240.0/ | ||
+ | set_real_ip_from 197.234.240.0/ | ||
+ | set_real_ip_from 198.41.128.0/ | ||
+ | set_real_ip_from 2400: | ||
+ | set_real_ip_from 2606: | ||
+ | set_real_ip_from 2803: | ||
+ | set_real_ip_from 2405: | ||
+ | set_real_ip_from 2405: | ||
+ | set_real_ip_from 2c0f: | ||
+ | set_real_ip_from 2a06: | ||
+ | |||
+ | # use any of the following two | ||
+ | # | ||
+ | real_ip_header X-Forwarded-For; | ||
+ | |||
+ | location / { | ||
+ | set_real_ip_from | ||
+ | real_ip_header | ||
+ | real_ip_recursive on; | ||
+ | |||
+ | proxy_pass | ||
+ | proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; | ||
+ | proxy_redirect off; | ||
+ | proxy_buffering off; | ||
+ | proxy_set_header Host $host; | ||
+ | proxy_set_header X-Real-IP $remote_addr; | ||
+ | proxy_set_header | ||
+ | : | ||
+ | : | ||
+ | </ | ||
+ | * 設定完成後看到的 log 檔就可看到來源實際 IP, 不會是 CloudFlare 的 Proxy IP < | ||
+ | : | ||
+ | 46.229.168.142 - - [11/ | ||
+ | 46.229.168.129 - - [11/ | ||
+ | : | ||
+ | </ | ||
+ | * CloudFlare 傳遞過來出現的 log 最後面本來也會出現實際來源 IP Exp. " | ||
+ | |||
+ | ==== Nginx Web Server (192.168.11.233) ==== | ||
+ | * 編輯 Web Server 內 www.ichiayi.com 的設定檔 default.conf <code sh> | ||
+ | vi / | ||
+ | server { | ||
+ | listen 80; | ||
+ | server_name www.ichiayi.com; | ||
+ | autoindex off; | ||
+ | client_max_body_size 15M; | ||
+ | client_body_buffer_size 128k; | ||
+ | index index.html index.htm index.php doku.php; | ||
+ | access_log | ||
+ | error_log | ||
+ | root / | ||
+ | |||
+ | set_real_ip_from 192.168.11.234/ | ||
+ | # use any of the following two | ||
+ | # | ||
+ | real_ip_header X-Forwarded-For; | ||
+ | |||
+ | location / { | ||
+ | try_files $uri $uri/ @wiki; | ||
+ | } | ||
+ | |||
+ | location ~ ^/ | ||
+ | expires 30d; | ||
+ | } | ||
+ | : | ||
+ | : | ||
+ | </ | ||
+ | * 設定完成後看到的 log 檔就可看到來源實際 IP, 不會是 Proxy IP 192.168.11.234 < | ||
+ | : | ||
+ | 46.229.168.142 - - [11/ | ||
+ | 46.229.168.129 - - [11/ | ||
+ | : | ||
+ | </ | ||
+ | * 和 Proxy 內的 Log 相同, 只是少了 CloudFlare 提供最後的 IP 欄位 | ||
+ | * 一開始設定是使用 CF-Connecting-IP 但發現透過 vpn 內部連線無法轉回 vpn IP 而是 Proxy IP, 所以改成 X-Forwarded-For 就可以看到來自 vpn 的 IP. | ||
+ | |||
+ | |||
+ | ===== 參考網址 ===== | ||
+ | * https:// | ||
+ | * http:// | ||
+ | |||
+ | {{tag> |