Info
Content

Nginx config

Return 404 on specific extension

Return 404 trying to load .ini or .db files. Add this to /etc/nginx/sites-available/default or whatever file you are using inside server{...}.

location ~\.(db|ini)$ {
    return 404;
}

Return custom 404 page

In this case is .php file but it can also be .html, .gif and others...

error_page 404 = /404.php;

Config example

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php;
		
        server_name _;
		
        #custom 404 page
        error_page 404 = /404.php;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
		
        #block access to .db and .ini files
        location ~\.(db|ini)$ {
                return 404;
        }

        # pass PHP scripts to FastCGI server
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
}
No Comments
Back to top