0

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;
    }
}
Cemal
  • 1,469
  • 1
  • 12
  • 19
PBX
  • 3
  • 4
  • Hello, you might want to ask this on [serverfault](https://serverfault.com/) or check the answers there before posting into **coding** QA – Cemal Feb 26 '18 at 20:08
  • Here's a [link](https://serverfault.com/questions/418565/serving-php-from-an-nginx-alias) that may be holding answers to your problem – Cemal Feb 26 '18 at 20:11

1 Answers1

0

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


}
Cemal
  • 1,469
  • 1
  • 12
  • 19
  • I actually started with a solid copy trying to just get the copy to show on the new location /test/ - unfortunately that didn't solve it. I've seen your other answers though and am reading through the link on serverfault so thank you very much for the advice. – PBX Feb 26 '18 at 20:25
  • I've updated the conf, can you retry and tell me if it worked for you? – Cemal Feb 26 '18 at 20:38
  • Hi Cemal, thanks for the advice. I made the changes and the main page is still working however /test/ is now outputting a 404 not found - the only way I amnaged to get it looking in the first place (but with the error in nginx) was actually the use of alias in the second block. https://stackoverflow.com/questions/34082831/nginx-configuration-with-multiple-location-blocks this where I got the nested location block inverse regex for - that led to 404's – PBX Feb 27 '18 at 00:10
  • then went into this https://stackoverflow.com/questions/35259014/forbidden-location-when-using-alias-in-nginx-for-relative-urls which is where I found the information on going from a 403 forbidden to getting it to where I am now, under the advice of removing removing tryfiles from the second block. With or without $request_filename which is what seemed to fix the exact same problem for OP in this thread – PBX Feb 27 '18 at 00:17
  • So what is your url that you're trying to match? I assumed your regex' were correct and didn't touch them. – Cemal Feb 27 '18 at 05:14
  • domain.com domain.com/test the regex works as far as I can tell. – PBX Feb 27 '18 at 11:10
  • can you try now with either of the configs, I've updated. – Cemal Feb 27 '18 at 11:21
  • Hi Cemel, cannot state how thankfully I am for you trying to help me here. I've tried the frist one and it fails with duplicate location /test (imagine this something to do with the regex) However tried with the second one and the first page works fine on domain.com however domain.com/test now 404'ing again. – PBX Feb 27 '18 at 11:38
  • The first block of the first config should actually match anything that doesn't start with /test, but if it fails, them I'm going to remove it. – Cemal Feb 27 '18 at 11:39
  • Sorry Cemal I acidentally submitted it by hitting enter trying to get to a new line, I updated my comment that wasn't all I was going to say there lol – PBX Feb 27 '18 at 11:41
  • can you post `tail /var/log/nginx/error.log` so we can see where it's actually trying to look for – Cemal Feb 27 '18 at 11:48
  • Good call! ```2018/02/27 11:53:01 [error] 26403#26403: *35807 "/var/www/html/test2/test/index.php" is not found (2: No such file or directory), client: x.x.x.x, server: mydomain.co.uk, request: "GET /test/ HTTP/1.1", host: "mydomain.co.uk"``` For some reason it's appending /test/ onto the directory which is what's causing the 404 – PBX Feb 27 '18 at 11:57
  • I could move the contents into a test folder surely that will work? Seems a bit of a hacky way around it. a /var/www/html/test2/**test**/index.php – PBX Feb 27 '18 at 12:02
  • well, `$fastcgi_script_name` is supposed to do that. Imagine you need to browse `foo.com/test/somefolder/a.php` if it didn't pass on the path, this will not work. – Cemal Feb 27 '18 at 12:03
  • Would you mind explaining to me how that works out? Sorry for being a total noob here. I've moved the contents of /test2 to /test2/test and it now picks it up but a lot of the PHP isn't rendering properly (presumably because of the file layout in the PHP itself which I can fix) I'm just wondering why it works for the first location without moving any files – PBX Feb 27 '18 at 12:09
  • 2018/02/27 12:10:21 [error] 27611#27611: *37435 open() "/var/www/html/test2/test/raw_data" failed (2: No such file or directory), client: 80.6.24.115, server: dunstablepogo.co.uk, request: "POST /test/raw_data HTTP/1.1", host: "dunstablepogo.co.uk", referrer: "https://dunstablepogo.co.uk/test/" Is now causing PHP render errors even though the files are there. How strange. – PBX Feb 27 '18 at 12:12
  • 1
    I believe this to be with my rewrite rules - I have some specifically for the base domain.com in the global section of the config, for whatever reason they aren't applying - I think I can solve that. rewrite ^/raw_data$ /raw_data.php?$1 last; rewrite ^/gym_data$ /gym_data.php?$1 last; rewrite ^/motd_data$ /motd_data.php?$1 last; rewrite ^/(.*)map.common.js$ /static/js/map.common.php last; rewrite ^/weather_data$ /weather_data.php?$1 last; – PBX Feb 27 '18 at 12:18
  • good luck, if you can't remember, these types of server related problems actually belong to [ServerFault](http://www.serverfault.com) – Cemal Feb 27 '18 at 12:20