對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2018/12/21 10:39:20
CentOS 6 建立 Nginx Reverse Proxy
Nginx 很常拿來做 Proxy / Reverse Proxy,設定方便又簡單
通常 Reverse Proxy 會放在非交戰區(DMZ) 來做為 Public 跟 Private 之間的連線,避免 attack 直接接觸到資料主機,也能降低資料主機的 Loading
範例架構
試想將 Web Server 放置內網,所有使用者都必須由 Reverse Proxy 代為交涉
為了瞭解概念,用 Nginx官網 來當作 Web Server
而我們必須要從 Reverse Proxy 來反向代理到 Nginx官網
環境
CentOS 6.6
Reverse Proxy: 172.16.10.10
目標 Web : http://nginx.com
安裝 Nginx Reverse Proxy
step1. 先把 Nginx 安裝起來
要從EPEL才能找到 Nginx
1 | $ yum install nginx |
step2. nginx.conf 常態性調整
1 2 3 4 5 6 7 8 | $ vim / etc / nginx / nginx . conf user nginx ; worker _processes 2 ; worker_cpu _affinity 01 10 ; events { use epoll ; worker _connections 1024 ; } |
step3. 設定 Reverse Proxy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | $ vim / etc / nginx / conf . d / default . conf server { listen 80 ; #Proxy的位置,提供給使用者連線 server _name 172.16.10.10 ; location / { root / usr / share / nginx / html ; index index . html index . htm ; #後端的Web伺服器,以Nginx官網為例 proxy_pass http : //nginx.com; #定義header記錄使用者IP proxy_set _header X - Real - IP $ remote_addr ; #讓後端的Web伺服器可以取得使用者真實IP proxy_set _header X - Forwarded - For $ proxy_add_x_forwarded_for ; #把 Protocol header 也往後送 proxy_set _header X - Forwarded - Proto $ http_x_forwarded_proto ; proxy_max_temp_file _size 0 ; } } |
其實設定檔很簡單,其常常忘記的就是忘記加上 $proxy_add_x_forwarded_for
若是沒有加上這個,在後端的 Web Server 取得的 access IP 都會是 Proxy Server 的 IP,而不是真實IP。
在後端如果也是使用 Nginx 的話,必須在 nginx.conf 加上一些設定
若是前端的 IP header 為 X-Forwarded-For
那就要設定為
1 2 3 4 | $ vim / etc / nginx / nginx .conf set_real_ip _from 125.119.141.65 ; real_ip _header X - Forwarded - For ; |
set_real_ip_from 就是前端的 Proxy IP
read_ip_header 是前端記錄真實 IP 的 header,必須相符合
原文出處:CentOS 6 建立 Nginx Reverse Proxy | Mr. 沙先生