Nginx URL security policy writing guide
As a high-performance web server and reverse proxy server, Nginx is widely favored by website architects. But when using Nginx, we also need to pay attention to security issues, especially when processing URLs.
Due to the flexibility of Nginx, if we do not adopt some URL security strategies, we may be subject to the following attacks:
- SQL injection
- XSS attack
- Illegal file download
- CSRF attack
- Illegal request for access, etc.
This article will introduce the guide for writing Nginx URL security policy.
1. Preconditions
Before writing Nginx URL security policy, you need to master the following knowledge points:
- Regular expression
- Nginx configuration file syntax
- Basic knowledge of HTTP protocol
2. Input filtering
Nginx can use http request header detection to prevent malicious HTTP requests. The specific implementation method is to add a configuration similar to the following to the Nginx configuration file:
if ($http_user_agent ~* "some evil expression") { return 403; }
Or use Nginx’s built-in firewall module for input filtering, as follows:
# block ip sends more than 100 requests per 5 seconds limit_conn_zone $binary_remote_addr zone=one:10m; limit_req_zone $binary_remote_addr zone=two:10m rate=1r/s; server { location / { limit_conn one 10; limit_req zone=two burst=5 nodelay; } }
This example does the following:
- First define two zones, which are memory areas that can store status information. (This also means that if there are many accesses, the cost of this protection may be relatively high)
- If the same IP address sends more than 100 HTTP requests within 5 seconds, then block.
- If the same IP address sends more than 5 HTTP requests within 1 second, it will be blocked.
3. Prevent SQL injection
In actual development, it is necessary to avoid SQL injection. In order to prevent SQL injection attacks, we can configure it as follows:
location ~* (.php|.asp|.ashx)/?$ { if ($args ~* "select.*from") { return 403; } }
This example uses Nginx’s built-in if module to prevent attackers from using select statements to obtain data from the database. If this happens, return 403 Access Forbidden .
4. Prevent XSS attacks
For XSS attacks, we can strengthen the input detection. If a possible XSS attack is detected, the connection can be redirected to a safe URL or an error message can be returned.
if ($args ~* "<script.*>") { return 403; }
This example uses Nginx's built-in if module to detect whether there is content with script tags nested in the URL.
5. Prevent CSRF attacks
When using Nginx, in order to prevent CSRF attacks, you need to prohibit requests from external sites. For example, you can add the following configuration:
location / { if ($http_referer !~ "^https?://$host/") { return 403; } }
This example uses Nginx's built-in if module to limit it to only receiving requests from the $host site. If there are requests from other sites, Nginx will return 403.
6. Prevent file download vulnerabilities
In order to prevent access to improper files, such as private documents, scripts, configuration files, etc., please use the following strategy:
location ~* .(xls|doc|pdf)$ { valid_referers none blocked server_names; if ($invalid_referer) { return 401; } }
This example Using Nginx's built-in valid_referers module, when a request is found to come from an unauthorized site, 401 will be returned.
7. Prohibit access to some URLs
In actual projects, some URLs can be used by attackers, such as admin.php, login.php, etc. We can simply ban their access.
location ~ /(admin|login).php { deny all; }
The configuration of this example prohibits access to URLs ending with admin.php and login.php.
8. Complete example
Finally, based on the above configuration, we can get the following complete example:
server { listen 80; server_name yourdomain.com; # 设置过滤规则 location / { # 禁止非法请求 limit_conn_zone $binary_remote_addr zone=one:10m; limit_req_zone $binary_remote_addr zone=two:10m rate=1r/s; limit_conn one 10; limit_req zone=two burst=5 nodelay; # 防止XSS攻击 if ($args ~* "<script.*>") { return 403; } # 防止SQL注入 if ($args ~* "select.*from") { return 403; } # 禁止admin和login的访问 location ~ /(admin|login).php { deny all; } } # 防止文件下载漏洞 location ~* .(xls|doc|pdf)$ { valid_referers none blocked server_names; if ($invalid_referer) { return 401; } } }
The above is the guide for writing Nginx URL security policy. I hope it can provide some help for your Nginx configuration and improve the security of the system.
The above is the detailed content of Nginx URL security policy writing guide. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.
