Question

nginx as proxy to wordpress

  • Posted on August 11, 2014
  • germanoAsked by germano

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 www.hostname.com/blog/, 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?


Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

proxy_pass http://backend/$request_uri;

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

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
X-Pingback: http://104.131.199.80/xmlrpc.php
Location: http://www.insectashoes.com/

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_pass http://127.0.0.1:8080$is_args$args;
    proxy_redirect off;
}

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