Creating 403 page for deny url

Author: Al-mamun Sarkar Date: 2020-04-14 13:53:01

Creating a 403 page for deny URL. The following code show how to add a 403 page for denied URL.

 

Edit Configuration file:

vim /etc/nginx/conf.d/artofcse.local.conf

 

Create a location:

location /deny/ {
       deny all;
}

Add 403 page:

error_page 403 /403.html;
location = /403.html {
      internal;
}

 

Now artofcse.local.conf File will be as follows:

server {
        listen 80 default_server;
        server_name artofcse.local www.artofcse.local;
        index index.html index.htm index.php;
        root /var/www/artofcse.local;

        access_log /var/log/nginx/artofcse.local.access.log;
        error_log /var/log/nginx/artofcse.local.error.log;

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

        location /images {
                autoindex on;
                access_log /var/log/nginx/artofcse.local.images.access.log;
                error_log /var/log/nginx/artofcse.local.images.error.log;
        }

        location /appointments/ {
                allow 192.168.33.0/24;
                allow 10.0.0.0/8;
                deny all;
        }

        location /deny/ {
                deny all;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                fastcgi_intercept_errors on;
        }

        error_page 403 /403.html;
        location = /403.html {
                internal;
        }

        error_page 404 /404.html;
        location = /404.html {
                internal;
        }
}

 

Test Configuration:

nginx -t

Restart Server:

systemctl reload nginx

 

Create a 403.html page:

echo "403 Access denied" > /var/www/artofcse.com/403.html