Score:1

404, 50x error handling HTML pages in nginx is not detected

cn flag

nginx version: nginx/1.18.0

This is my 'nginx' conf default 'vhost':

cat /etc/nginx/sites-enabled/000-default
server {
    listen 80;
    listen [::]:80;

    server_name example.org;
    return 301 https://$server_name$request_uri;
}

# HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    index index.html;

    server_name example.org www.example.org;
    root /home/www/example.org;

    include /etc/nginx/ssl.conf;

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }

    location / {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }

    error_page   500 502 503 504  /50x.html;
    error_page   404  /404.html;

    location = /50x {
         root   /home/www/example.org;
         # rewrite foobar.html to foobar, foobar.php to foobar
         if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
         try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    location = /404 {
         root   /home/www/example.org;
         # rewrite foobar.html to foobar, foobar.php to foobar
         if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
         try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    access_log  /var/log/nginx/example.org.access.log;
}

I have both '404.html' and '50x.html' files in my root directory.

If I call them directly, It's a success, but if I try to get an unknown page like https://example.org/non-existant, I get File not found string instead of my HTML page.

Any help?

TIA

Score:1
by flag

There is an error in your configuration file, update the location block for the 404 and 50x error pages

just replace this block:

location = /50x {
    root   /home/www/example.org;
    if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
    try_files $uri $uri/ $uri.html $uri.php?$args;
}
location = /404 {
    root   /home/www/example.org;
    if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
    try_files $uri $uri/ $uri.html $uri.php?$args;
}

with

location = /50x.html {
    root /home/www/example.org;
    internal;
}
location = /404.html {
    root /home/www/example.org;
    internal;
}

then you can reload nginx sudo nginx -s reload it should work properly.

Mévatlavé Kraspek avatar
cn flag
Sorry, thanks, but same behavior. Just get `File not found.`
Mévatlavé Kraspek avatar
cn flag
See my answer, you helped me++, but not the main issue. Thanks anyway
Score:0
cn flag

What I need to do to make it works: there's was a conflict, I need to explicitly set known paths I want to rewrite index.html index.php to index (an so on):

cat /etc/nginx/sites-enabled/000-default
server {
    listen 80;
    listen [::]:80;

    server_name example.org;
    return 301 https://$server_name$request_uri;
}

# HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    index index.html;

    server_name example.org www.example.org;
    root /home/www/example.org;

    include /etc/nginx/ssl.conf;

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }

    location = /known {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    location = /also-known {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }
    location = /also-also-known {
            # rewrite foobar.html to foobar, foobar.php to foobar
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
    }

    error_page   500 502 503 504  /50x.html;
    error_page   404  /404.html;

    location = /50x {
         root   /home/www/example.org;
         internal;
    }
    location = /404 {
         root   /home/www/example.org;
         internal;
    }

    access_log  /var/log/nginx/example.org.access.log;
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.