leevis.com icon indicating copy to clipboard operation
leevis.com copied to clipboard

记录nginx使用需要特殊注意的指令

Open vislee opened this issue 4 years ago • 0 comments

概述

nginx是一个功能强大的软件,在使用的时候有一些配置的结果和你历史的经验不一致、和你理解的预期不一致。 稍不注意就会在使用过程成中出现意外的惊喜。

需要注意的指令

  • 请求头允许使用下划线(underscores_in_headers)

多个server块监听同一个IP,default server没有配置underscores_in_headers on;,跳转的server配置了underscores_in_headers on;。 那么,在请求中,优先Host 传递的带下划线的请求头都被忽略了。

具体:https://trac.nginx.org/nginx/ticket/578#no3

  • 内部location(internal)

    server {
        listen       127.0.0.1:8080;
        server_name  localhost;

        location /abc {
            proxy_pass http://127.0.0.1:8081;
        }

        location /abcd {
            internal;
            proxy_pass http://127.0.0.1:8082;
        }
    }

上面的配置,直接请求/abcd这个URL会返回404,而不是被反向代理到8081端口。

  • 回源长链接(keepalive)

nginx有的指令在配置上是没有先后顺序的,但是 keepalive 和 负载均衡算法(例如: ip_hash ) 是有先后次序的,nginx文档中也有说明:

When using load balancing methods other than the default round-robin method, it is necessary to activate them before the keepalive directive.

vislee avatar Mar 05 '21 09:03 vislee