2

Mostly that I need to be more familiar with the configuration of nginx and I have tried to search for an answer but have been unable to find one,I need to have all 401 403 and 404 errors directed to /error.php, however, my configuration seems to serve / instead. If you want to try this working have a look, it is on the PHP mirror at http://php.willtech.net.au/

The problem is noticeable when the page generates a link such as,
https://php.willtech.net.au/manual/en/function.random-bytes
However, the page that needs to be served is,
https://php.willtech.net.au/manual/en/function.random-bytes.php
Which works and I expect /error.php to resolve that automatically albeit the nginx configuration could do it I would suppose there will be other parts of the PHP mirror that also rely on the /error.php

I could make some presumptions but then I would not be so certain that there was not any other problem, it is a better opportunity to learn from somebody more experienced.

server {
    listen 80;
    port_in_redirect off;
    access_log  /var/log/nginx/www-access-site.log;
    error_log   /var/log/nginx/www-error-site.log;

    root  /var/www/html;
    index index.php index.html;
    error_page 401 403 404 /error.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~* \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param MIRROR_LANGUAGE "en";
        include fastcgi_params;
    }
}


5
  • 1
    I don't know much about nginx, but isn't try_files $uri $uri/ /index.php?$query_string; going to fall back to index.php if $uri doesn't exist? Why is /index.php?$query_string in that list? Aug 6 at 12:17
  • 1
    Changing /index.php?$query_string to /error.php resolves the example, appreciated. In the case of php.willtech.net.au/manual performance is different to php.net/manual
    – Willtech
    Aug 7 at 2:04
  • 1
    Why did you change it to /error.php as opposed to removing it? Why was it there in the first place? Where did your nginx configuration come from anyway? If you are mirroring for some other site, presumably you would want to use the same configuration they use. Aug 7 at 23:48
  • 1
    Routing everything to error.php would seem to be an "error" (no pun intended), when you are seemingly trying to serve extensionless URLs. (?) I notice your example URL now results in an external redirect to append .php (which is incorrect) and there is an erroneous intermediary redirect from HTTPS to HTTP and back to HTTPS. (?)
    – MrWhite
    Aug 7 at 23:59
  • 1
    @MrWhite Thank you for taking a look. In comparison with php.net it is a mirror site by rsync from the instructions php.willtech.net.au/mirroring.php - If I look carefully possibly the http/https redirects are provided by Cloudflare insisting on https possibly I suppose I could turn off... The .php extension request tends to generate links to additional pages including the .php extension but, short links do use error.php in the example https://php.willtech.net.au/mirroring-troubles.php https://php.willtech.net.au/echo https://php.willtech.net.au/manual/en/function.echo.php
    – Willtech
    Aug 22 at 10:49
3

This seems to be resolved with the following configuration, accepting your comments for improvement.

server {
server_name php.willtech.net.au;
    listen 80;
    port_in_redirect off;

    access_log  /var/log/nginx/www-access-site.log;
    error_log   /var/log/nginx/www-error-site.log;

    root  /var/www/html;
    index index.php;
    error_page 401 403 404 /error.php?$query_string;

    location / {
        try_files $uri $uri/ /error.php?$query_string;
    }
    
    location ~* \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param MIRROR_LANGUAGE "en";
        include fastcgi_params;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.