在 nginx 中的位置块之间共享指令

Share directives between location blocks in nginx

我有一个 nginx 位置块应该委托给 uwsgi 后端 提供 http --> https 重定向,所以它是这样的:

location  ~ ^/(api/v1) {
    if ($http_x_forwarded_proto != 'https') {
        rewrite ^ https://$host$request_uri? permanent;
    }
     access_log my-access.log;
     error_log my-error.log;
     include uwsgi_params;
     uwsgi_read_timeout 300;
     uwsgi_send_timeout 300;
     uwsgi_param ..;
     uwsgi_param ..;
     etc ...
    }

我想要的是针对特定端点 而不是 提供 https 重定向,因此我不得不执行以下操作:

location  = /api/v1/my/more/specific/endpoint {
     access_log my-access.log;
     error_log my-error.log;
     include uwsgi_params;
     uwsgi_read_timeout 300;
     uwsgi_send_timeout 300;
     uwsgi_param ..;
     uwsgi_param ..;
     etc ...
    }

}

有没有办法 nginx 可以避免上述 uwsgi 参数定义的重复?

2 个选项:

  1. 将具有 server 上下文且必须影响 location 块的所有位置的参数移动到 server
  2. 使用include共享通用代码以避免复制和粘贴

Includes another file, or files matching the specified mask, into configuration. Included files should consist of syntactically correct directives and blocks.