nginx as proxy to wordpress

Posted August 11, 2014 30.7k views

I’m trying to use nginx as a proxy to wordpress but I’m getting weird behavior.

There is my nginx configuration:

location ~ ^/blog(/?)(.*) {
    proxy_pass http://<ip addr>/$2$is_args$args;
    proxy_redirect off;

When I access, I receive a redirect (301) to <ip addr>/blog/.

I checked the apache’s access.log and there is no record of the redirects, what makes me believe that is nginx that is doing that.

Any idea of what could be causing this?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

Try this instead:

location /blog {
    rewrite ^/blog/(.*)$ /$1 break;

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;

Does that work? (make sure you replace 8080 with the port apache is listening on)

Thanks for your reply.

I tried this configuration and now when I request /blog it works ok, but when I request /blog/anything the response is a redirect to root:

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 11 Aug 2014 16:27:16 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4

proxypass http://backend/$requesturi;

this does the trick for most scenarios as it matches the url that was requested. However, redirects don’t seem to work when using the proxy. Still scouring the interwebs for an answer