Can i can return 404 if file not exists?

December 2, 2019 83 views
Nginx

Hello can i return 404 or 403 if anyone try to open /?a=b AND 1=1

My vhost is:

server {
listen 80;
listen [::]:80;
server_name mysite.com www.mysite.com;
root /var/www/mysite.com/public_html;
index index.php;
rewrite ^ https://$server_name$request_uri? permanent;
}

server {
listen 443 ssl;
listen [::]:443 ssl;
root /var/www/mysite.com/public_html;
index index.php index.html index.htm index.nginx-debian.html;
server_name mysite.com www.mysite.com;
error_page 403 https://mysite.com/dead.html;

location = /dead.html {
allow all;
}

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_read_timeout 240;                    
fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\. {
deny all;
}

location ~ /\.ht {
access_log        off; 
log_not_found     off; 
deny              all; 
}
}
1 Answer

Hello,

Yes, I believe that with your current config it should work as expected as you have this part here:

location / {
    try_files $uri $uri/ =404;
}

I could suggest also taking a look at this article here on how to set up a custom 404 page with Nginx:

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-to-use-custom-error-pages-on-ubuntu-14-04

Regards,
Bobby

by Justin Ellingwood
Nginx is a high performance web server capable of serving content with flexibility and power. When designing your web pages, it is often helpful to customize every piece of content that your users will see. This includes error pages for when they request content that is not...
Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!