How to solve ERR_INVALID_REDIRECT for an HTTPS Wordpress website on Apache behind Nginx and varnish?

How to solve ERR_INVALID_REDIRECT for an HTTPS Wordpress website on Apache behind Nginx and varnish?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to solve ERR_INVALID_REDIRECT for an HTTPS Wordpress website on Apache behind Nginx and varnish?

When a WordPress site returns ERR_INVALID_REDIRECT specifically on the homepage, it is almost always a chain-reaction failure involving your reverse proxy stack. In this scenario, you have Varnish sitting in front of Nginx, which sits in front of Apache hosting the actual application. The browser receives a redirect from Apache, but by the time it tries to follow that link, the security layers (Nginx and Varnish) or the TLS handshake fail to validate the new URL, triggering the browser's hard block.

The error message indicates that the redirect location header points to an invalid destination. Based on the headers provided in your logs, Apache is correctly issuing a 301 Moved Permanently with a Location: https://www.mywebsite.com/. The culprit, however, lies in how Nginx handles this specific response before it reaches Varnish or the browser.

The Root Cause: Header Manipulation and TLS Handshake

The core issue stems from the interaction between your Apache virtual host and Nginx's proxy_redirect directive. In your Nginx configuration (mywebsite.conf), you have set proxy_redirect off;. This is a critical setting that tells Nginx not to modify the Location header sent by the backend (Apache).

While this prevents Nginx from accidentally rewriting redirects, it can sometimes interfere with how Varnish or the browser interprets the response if there are discrepancies in the Host header or SSL session management. More importantly, ERR_INVALID_REDIRECT often appears when the redirect loop involves a mismatch between the scheme (http vs https) or the hostname resolution within the proxy chain.

Your Apache virtual host listens on port 8080 and sends the redirect back to Nginx. Nginx then proxies this to Varnish (or directly to the browser depending on your exact deployment). If Nginx is configured with proxy_redirect off, it passes the header exactly as Apache sent it. However, if there is any issue with how the SSL session is re-established for the redirected URL, the browser will flag it as invalid.

Debugging the Chain of Trust

To isolate where the redirect breaks, you need to verify what each layer is actually seeing and sending. Start by checking if the redirect loop is occurring because Nginx is incorrectly handling the Location header or if Varnish is stripping it.

Run a verbose curl command against your backend directly to see how Apache responds without Nginx interference:

curl -v -H 'Host: www.mywebsite.com' http://127.0.0.1:8080/

Your logs show Apache responding with Location: https://www.mywebsite.com/ and the header X-Redirect-By: WordPress. This confirms WordPress is generating the redirect as intended, likely due to a canonical URL mismatch or a plugin forcing HTTPS on the root path.

The next step is to inspect Nginx's behavior. Ensure that the proxy_pass directive in your Nginx config is correctly pointing to the backend. In your snippet:

location / {
    proxy_pass http://127.0.0.1:6081;
    proxy_redirect off;
    # ... headers
}

Notice that proxy_pass points to port 6081, but your VirtualHost listens on 8080. If the backend Apache is indeed listening on 8080, this mismatch could cause Nginx to fail to connect to the correct service, resulting in an empty response or a fallback error that manifests as an invalid redirect in the browser. Verify that the port in proxy_pass matches the actual listening port of your Apache instance.

Configuration Adjustments and Best Practices

If the ports are correct, the issue might be related to how Nginx handles the Host header during the redirect. When Apache sends a 301 with an absolute URL, Nginx must ensure that the Host header in the subsequent request matches what the backend expects. Your current config sets proxy_set_header Host $host;, which is correct for passing the original host to the backend.

However, if you are using Varnish as a true reverse proxy before Nginx, Varnish might be caching the 301 response or stripping headers that Nginx relies on. Check your Varnish configuration to ensure it is not inadvertently rewriting URLs or stripping the Location header for the root path.

A common pitfall with WordPress upgrades (like moving to version 5.6) and theme changes is a mismatch between WP_HOME, WP_SITEURL, and the actual server URL. Even though you have set these in wp-config.php, ensure that no plugin is overriding them via filter hooks like home_url() or site_url(). If a plugin forces a redirect to a slightly different domain (e.g., missing a trailing slash or using a different subdomain), modern browsers will reject it as an invalid redirect.

Finally, consider enabling proxy_redirect on; temporarily in your Nginx config and observe if the error persists. Sometimes, letting Nginx manage the redirect headers can resolve mismatches caused by backend inconsistencies. If that works, investigate why Apache is sending a header that differs from what Nginx expects to rewrite.

By systematically verifying the port connectivity, ensuring consistent Host headers across the stack, and checking for plugin-level URL overrides, you can pinpoint exactly where the redirect chain breaks and fix the ERR_INVALID_REDIRECT error permanently.

Our network