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 reverse proxy to Apache which is handling PHP, here's my nginx site config:

server {
        listen   80 default;
        server_name  localhost;

        access_log  /var/log/nginx/localhost.access.log;

        root /var/www/www.example.com/httpdocs;

        location ~ \.php$ {
                proxy_pass              http://www.example.com:80;
        }

        location ~ /\.ht {
                deny  all;
        }

        #location / {
                try_files $uri @proxy;
        #}

        location @proxy {
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size    10m;
                client_body_buffer_size 128k;
                proxy_connect_timeout   90;
                proxy_send_timeout      90;
                proxy_read_timeout      90;
                proxy_buffers           32 4k;
                proxy_pass              http://www.example.com:81;
        }
}

My issue is that requests for index.php are being returned by nginx unparsed, so my location specifically for handling php files is not doing what it should.

If I request index.php?q=whatever, which is a valid request for apache, then the site returns a 500 with the error:

2012/07/13 11:22:27 [alert] 8490#0: *17994 4096 worker_connections are not enough while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /index.php?q=whatever HTTP/1.0", upstream: "http://127.0.0.1:80/index.php?q=whatever", host: "www.example.com"
share|improve this question
1  
nginx is just proxying, and shouldn't be doing anything with the PHP files. What's Apache's config look like for executing the PHP? –  Shane Madden Jul 13 '12 at 3:35

1 Answer 1

up vote 3 down vote accepted
    location ~ \.php$ {
            proxy_pass              http://www.example.com:80;
    }

In your configuration, Nginx is listening in port 80. So passing index.php file to Nginx (port 80) will leave it unparsed. So, assuming that Apache listens on port 81, the correct proxy_pass directive would be...

    location ~ \.php$ {
            proxy_pass              http://www.example.com:81;
    }

To know more about how Nginx handles proxy requests, please check out the official wiki on HttpProxyModule.

share|improve this answer
    
Haha, oops, I'll fix that ASAP and see how it goes! I had originally ran nginx on 81 to leave apache on 80 as it was originally. That'll teach me to DRY. –  DanH Jul 13 '12 at 11:29
    
That solved everything, thanks! :) –  DanH Jul 15 '12 at 8:35
    
I'm glad it worked for you, @DanH! –  Pothi Jul 15 '12 at 10:31

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.