Teson
Teson

Reputation: 6736

howto setup window nginx virtual directory with php support?

Ngigx + PHP-FPM setup and working in root-directory, but I'm having trouble getting virtual directories to work.

I want //localhost/pb/test.php to execute c:\opt\php\public\test.php but it breaks with "no input file specified". In fact, not even .html files works, but once working, I want the php-directive to work under /pb as well.

current nginx.conf:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm index.php;
    }

location /pb/ {
    root   /opt/php/public;
        index  index.html index.htm index.php;
}

location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9123;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include         fastcgi_params;
}
}

Upvotes: 2

Views: 6224

Answers (1)

cobaco
cobaco

Reputation: 10546

http://nginx.org/en/docs/http/ngx_http_core_module.html#location explains how nginx matches locations. in this case your prefix location /pb/ will match, and nginx will never get to the *.php matching location

what I would try is to set up a named location (the @bit makes it a named location):

location @fastcgi {
  fastcgi_pass    127.0.0.1:9123;
  fastcgi_index   index.php;
  fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include fastcgi_params;
}

and then refer to that in try_files directives from other locations, like this:

location /pb/ {
  root   /opt/php/public;
  index  index.html index.html;
  try_files $uri @fastcgi;
} 

location ~ \.php$ {
  alias @fastcgi;
}

the try files above would first try an exact match file name and if it doesn't find that it will pass the request to the @fastcgi location

alternatively you could offcourse simply repeat the fastcgi bits in a nested location block inside your /pb/ location

Upvotes: 1

Related Questions