我有以下 API:
- 本地主机:300/api/customers/
- localhost:400/api/customers/:id/billing
- 本地主机:500/api/orders
我想使用 NGINX 让它们全部在以下位置运行:
本地主机:443/api/
这似乎非常困难,因为客户跨越两台服务器。
这是我从订单开始的失败尝试
server {
listen 443;
server_name localhost;
location /api/orders {
proxy_pass https://localhost:500/api/orders;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 443;
server_name localhost;
location /api/customers/$id/billing {
proxy_pass https://localhost:400/api/customers/$id/billing;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 443;
server_name localhost;
location /api/customers {
proxy_pass https://localhost:300/api/customers;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
有什么问题需要修复吗?谢谢!
最佳答案
这三个服务由同一服务器代理(就 nginx
而言),因此必须在一个 server< 内将其构造为三个
block 。请参阅this document了解详情。location
block
如果您只是传递未经修改的原始 URI,则无需在 proxy_pass
语句中指定 URI。
server {
{
listen 443;
server_name localhost;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location /api/orders {
proxy_pass https://localhost:500;
}
location /api/customers {
proxy_pass https://localhost:400;
}
location = /api/customers {
proxy_pass https://localhost:300;
}
}
如果proxy_set_header
语句相同,则可以在父 block 中指定一次。
所需的 location
语句类型取决于 localhost:300/api/customers/
服务处理的 URI 范围。如果它是一个 URI,则 =
语法将起作用。如果它是与 /api/customers/:id/billing
不匹配的任何 URI,那么您将需要使用正则表达式位置 block 。请参阅this document了解详情。
除非您在此处终止 SSL,否则我不确定这是否有效。即配置reverse proxy as a secure server .
关于api - NGINX - 不同端口上的反向代理多个 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769963/