[英]Issue with Nginx rate limiting: Invalid number of arguments
我正在嘗試為我的 nginx Web 服務器添加速率限制支持,但我不斷收到以下錯誤:
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 compose 文件:
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 結尾
您需要轉義$binary_remote_addr
因為您正在使用某種模板語言將變量轉換$binary_remote_addr
。
也許像這樣:
limit_req_zone $$binary_remote_addr zone=slow:10m rate=30r/s;
# with this ---^
我有同樣的問題(雖然沒有在 Docker 上運行),解決方案是用反斜杠轉義 $,即 \\$
limit_req_zone \$binary_remote_addr zone=slow:10m rate=30r/s;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.