Nginx proxy pass php index problems

Chris B asked:

I am having slight issues making my indexes work with nginx serving static content and passing php to apache on a different server.

Basically if I need it to try index.php on the backend apache server when xxx.co.uk/ is requested. If no index.php or index.html exists on the nginx server i get 403 Forbidden. If i request xxx.co.uk/index.php behaviour is as expected and apache serves the page.

Could anyone suggest a solution to this? My initial thoughts was using try_files – but exactly how I am unsure – my tinkering didn’t work!

I hope I have been clear, if not then please ask and I will try to elaborate further.

Nginx settings:

server
{
   listen 80;
   server_name www.xxx.co.uk xxx.co.uk;
   access_log /srv/www/xxx.co.uk/logs/access.log;
   error_log /srv/www/xxx.co.uk/logs/error.log;

   root /srv/www/xxx.co.uk/public_html;

   index index.php index.html;

   # deny access to apache .htaccess files
   location ~ /.ht
   {
        deny all;
   }

   location ~* ^.*.php$ {

        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://192.168.0.6:80;
    }   
}

and apache:

 <VirtualHost 192.168.0.6:80>
     ServerAdmin webmaster@xxx.co.uk
     ServerName xxx.co.uk
     ServerAlias xxx.co.uk www.xxx.co.uk
     DocumentRoot /srv/www/xxx.co.uk/public_html/
     ErrorLog /srv/www/xxx.co.uk/logs/error.log
     CustomLog /srv/www/xxx.co.uk/logs/access.log combined
     DirectoryIndex index.php
</VirtualHost>

My answer:


You need a try_files and a location to put it in.

Example:

location / {
    try_files $uri $uri/ =404;
}

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.