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 is requested. If no index.php or index.html exists on the nginx server i get 403 Forbidden. If i request 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:

   listen 80;
   access_log /srv/www/;
   error_log /srv/www/;

   root /srv/www/;

   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;

and apache:

     DocumentRoot /srv/www/
     ErrorLog /srv/www/
     CustomLog /srv/www/ combined
     DirectoryIndex index.php

My answer:

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


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.