Resolving Too Many Redirects Issue with Nginx Reverse Proxy
If you’re setting up an Nginx reverse proxy to route traffic between Magento and Drupal sites on the same domain, you might encounter the “Too many redirects” error when accessing your Magento store URL. This issue arises due to improper configuration of the proxy_redirect directive.
Let’s troubleshoot this problem step-by-step:
Understanding the Issue
The primary cause is that Nginx is configured to redirect both incoming requests and outgoing responses, creating a loop that results in too many redirects.
In your current configuration:
- For Drupal: The
proxy_redirectline is correctly set up to rewrite the base URL fromhttp://10.21.120.12/tohttp://$server_name/. - For Magento: However, you’ve forgotten to include the trailing slash in the
proxy_redirectline (http://10.192.134.10/instead ofhttp://10.192.134.10/store/). This causes an infinite redirect loop when Nginx attempts to rewrite the outgoing Magento responses.
Resolving the Redirect Loop
To fix this issue, you need to modify your Magento proxy configuration as follows:
location /store {
proxy_pass http://10.192.134.10/store;
proxy_redirect http://10.192.134.10/store https://$server_name/store/;
# ... (other configuration lines remain the same)
}
Here’s what I’ve done:
- Changed
proxy_passto include the/store/path - Modified
proxy_redirectto rewrite the outgoing Magento URL correctly, including the trailing slash
By making these changes, Nginx will no longer create an infinite redirect loop when serving your Magento store.
Additional Best Practices for Nginx Reverse Proxy Configurations
While you’ve already set up most of the necessary directives (like proxy_set_header and buffer sizes), here are a few additional best practices to ensure optimal performance:
-
Enable Gzip Compression: Add the
gzip on;directive at the top-level server block to enable compression, reducing the size of data sent between Nginx and your upstream servers. -
Configure SSL/TLS Settings: If you’re using HTTPS, make sure to configure SSL/TLS settings properly, including enabling
ssl_session_cacheand setting appropriatessl_certificateandssl_certificate_keypaths. -
Optimize Buffer Sizes: While you’ve already set some buffer sizes, consider tuning them further based on your specific traffic patterns and server resources.
Conclusion
In summary, when configuring Nginx as a reverse proxy for multiple applications sharing the same domain (like Magento and Drupal), it’s crucial to:
- Ensure proper base URL rewriting in both
proxy_passandproxy_redirect - Include trailing slashes where necessary
- Follow best practices for buffer sizes and compression
By making these adjustments, you should be able to resolve the “Too many redirects” issue and serve your Drupal and Magento sites seamlessly under a single domain using Nginx.
Remember, proper configuration is key to ensuring optimal performance and security when working with reverse proxies. If you have further questions or need assistance with more complex setups, feel free to explore our Nginx Reverse Proxy guide for in-depth explanations.
Happy coding!