PBX
PBX

Reputation: 3

NGINX PHP FastCGI multiple locations (alias) issue

First time posting and very new to all this. Have found the existing guides here fantastic but am struggling to get something to work. After alot of trial and error and looking my location blocks currently look like this - global PHP has been removed and included in my location blocks.

The first one works fine, the second one after a few changes now doesn't show a 403 Forbidden or 404 not found but shows a generic string 'File not found.'

My Nginx error log shows the following when visiting:

2018/02/26 19:13:47 [error] 25229#25229: *185968 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: X.X.X.X, server: domain.co.uk, request: "GET /test/ HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "domain.co.uk"

I've been looking at various bits and pieces here about different fastcgi_param SCRIPT_FILENAME parameters but I can't get any of them to work. Any suggestions would be really appreciated as I've spent hours trying to get this to work and have managed to make some progress but stumped by what I assume will be the final task in making this work.

I've already removed try_files from the alias as I've been told that doesn't play nice (403's before that) and added $request_filename to my fastcgi_param bu that hasn't stopped this error.

location ~ ^((?!\/test).)*$ {
include /etc/nginx/mime.types;
    root /var/www/html/test;
    index index.php index.html index.htm;
    location ~ \.php$ {
        root /var/www/html/test;
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}



location ~ ^/test(.*)$ {
include /etc/nginx/mime.types;
    alias /var/www/html/test2/;
    index index.php index.html index.htm;
    location ~ \.php$ {
        alias /var/www/html/test2/;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename;
        include fastcgi_params;
    }
}

Upvotes: 0

Views: 1285

Answers (1)

Cemal
Cemal

Reputation: 1579

Afew minor tweaks to your conf.

  1. Move the root /var/www/html/test; outside the first location block. nginx pitfalls
  2. Replace alias with root in second block. according to nginx docs
  3. remove the second alias in second block. it was redundant anyway
  4. remove $request_filename; from fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename; same error on serverfault

which will basically look like below

 server {
   root /var/www/html/test;

   location /test {
      include /etc/nginx/mime.types;
      root /var/www/html/test2/;
      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }

   location / {
      include /etc/nginx/mime.types;
      index index.php index.html index.htm;
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }


}

Upvotes: 0

Related Questions