0

When I use NGINX to create a reverse proxy for a WordPress Docker container, WordPress specifies WordPress Address (URL) and Site Address (URL) to be https://hiddenurl.com:443. So all my links contain the port 443, and if I delete this port, the website is no longer accessible due to an infinite redirect loop.

I have entered in the wp-config.php that SSL should be activated via $_SERVER['HTTPS'] = 'on';.

NGINX Config:

 location ~ /(?<wppath>.*) { rewrite ^/(.*) /$1 break; client_max_body_size 100M; proxy_pass http://127.0.0.1:7676/$wppath$is_args$args; proxy_http_version 1.1; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-forwarded protocol https; proxy_set_header upgrade $http_upgrade; proxy_set_header Connection "upgrade } 

This causes the domain hiddenurl.com to work, but hiddenurl.com/about does redirect to 127.0.0.1/about.

The redirect seems to come from WordPress: Response Headers

2 Answers 2

1

I solved it by using this as an nginx config:

 location / { proxy_pass http://127.0.0.1:7676; proxy_redirect off; 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 https; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; } 

And append this to the top of wp-config.php

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') $_SERVER['HTTPS'] = '1'; if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; } 
Sign up to request clarification or add additional context in comments.

Comments

0

I too had a WordPress 301 redirect loop using the WordPress container serving over port 80 behind an nginx reverse proxy with the following configuration:

server { # SSL configuration # listen 443 ssl; listen [::]:443 ssl; server_name _______.com.au www._______.com.au; proxy_redirect off; location / { proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://192.168.0.0:6080/; } ssl_certificate /etc/letsencrypt/live/_______.com.au/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/_______.com.au/privkey.pem; # managed by Certbot } server { if ($host = www._______.com.au) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = _______.com.au) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name _______.com.au www._______.com.au; return 301 https://_______.com.au$request_uri; } 

My wp_config.php was using the default configuration generated by Official WordPress Docker container. The problem occurred in the canonical.php function because of this line in the nginx config:

proxy_set_header Host $host:$server_port; 

When the url is passed into the canonical.php function a 301 redirect loop was setup because https://______.com.au:443 was different from https://______.com.au. Removing $server_port fixed the problem for me:

proxy_set_header Host $host; 

I hope that my time consuming investigation saves someone some time!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.