Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm using nginx as a frontend for apache2. Apache2 will handle all dynamic content.

This is my main file with nginx being a frontend for apache and having all dynamic pages forwarded to apache2:

server {
    listen 80; 

    root /var/www/websites/main/htdocs; 
    index index.html index.php index.htm;

    server_name *removed*;

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

    location ~ \.php$ {
        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:81;
    }

    location ~ /\.ht {
        deny all;
    }
}

This results in a redirect loop unfortunately with Wordpress.

When I use the following configuration, the website loads fine, although nginx is handling all static and dynamic content which is against the point.

server {
    listen 80; 

    root /var/www/websites/main/htdocs;
    index index.html index.php index.htm;

    server_name *removed*;

    location ~* ^.+\.(ico|jpg|jpeg|gif|png|css|txt|js|flv|swf|html|htm|eot|woff|ttf|svg)$
    {
        access_log off;
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location / {
        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:81;
    }

    location ~ /\.ht {
        deny all;
    }
}

Using Chrome Developer tools I find that this is a 301 redirect loop. I can access wp-admin without any problems.

share|improve this question
    
Why are you bothering to keep Apache here? It doesn't seem that you need it at all. –  Michael Hampton Sep 2 '13 at 22:11
    
I'm using apache2 to handle php requests. I have a varnish cache setup in between nginx and apache2. Would there be a more viable option? –  ssabetan Sep 2 '13 at 22:13
    
Are you terminating SSL? That's the usual reason for putting varnish behind nginx. Otherwise nginx is perfectly capable of replacing varnish and doing caching, and it's also capable of handling PHP with php-fpm, which often gets better performance. You probably could simplify this design significantly by getting rid of both varnish and Apache. –  Michael Hampton Sep 2 '13 at 22:16
    
As stated here, gist.github.com/fennb/1283573 nginx doesn't do "varnish-like" caching. It seems that it only caches things for seconds at a time. While this may be true, with apache2 and varnish out of the question, I will still be encountering the redirect problem. –  ssabetan Sep 2 '13 at 22:26
    
That's a microcache example specifically designed to cache for only one second. You can cache for as long as you want with nginx. As for WordPress, is your URL in the General settings the same as your server_name? –  Michael Hampton Sep 2 '13 at 22:27

4 Answers 4

add to your wordpress themes function.php ->

remove_filter('template_redirect', 'redirect_canonical');
share|improve this answer
    
This might work, but it is guaranteed to cause other problems in future. Don't do this. –  Michael Hampton Oct 13 '13 at 1:58

I realize it's been a while since you posted your question, but in case you're still struggling...

I was experiencing the same problem as you and solved it thanks to this blogpost. From what I can tell you need to define a backend for your proxy, using the ngx_http_upstream_module (documentation here). In nginx.conf, include the following lines:

http {

[...]

upstream backend {
        ip_hash;
        server 127.0.0.1:8081; #or other IP-address:port config - you may add several
    }

[...]

  server {

  [...]

      location / {
            try_files $uri $uri/ /index.php;
            proxy_pass http://backend;
      }

  [...]

  } # END "server"

} # END "http"
share|improve this answer
    
Thanks for the link, I'll look into it when I start messing with nginx and apache again. Cheers! –  ssabetan Nov 28 '13 at 23:30
    
This works for me. Thanks –  Hemc May 28 at 12:58

i think wordpress uses some kind of seo-friendly-path-style-urls or redirect, so you go like

index.php -> /nice-url -> index.php -> /nice-url 

due to your try_files - block.

what happens if you access /index.php directly? what doies your logs are telling?

you should look for an nginx-worpdress-receipt

share|improve this answer
    
When I try to access index.php directly I still get the redirect problem. The logs are spamming this line about 20 times per request: pastebin.com/rqpueHFA I have tried the nginx helper plugin for Wordpress and I have the same problem. –  ssabetan Sep 2 '13 at 22:37
    
i think you should change your nginx-config, since it cannot work with the apache-redirects. your try_files part is the problem. btw, nginx has some nice caching-options, still usefull even if you dont want to have static-files served by nginx –  that guy from over there Sep 2 '13 at 22:50

Try this instead: (Src: http://wiki.nginx.org/WordPress)

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

EDIT: I also recommend using PHP via FastCGI rather than a passthrough to Apache.

share|improve this answer
    
I thought about this solution also. Unfortunately it didn't work. –  ssabetan Sep 2 '13 at 23:04
    
Have you tried setting up PHP in Fast CGI mode rather than using Apache as a pass-through? –  Aaron Mason Sep 2 '13 at 23:09
    
I will attempt it since everyone says it's the way to go. To my understanding the reason I chose Varnish over nginx caching was because of it's simplicity integrated with Wordpress. –  ssabetan Sep 2 '13 at 23:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.