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

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

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

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by lia , 7 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.

by aglae_feil , 2 months ago

@coty_beier 

Here is an example of how you can block URLs containing the word 'admin' in Nginx:


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". Add the following lines inside the server block:

1
2
3
4
5
6
7
server {
    ...
    location ~* /admin {
        return 403;
    }
    ...
}


Save the configuration file and exit the editor. Test the Nginx configuration to ensure there are no syntax errors by running the following command:

1
sudo nginx -t


If the configuration test is successful, reload Nginx to apply the changes:

1
sudo systemctl reload nginx


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