Skip to content

Proposal: Support for x-forwarded-proto #943

Closed
@mildsunrise

Description

@mildsunrise

Background

A lot of PHP apps (incorrectly) use HTTPS to determine the protocol that the browser made the request with. They then use it to construct the browser URL and determine when to redirect, etc.

Thus, when a reverse proxy is placed before the app, the URL isn't constructed correctly and you often get the dreaded "Too many redirects" error. Using reverse proxies is even more common in containers, so images like wordpress often patch the code so that the standard X-Forwarded-Proto header is obeyed and it behaves correctly behind a proxy:

			awk '
				/^\/\*.*stop editing.*\*\/$/ && c == 0 {
					c = 1
					system("cat")
					if (ENVIRON["WORDPRESS_CONFIG_EXTRA"]) {
						print "// WORDPRESS_CONFIG_EXTRA"
						print ENVIRON["WORDPRESS_CONFIG_EXTRA"] "\n"
					}
				}
				{ print }
			' wp-config-sample.php > wp-config.php <<'EOPHP'
// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
	$_SERVER['HTTPS'] = 'on';
}
EOPHP

Solution

Ideally, every application should be modified to obey reverse proxies, but there appears to be a tendency in the PHP world to, umm, not do that (otherwise I don't understand why these patches are still needed for maintained apps like Wordpress and such).

Manually patching the PHP to set HTTPS (as shown above) is error prone, fragile and app-dependent. A better way is adding the following:

SetEnvIf x-forwarded-proto https HTTPS=on

to the Apache config. It achieves the same, but is much simpler, robust and application-independent. So, we could put it in /etc/apache2/conf-available/reverse-proxy.conf and then, a2enconf reverse-proxy.conf in the Dockerfile is all that's needed to make it work.

We could even enable it by default. It would be awesome, especially for new users, but some things should be taken into account first:

  • Can it break existing images? Can it break apps that are proxy-aware? → Images/apps that are proxy-aware should obey X-Forwarded-Proto before HTTPS, so nothing should be broken.
  • Does it pose a security risk? → I have to research this one further.
  • Can it be unintuitive for users that write their own PHP with this image? → I think most users will expect the patched behaviour rather than the current one. If it's mentioned in the documentation, I don't see a big deal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RequestRequest for image modification or feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions