【问题标题】:nginx http server location include unknown directive errornginx http服务器位置包括未知指令错误
【发布时间】:2019-05-17 10:43:41
【问题描述】:

我的 nginx.conf

worker_processes 1;
events {
    worker_connections 1024;
}

http {
    upstream app_servers {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
    }

    server {
        listen 6200;
        server_name test;
        add_header X-GG-Cache-Status $upstream_cache_status;
        include rewrite.conf;
    }
}

和我的 rewrite.conf 在同一个文件夹中

location = / {
    rewrite ^/some-custom-destination/?$ /destination/detail?id=33;
    proxy_pass http: //app_servers;
    proxy_intercept_errors on;
    error_page 400 404 /;
    error_page 500 502 503 504 /error.html;
    location = /error.html {
        root /etc/nginx/;
    }
}

当我使用 nginx -s reload 命令时出现该错误:nginx: [emerg] unknown directive "location" in /etc/nginx/rewrite.conf:1

我该如何解决这个问题?

请帮忙。谢谢。

【问题讨论】:

  • 我不知道你是如何得到这个特定错误的。当我测试您的配置时,proxy_pass 由于虚假空间而失败,第二个 location 失败,因为它嵌套在完全匹配的 location 块中。
  • 由于复制粘贴,空格无关紧要。我只是想像这样将我的配置拆分为位置。如何拆分我的配置?
  • 请在重新加载(或尝试)之前使用 nginx -t...
  • 您不能嵌套在 = location 块中。它甚至在数学上是正确的,因为 1=1 检查但 1=3 并没有完全注册。将您的“完全匹配”请求分开,并让您的 = / just / 如果它不需要绝对是 domain.com - location / 也将匹配 domain.com/blah/ 下的任何内容(如果未在其他地方指定)

标签: nginx configuration config


【解决方案1】:

除了您的 proxy_pass 指令中的空格之外,您的位置块还有一个问题。

从有关 nginx 位置指令 (http://nginx.org/en/docs/http/ngx_http_core_module.html#location) 的 nginx 文档中,您不能在位置块“location = /”内拥有嵌套位置。

“Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.”

【讨论】:

    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2021-02-07
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多