我在 nginx 上运行了这个配置
root@homer:/home/admin# cat /etc/nginx/sites-enabled/example.com
server
{
listen 80;
server_name www.example.com example.com;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;
## Only allow these request methods (do not accept DELETE, SEARCH and others.. ##
if ($request_method !~ ^(GET|HEAD|POST)$ ) {return 444; }
location /
{
root /var/www/example.com/public_html;
index index.html index.htm index.php;
}
location ~ \.php$
{
include /etc/nginx/fastcgi_params;
if ($uri !~ "^/upload/") {fastcgi_pass 127.0.0.1:9000; }
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public_html$fastcgi_script_name;
}
# Prevent (deny) Access to Hidden Files
location ~ /\.
{
access_log off;
log_not_found off;
deny all;
}
# Limit bandwidth rate on a specific folder
location /download/
{
set $limit_rate 150k;
}
}
我的目标是限制有人从“/download/”目录请求某些内容时的带宽速率。使用此配置,当我打开浏览器并输入“example.com/download/filename.example”之类的内容时,我会收到 404 错误。如果我从配置文件中删除“location /download/”块,一切就都好了。
有人能帮我修复它吗?
我使用 nginx/0.7.67 和 PHP 5.3.18-1~dotdeb.0 以及 Suhosin-Patch (cli)
答案1
您需要root
为 /download/ location-block 设置指令。
这可以通过在位置块内明确设置根指令,或将全局根指令放在位置块之外、服务器块之内来实现。
替代方案 1 - location-block 内的显式 root 指令
location /download/ {
root /var/www/example.com/public_html;
set $limit_rate 150k;
}
替代方案 2 - 服务器块的全局根指令
server {
root /var/www/example.com/public_html;
...
location / {
index index.html index.htm index.php;
}
...
location /download/ {
set $limit_rate 150k;
}
}