0

I got strange Nginx behavior while trying to make site config. I need Nginx to use article.php in case the file is missing on server. But it works strange. If the query is example.com/snaff, it gets redirected to article.php. But if the query is example.com/snaff.php it just throws 404 error in browser.

I use Ubuntu 20.04 + Nginx 1.18.0. Earlier the same config worked ok on Ubuntu 18.04 + Nginx 1.19.0.

Can't understand what's going wrong.

Here is the config code.

server {
    listen 80;
    server_name example.com www.example.com;
    #return 404; # managed by Certbot

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
}

server {
    listen 443 ssl http2;

    server_name example.com www.example.com;
    root /var/www/example.com/public_html/;

    index index.php index.html index.htm;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    access_log /var/log/nginx/example_access.log;
    error_log /var/log/nginx/example_error.log error;

    etag on;

    gzip            on; 
    gzip_comp_level 5;
    gzip_min_length 10;
    gzip_proxied    any;
    gzip_types      *;
    gzip_disable "msie6";

    client_max_body_size 15m;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        add_header Cache-Control "no-store, no-cache, must-revalidate";
    }

    location ~ /\.ht {
        deny all;
    }

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /article.php;
    }
}
Zealot
  • 103
  • 3

1 Answers1

0

Maybe your snippets/fastcgi-php.conf file changed with the new distribution to this one:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

The cause of your problems is the try_files $fastcgi_script_name =404; line. I suggest you to remove this snippet at all and use something more simple like

location ~ \.php$ {
    try_files $fastcgi_script_name /article.php;
    include fastcgi.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    add_header Cache-Control "no-store, no-cache, must-revalidate";
}
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37