我想进行 URL 重写,将所有带有 /api 的请求重定向到其他端口。我想将所有 /api 请求重定向到 http://localhost:8081/api。我尝试创建这样的重写规则,但它返回 404:
location /api {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://localhost:8081/api;
}
编辑:它尝试使用此 curl 使用评论中的建议进行更新
curl -i GET "http://localhost/api/healthcheck/check" -H "accept: */*"
回应如下:
curl: (6) Could not resolve host: GET
HTTP/1.1 502 Bad Gateway
Server: nginx/1.14.0 (Ubuntu)
Date: Mon, 10 Aug 2020 23:56:17 GMT
Content-Type: text/html
Content-Length: 182
Connection: keep-alive
这是我的整个配置
server {
access_log /var/log/nginx/access_log.log;
error_log /var/log/nginx/error_log.log;
listen 80;
server_name ui.buildanalyzer.io;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api {
proxy_pass http://localhost:8081/api;
}
}
答案1
最后让它工作起来!这是我的配置
location /api {
proxy_pass http://localhost:5021;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Proto $scheme;
}
我可以调用所有 GET 端点,但问题是我无法使所有 POST 工作。我还使用几乎相同的配置实现了显示端点文档。