nginx as proxy to wordpress

August 11, 2014 23.3k 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?

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
  • Replace


    with just


    Does that fix it?

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

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!