我正在使用NGINX将带前缀proxy_pass的所有请求D0到localhost:3000的nodejs服务器。
我有一个配置文件/etc/nginx/sites-enabled/default3.conf:
server {
location /auth/ {
rewrite /auth/(.+) /$1 break;
proxy_pass http://127.0.0.1:3000;
proxy_redirect off;
proxy_set_header HOST $host;
}
}我的大多数请求(而不是GET http://localhost:3000/logout,GET http://localhost/auth/logout将按预期工作)都能正常工作,但这个请求GET http://localhost/auth/docs除外,它应该映射到http://localhost:3000/docs,但我得到了重定向:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 15 Dec 2021 01:59:02 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 175
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: nosniff
Location: /docs/然后没有找到404,而是:
HTTP/1.1 404 Not Found
Server: nginx/1.18.0 (Ubuntu)
Date: Wed, 15 Dec 2021 03:20:26 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip当我得到http://localhost/auth/docs时,请求会击中我的nodejs服务器,但随后又被重定向回http://localhost/docs,但这怎么可能呢?我仍然可以在没有问题的情况下得到http://localhost:3000/docs。
更新的
http://localhost:3000/docs是我使用npm模块swagger-ui-express为服务器API提供Swagger的地方。
发布于 2021-12-15 07:47:43
swagger-ui-express。/docs上服务,模块将进行301个重定向,以使请求完全符合$HOST/docs,因为主机是请求主机(localhost:3000,或localhost)。http://localhost/auth/docs都会击中我的nodejs服务器,但会像我在项目2中所说的那样被重定向到http://localhost/docs。/docs/。 location /docs/ {
proxy_pass http://127.0.0.1:3000/docs/;
}swagger-ui-express期望尾随斜杠。否则,/auth/docs将被重定向到/docs/,/docs/将再次被重定向到/auth/docs (无限循环,直到出错)。尽管在位置匹配和代理传递URL中删除尾随斜杠是可以的。https://serverfault.com/questions/1086358
复制相似问题