一个位置内的 nginx 行顺序是否重要?

Does nginx line ordering within a location matters?

具体我有以下位置:

location / {
    root ...;
    try_files $uri $uri/ /index.html =404;
    ....
    add_header ....
    server_tokens off;
    # Does the order of the next 2 lines matter?
    include uwsgi_params;
    uwsgi_pass django;
  }

我想知道其中行的顺序是否重要。特别是关于最后两行。

我找不到任何文档来回答“位置块中指令的顺序重要吗?”这个问题。但是,通过研究Nginx uwsgi module document中的例子,如下所示:

location / {
    include    uwsgi_params;
    uwsgi_pass localhost:9000;
}

location / {
    uwsgi_pass        backend;
    uwsgi_cache       cache_zone;
    uwsgi_cache_key   $uri;
    uwsgi_cache_purge $purge_method;
}

location /fetch/ {
    internal;

    uwsgi_pass         backend:9000;
    ...

    uwsgi_store        on;
    uwsgi_store_access user:rw group:rw all:r;
    uwsgi_temp_path    /data/temp;

    alias              /data/www/;
}

我猜 uwsgi_pass 指令和其他 uwsgi_param 指令在 uwsgi_params 文件中的顺序并不重要。如您所见,uwsgi_pass 指令可以位于块的开头、中间或结尾。

另外,在解释how to use NGINX as an application gateway with uWSGI and Django的文章中,uwsgi_pass指令也可以位于uwsgi_param指令之间,如下:

location /main {
    include /etc/nginx/uwsgi_params;
    uwsgi_pass django;
    uwsgi_param Host $host;
    uwsgi_param X-Real-IP $remote_addr;
    uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
    uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
}

根据我在使用 FastCGI 设置 PHP 网站时的经验,指令的顺序无关紧要。但是,如果您在一个块中多次声明同一指令,最后声明的指令将生效