我怎样才能为一组位置configuration共享configuration块?
location / { proxy_pass http://127.0.0.1:9000/; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache cache-test; proxy_cache_valid 200 302 24h; proxy_cache_valid 404 60s; add_header X-Cache-Status $upstream_cache_status; } location /api/0.1/user{ proxy_cache_key /user/$http_authorization; }
现在,如果我尝试访问/api/0.1/user,那么我将得到404,因为它不会将请求传递给127.0.0.1:9000
创build一个通用的代理configuration并按需包含。
/etc/nginx/api_proxy.conf
proxy_pass http://127.0.0.1:9000/; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache cache-test; proxy_cache_valid 200 302 24h; proxy_cache_valid 404 60s; add_header X-Cache-Status $upstream_cache_status;
您的主机configuration文件
... location /api/0.1/user { include /etc/nginx/api_proxy.conf; proxy_cache_key /user/$http_authorization; } ...
大部分proxy_ *configurationvariables在服务器上下文中也是允许的,所以你可以把它们移动到多个位置共享相同的设置。
但是,proxy_pass只能在位置内使用。 所以你应该在每个位置至less有这个指令,可选地覆盖一些额外的proxy_ * vars的值。