Nginx + Apache + WordPress redirects to localhost/127.0.0.1

apache-2.2nginxWordpress

Anyone know how to fix an issue with Nginx + Apache + WordPress redirecting to localhost/127.0.0.1? I've tried a lot of different fixes, but none have worked for me.

I can go to http://domain.com/wp-admin just fine and use everything there normally. But if I try to go to http://domain.com it redirects to 127.0.0.1. Everything also works fine if I just run through Apache.

Here are the relevant portions of my nginx.conf:

server {                                
    listen 80;           
    server_name domain.com;      
    root /var/www/html/wordpress;       

    location / {                      
        try_files $uri $uri/ /index.php;
    }                                 

    location ~ \.php$ {
        proxy_pass http://127.0.0.1:8080; 
    }                                                                 
}

Here are the relevant portions of my httpd.conf:

Listen *:8080

ServerName <ip>

<VirtualHost *:8080>
    ServerAdmin test@test
    DocumentRoot /var/www/html/wordpress
    ServerName domain.com
</VirtualHost>

This is what my nginx log loks like:

<ip> - - [19/Jun/2012:22:35:35 +0400] "GET / HTTP/1.1" 301 0 "-" "Mozilla/5.0

This is what my httpd log looks like:

127.0.0.1 - - [19/Jun/2012:22:24:46 +0400] "GET /index.php HTTP/1.0" 301 - "-"

WordPress Address (URL) and Site Address (URL) both have same http://domain.com

Adding proxy_set_header Host $host; results in "This webpage has a redirect loop."

It also works if I use

location / {
  proxy_set_header Host $host;
  proxy_pass http://127.0.0.1:8080;
}

but not with any try_files statements that fall through to other locations.

Best Answer

This is an old question, but as I come across...

 location ~ \.php$ {

It's not enough to proxy requests to WordPress. Basically, you should do it the other way around :

Send all static file requests to Nginx explicitly :

location ~ \.(css|js|ico|jpg|jpeg|png|gif|svg|pdf)$ {
    try_files $uri $uri/ /index.html;
}

Then proxy the rest to Apache :

location / {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
}