是否可以在 nginx.conf 文件中将传入的 POST 请求限制为仅仅几页?
假设我只想允许在 signup.php 和 login.php 上发布请求,但不允许在所有其他可能的页面上发布请求。
我询问的原因是我的服务器经常被随机页面上的 POST 请求淹没,这造成了不必要的负载。
我尝试在 PHP 中阻止它,但在这种情况下,nginx 仍然必须处理并接受完整的 POST 数据。我正试图阻止这种情况。
答案1
您需要创建多个位置来处理不同的情况:
# including your fastcgi params at a higher level
# allows them to be inherited in both locations
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
# regex locations are matched in order, so this will handle
# /anything/signup.php and /anything/login.php
# If you want to just have /signup.php and /login.php,
# add ^ before the / or you could split this into two more:
# location = /signup.php abd location = /login.php
location ~ /(signup|login)\.php$ {
fastcgi_pass php;
}
# this will handle the rest of the php requests
location ~ \.php$ {
# Only allow GET and HEAD requests for all other php scripts
limit_except GET HEAD {
allow 1.1.1.1/32; #trusted ip
deny all;
}
fastcgi_pass php;
}