python - 根据 NGINX 中的请求 URL 将请求转移到不同的服务器
问题描述
我想实现如下所示的目标,但找不到可能性:
server {
listen 80;
server_name localhost;
location /get-product {
if(request=GET){
proxy_pass http://get-app-server:8080/products;
proxy_set_header Host "get-app-server";
}
if(request=POST){
proxy_pass http://post-app-server:8080/products;
proxy_set_header Host "post-app-server";
}
}
}
我怎么能执行这样的决定?
解决方案
有许多可能的解决方案。例如,使用伪造的内部 URI ( /post/get-product
) 来处理以下两种情况之一:
location /get-product {
if ($request_method = POST) {
rewrite ^ /post$uri last;
}
proxy_pass http://get-app-server:8080/products;
proxy_set_header Host "get-app-server";
}
location /post/get-product {
internal;
proxy_pass http://post-app-server:8080/products;
proxy_set_header Host "post-app-server";
}
请注意该if
语句的正确语法和变量名称。
推荐阅读
- python - 如何在 Python 中找到 MemoryError 的来源?
- c - 我的第二个扫描功能不起作用,默认从我的数组中提取第二个值
- javascript - 如何使用 async/await 实现 Promise.race()
- build - 如何导入 DevOps 构建?
- node.js - 如何修复 Windows 上的“npm 不支持 Node.js v11.14.0 错误”?
- r - 在R中找到广义正态分布参数的最大似然估计的问题
- yocto - yocto 自定义收据的 bbappend 修补错误
- javascript - 如何修复气泡图中带有文本的可拖动功能?
- android - 如何使用 dart/flutter 从 API 获取数据?
- html - 不能 POST /ha 使用超过一个 FORM