WordPress位於reverse proxy後的設定方式
參考文章 https://medium.com/@tyh409700530/nginx-reverse-proxy-wordpress-in-https-mode-a21658f23978 ,使用 nginx,於 nginx 設定檔設定:
# location 設定 location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Forwarded-Proto $scheme; proxy_redirect off; }
確保wp-config.php加入以下原始碼,否則使用HTTPS連線時,有機會遇到redirect loop (HTTP 302):
if (isset($_SERVER[‘HTTP_X_FORWARDED_HOST’])) { $_SERVER[‘HTTP_HOST’] = $_SERVER[‘HTTP_X_FORWARDED_HOST’]; } if (isset($_SERVER[‘HTTP_X_FORWARDED_PROTO’])) { if ($_SERVER[‘HTTP_X_FORWARDED_PROTO’] == ‘https’) { $_SERVER[‘HTTPS’] = ‘on’; } }
官網提供的範例是:
if( strpos( $_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false ) $_SERVER['HTTPS'] = 'on';