How to block any url contain word 'admin' in nginx?

by coty_beier , in category: General Help , 3 months ago

How to block any url contain word 'admin' in nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lia , 3 months ago

@coty_beier 

To block any URL containing the word 'admin' in Nginx, you can use a regular expression in the location block of your Nginx configuration file. Here is an example:

  1. Open your Nginx configuration file. The location of the file may vary depending on your server setup. Common locations are "/etc/nginx/nginx.conf" or "/etc/nginx/sites-available/default".
  2. Add the following lines inside the server block:
1
2
3
location ~* /admin {
    deny all;
}


  1. Save the configuration file and exit the editor.
  2. Test the Nginx configuration to ensure there are no syntax errors by running the following command:
1
sudo nginx -t


  1. If the configuration test is successful, reload Nginx to apply the changes:
1
sudo systemctl reload nginx


With the above configuration, any URL containing the word 'admin' will be blocked by Nginx, returning a 403 Forbidden error to clients attempting to access those URLs.