nginx try_files, proxy_pass and upstream nginx try_files, proxy_pass and upstream wordpress wordpress

nginx try_files, proxy_pass and upstream


The issue you're experiencing with the redirect loop in your question is that basically every request, even for static files tries to route via your index.php?$args block.

I see 2 possible solutions here. First, if you are willing to implement the nginx setup with a single nginx instance, look at this thread: NGINX try_files with multiple named locations and this blog post http://linuxplayer.org/2013/06/nginx-try-files-on-multiple-named-location-or-server

What needs to happen, is you need to first test if the resource is present on the upstream as is (i.e., doesn't return 404). If it does, then you serve it as is. If it does not, then you would be calling the rewrite block which tries to put it as a parameter to index.php. So you would end up with something like this (sorry, I don't really have a chance to test this, but hopefully it gives an idea):

location / {    try_files $uri $uri/ @phpproxy;}location @phpproxy {    proxy_pass http://phpfarm_52;    proxy_set_header Host $host;    proxy_set_header X-Forwarded-For $remote_addr;    proxy_intercept_errors on;    recursive_error_pages on;    error_page 404 = @rewrite_proxy;}location @rewrite_proxy {    rewrite ^/(.*)$ /index.php?$1 break;    proxy_pass http://phpfarm_52;    proxy_set_header Host $host;    proxy_set_header X-Forwarded-For $remote_addr;}

The second solution (that I would personally prefer) is to front every upstream with its own nginx. Then the top nginx which is fronting all others will have a much cleaner structure with simple proxy_pass's (+ maybe some static content) and that's it. It will also cut down on the request round-trip, since you wouldn't need to resolve 404's coming from the upstreams.