【问题标题】:Nginx invalid condition "="Nginx 无效条件“=”
【发布时间】:2017-01-20 10:58:49
【问题描述】:

我正在使用 Ngnix 以下配置:

upstream sync {                                                                 
    server couchbase-sync:4984;                                                 
}                                                                               

server {                                                                        
    listen 4984;                                                                
    server_name ${WEBAPP_HOST};                                                 
    add_header 'Access-Control-Allow-Origin' '*';                               
    add_header 'Access-Control-Allow-Methods' '*';                              
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';                                    

    location / {                                                                
        if ($request_method = OPTIONS) {                                       
            return 204;                                                        
        }                                                                      
        proxy_pass http://sync;                                                 
    }                                                                           
}                                                                               

我不想重定向 OPTIONS 请求,但我有一个错误:

nginx_1 | 2017/01/20 09:23:40 [emerg] 9#9: invalid condition "=" in /etc/nginx/conf.d/webapp.conf:13 nginx_1 | nginx: [emerg] invalid condition "=" in /etc/nginx/conf.d/webapp.conf:13

如何解决我的问题? Official Doc

感谢您的阅读

【问题讨论】:

  • 此文件是否经过后处理?也许$request_method 字符串已损坏。试试nginx -T 看看nginx 看到了什么。

标签: nginx nginx-location


【解决方案1】:

在nginx 中限制http methods 的最佳实践:

limit_except GET {
    allow 192.168.1.0/24;
    deny  all;
}

上面的例子将只允许提供IP Range 的所有内容。对于其他客户,只有GET method 可用。

您可以在位置块中使用它。

可用方法:

GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, or PATCH

【讨论】:

    【解决方案2】:

    正如理查德史密斯所说,就我而言,有些角色存在一些问题。 我在 docker 容器中创建 nginx.conf 文件时遇到了这个问题。我在 bash 中使用echo 命令,然后粘贴复制的文本。

    当我使用nano(粘贴并保存)创建它时,问题消失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 2011-11-30
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多