我在如何使用 nginx 重写 URL 方面遇到了麻烦。我配置了一个服务器,将所有对 /api 的请求重写到不同端口上的其他应用程序,这是我的配置
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;
}
当我尝试使用以下方式发出 curl 请求时得到它成功并获取了数据。但是当我这样做时邮政其服务请求错误
curl -X POST "http://localhost/api/auth/forgotpassword" -H "accept: */*" -H "Content-Type: application/json" -d "{\"email\":\"[email protected]\"}"
以下是我从日志中得到的信息
2020/08/11 13:09:17 [debug] 15411#15411: *3 http keepalive handler
2020/08/11 13:09:17 [debug] 15411#15411: *3 malloc: 000055D139AF11D0:1024
2020/08/11 13:09:17 [debug] 15411#15411: *3 recv: eof:1, avail:1
2020/08/11 13:09:17 [debug] 15411#15411: *3 recv: fd:12 0 of 1024
2020/08/11 13:09:17 [info] 15411#15411: *3 client 127.0.0.1 closed keepalive connection
2020/08/11 13:09:17 [debug] 15411#15411: *3 close http connection: 12
2020/08/11 13:09:17 [debug] 15411#15411: *3 event timer del: 12: 50648776
2020/08/11 13:09:17 [debug] 15411#15411: *3 reusable connection: 0
2020/08/11 13:09:17 [debug] 15411#15411: *3 free: 000055D139AF11D0
2020/08/11 13:09:17 [debug] 15411#15411: *3 free: 000055D139AF07C0, unused: 136
答案1
尝试更改您的配置:
proxy_set_header Connection 'upgrade';
到:
proxy_set_header Connection $http_connection;
答案2
也许该应用程序也喜欢以“/api”开头的 URL?例如:
location /api {
proxy_pass http://localhost:5021/api;
....