I have a setup in which a frontend nginx server handles multiple names on one IP for HTTP port 80 and HTTPS port 443 (through SNI) and forwards the requests to distinct backend HTTP servers, based on name and/or path. One of these backends is a WordPress installation, and that one is problematic: WordPress tends to insert absolute URLs into everything. And since it doesn’t know that the frontend was accessed through HTTPS, it inserts HTTP URLs. Worse, when using the WordPress HTTPS plugin and choosing to force administrative logins through HTTPS only, you can end up with an endless redirect loop.
A quasi standard signal from frontend to backend in this case is the X‑Forwarded-Ssl HTTP header, which, when set to on, should indicate that the frontend used HTTPS. Unfortunately, WordPress ignores the header. Instead, is_ssl() only checks the HTTPS server variable (and alternatively checks the server port to be 443).
The WordPress documentation referenced above offers a simple fix by overwriting the HTTPS variable in the wp-config.php file. Instead, for nginx with FastCGI, I propose this simpler, more generic, and, in my humble opinion, more elegant fix:
The default nginx FastCGI configuration includes a file fastcgi_params, which, among others, has this line:
fastcgi_param HTTPS $https if_not_empty;
Below this simply add
fastcgi_param HTTPS $http_x_forwarded_ssl if_not_empty;
(and of course have something along the lines of
proxy_set_header X-Forwarded-Ssl on;
in the corresponding proxy configuration on the frontend nginx)
This will set the HTTPS server variable when the X‑Forwarded-Ssl HTTP header is set, allowing all kinds of PHP programs to transparently discover that they’re used to through HTTPS.
Webmentions
[…] Pass the $_SERVER[‘HTTPS’] into php. It can be done by modifiing nginx configuration but for the time being, we have opted for a less direct approach – prepending a file in […]