1

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"
DanH
  • 827
  • 2
  • 9
  • 26
  • 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 03:35

1 Answers1

3
    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.

Pothi Kalimuthu
  • 6,117
  • 2
  • 26
  • 38