1

First time posting and very new to all this - was advised to try here over stack overflow and have read a few different things linked but just cannot get my head around it. 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.'

www.mydomain.com - serve file index.php from /var/www/html/test

www.mydomain.com/test - should serve file from index.php from /var/www/html/test2 but it fails with the following error.

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;
    }
}

The user that recommended I try here advised

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

I did this however this was almost a step back resulting in a 404 nginx error when visiting /test/ with their suggested changes making it look like this as per their example -

 server {
   root /var/www/html/test;
   location ~ ^((?!\/test).)*$ {
      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;
      }
   }

   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;
      }
   }
}

I'm happy to work off either (I trust theirs more than my own) to try and get this worked out if anyone has any kind of knowledge that would help me with this.

3
  • Please show your requirements for different URL patterns and what they should serve, like: www.example.com should show file in /path/to/file.html and www.example.com/abc should show a page from PHP file /path/to/some/file.php. Without knowing your requirements this question cannot be answered. Feb 27, 2018 at 7:14
  • www.mydomain.com - should show index.php frm /var/www/html/test www.mydomain.com/test - should show index.php from /var/www/html/test2
    – PBX
    Feb 27, 2018 at 11:09
  • should point out www.mydomain.com works with either my original or the edits suggested on SO - its mydomain.com/test/ I am unable to get working.
    – PBX
    Feb 27, 2018 at 11:15

1 Answer 1

0

Cemal over on Stackoverflow has solved this for me!

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;
      }
   }


}

The problem with 404's came down to the files for the first location being in /var/www/html/test2 but with a location /test that gets prefixed on so the directory should be /var/www/html/test2 on web server end.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .