如何为一组位置配置共享配置块?
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;
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试访问 /api/0.1/user 然后我会得到 404 因为它没有将请求传递给 127.0.0.1:9000
小智 65
创建一个通用代理配置并根据需要包含。
/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;
Run Code Online (Sandbox Code Playgroud)
您的主机配置文件
...
location /api/0.1/user {
include /etc/nginx/api_proxy.conf;
proxy_cache_key /user/$http_authorization;
}
...
Run Code Online (Sandbox Code Playgroud)
Vad*_*dar 12
大多数 proxy_* 配置变量也允许在服务器上下文中使用,因此您可以将它们向上移动以在多个位置共享相同的设置。
但是,proxy_pass 应该只在 location 内使用。所以你应该在每个位置至少有这个指令,可以选择覆盖一些额外的 proxy_* 变量的值。
归档时间: |
|
查看次数: |
40622 次 |
最近记录: |