Nginx as reverse proxy for Apache HTTP
下面設定主要讓Nginx可以作為另一台server的reverse proxy
也就是說前端Nginx+後端Apache HTTPD(這兩台不一定要在同一台server/ip)
先說明一下:
- 紅色部分是主要設定,http, https都一樣,意思是讓nginx可以重導某個ip的80 port,如果server設定在同一台,則apache httpd必須是與nginx不同port (ex: httpd --> 8000, nginx --> 80,則這邊設定就變成proxy_pass http://$http_host:8000;)
- 藍色部分是如果nginx, httpd server在不同台時候,可以強制將後端網頁的ip與相對位置的ip均轉譯成前端的伺服器ip
server {
listen 80;
....(skip)
location / {
### Set headers ####
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
### Most PHP, Python, Rails, Java App can use this header ###
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_pass http://123.123.123.123:80;
}
}
server {
listen 443;
ssl on;
ssl_certificate /opt/local/etc/openssl/private/selfsigned.pem;
ssl_certificate_key /opt/local/etc/openssl/private/selfsigned.pem;
ssl_prefer_server_ciphers on;
error_page 500 502 503 504 /50x.html;
...(skip)
location / {
### Set headers ####
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
### Most PHP, Python, Rails, Java App can use this header ###
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_pass http://123.123.123.123:80;
}
}
附註:如果後端程式碼中有內涵絕對位置的鏈結或資源(ex: http://your.ip.address/style.css)的話,則透過nginx的reverse proxy僅只能對ip部分做置換,但卻不能針對協定做置換...(ex: 換成http://123.123.123.123/style.css),這樣實際上會影響到Chrome瀏覽器會block掉https頁面呼叫http部分的資源,但相同狀況在Firefox卻是OK...,也就是說,如果頁面上js, css或image等資源用到絕對位置,將會造成頁面破碎不堪@@ (如果有知道解法,歡迎提供給我<simonsu.mail@gmail.com>啦~謝謝!)