我正在尝试为我的nginx网络服务器添加速率限制支持,但我不断收到以下错误:
webserver_1 | 2017/10/24 11:27:40 [emerg] 6#6: invalid number of arguments in "limit_req_zone" directive in /etc/nginx/nginx.conf:7
webserver_1 | nginx: [emerg] invalid number of arguments in "limit_req_zone" directive in /etc/nginx/nginx.conf:7
isaserver_webserver_1 exited with code 1
这是我的nginx配置文件:
# normally you leave this at the default of 1024
events {
worker_connections 1024;
}
http {
limit_req_zone $binary_remote_addr zone=slow:10m rate=30r/s;
# cf http://blog.maxcdn.com/accept-encoding-its-vary-important/
gzip_vary on;
gzip_proxied any;
gzip_types *;
# http://nginx.org/en/docs/http/configuring_https_servers.html#optimization
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
server_tokens off;
upstream django {
server web:8000;
}
server {
# rewrite all HTTP to HTTPS
listen 80;
server_name ${NGINX_SERVER_NAME};
return 301 https://${NGINX_SERVER_NAME}$request_uri;
}
server {
listen 443 ssl default_server;
server_name ${NGINX_SERVER_NAME};
# see http://nginx.org/en/docs/http/configuriNGINX_https_servers.html
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/private/server.key;
ssl_client_certificate /etc/ssl/certs/ca.crt;
ssl_verify_client optional;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # ie defaults minus SSLv3
#Prevent serving of sysfiles / vim backup files
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ ~$ { access_log off; log_not_found off; deny all; }
location / {
limit_req zone=slow nodelay;
uwsgi_pass django;
include uwsgi_params;
}
}
}
我不确定为什么我会收到此问题,因为我直接从http://nginx.org/en/docs/http/ngx_http_limit_req_module.html的官方文档中复制了limit_req_zone代码
我做错了什么?
更新#1:
下面是我的docker文件,然后是nginx web服务器:
FROM nginx:1.13.5
# Add start script
ADD ./webserver/config/start.sh /
RUN chmod +x start.sh
ENV NGINX_CRT_NAME=localhost NGINX_KEY_NAME=localhost NGINX_SERVER_NAME=localhost
# Add nginx config file
ADD ./webserver/config/nginx.tmpl /
# Add SSL certs to location specified in nginx.conf
ADD ./webserver/config/*.crt /etc/ssl/certs/
ADD ./webserver/config/*.key /etc/ssl/private/
# Execute start script
CMD ["./start.sh"]
以下是docker撰写文件:
version: '2.1'
services:
web:
image: xxxxxxxx/xxxxxxxxx
build: .
ports:
- "8000:8000"
extra_hosts:
- "DB_HOST:192.168.1.xxxxx"
webserver:
build:
context: .
dockerfile: webserver/Dockerfile # This is the above dockerfile
ports:
- "80:80"
- "443:443"
links:
- web:web
如果我删除了两行速率限制,则服务器运行且没有问题。但是,一旦我尝试启用速率限制,我就会收到上述错误。
更新#2:
以下是启动脚本:
#!/bin/bash
envsubst < nginx.conf > /etc/nginx/nginx.conf
nginx -g "daemon off;"
*注意我已经将nginx模板配置文件更改为.conf结尾
答案 0 :(得分:0)
您需要转义$binary_remote_addr
,因为您正在使用一些将变量转换为空的模板语言。
也许就是这样:
limit_req_zone $$binary_remote_addr zone=slow:10m rate=30r/s;
# with this ---^
答案 1 :(得分:0)
我遇到了同样的问题(尽管没有在Docker上运行),解决方案是用反斜杠(即\ $
)来转义$。limit_req_zone \$binary_remote_addr zone=slow:10m rate=30r/s;