nginx try_files, proxy_pass and upstream
<p>I'm building a dockerised testing 'platform' for PHP apps - specifically (for the moment) for WordPress. I'm using PHPFarm to serve different versions of PHP on different ports. Using nginx in front, I've got much of it working. ( <a href="https://github.com/richardtape/testit" rel="noreferrer">https://github.com/richardtape/testit</a> is the main repo )</p>

<p>The big issue I'm facing now is getting WordPress's "pretty permalinks" to work. In a standard nginx setup, it's just a case of something like</p>

<pre><code>location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}
</code></pre>

<p>But in order to be able to have nice urls from the host machine, and in order to have one code base, I'm using something along the following lines:</p>

<pre><code>server {
    listen 80;
    index index.php index.html index.htm;
    server_name 52.spaces.dev;

    location / {

        proxy_pass http://phpfarm_52;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
    root /var/www;
}

upstream phpfarm_52{
    server phpfarm:8052;
}
</code></pre>

<p>This, as it stands, works. (There are 5 more rules similar for this for PHP 5.3, 5.4, 5.5, 5.6 and 7) The home page loads on each of the different server_names from the host machine (and if you output the PHP version on each of them, you can see that you're getting a different PHP version).</p>

<p>However, the second I switch to an 'internal' url (or any non-root i.e. <a href="http://52.spaces.dev/about/" rel="noreferrer">http://52.spaces.dev/about/</a>), I get a 404. I've tried something similar to</p>

<pre><code>location / {
    try_files $uri $uri/ /index.php?$args
}

location ~ \.php$ {
    proxy_pass http://phpfarm_52;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
}
</code></pre>

<p>I get a redirect loop, depending on a few different ways I've tried it's either just been a series of 301 redirects and the page never loads or an error such as</p>

<pre><code>nginx_1      | 2016/04/08 20:31:29 [error] 5#5: *4 rewrite or internal redirection cycle while processing "/index.php", client: 192.168.99.1, server: 52.spaces.dev, request: "GET /favicon.ico HTTP/1.1", host: "52.spaces.dev", referrer: "http://52.spaces.dev/"
</code></pre>

<p>I'm stuck. I'm also pretty new to nginx configuration (which may be obvious) so I might well be doing something completely wrong and/or dumb. Any suggestions?</p>