大网数据广告活动
服务器运维问答

NGINX reverse proxy doesn't work with swagger-ui-express

· 52CE 我爱测

I am using NGINX to proxy_pass all requests with prefix /auth/ to a nodejs api server at localhost:3000 . I have this single config file /etc/nginx/sites-enabled/default3.conf : server { location /auth/ { rewrite /auth/(.+) /$1 b…

问题原文

原帖链接 · xceeded · CC BY-SA 4.0

I am using NGINX to proxy_pass all requests with prefix /auth/ to a nodejs api server at localhost:3000.

I have this single config file /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;
    }


}

It works fine for most of my requests (instead of GET http://localhost:3000/logout, GET http://localhost/auth/logout would work as expected), except this request GET http://localhost/auth/docs, which should map to http://localhost:3000/docs but I got a redirect:

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/

then a 404 NOT FOUND instead:

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

It seems like when I GET http://localhost/auth/docs, the request hit my nodejs server, but then got redirected back to http://localhost/docs, but how could this be possible? I still can GET http://localhost:3000/docs without a problem.

UPDATED

http://localhost:3000/docs is where I am serving Swagger UI for the server API, using the npm module swagger-ui-express.

已采纳解答

原帖链接 · xceeded · CC BY-SA 4.0

The problem

  • The problem lies in the module swagger-ui-express which my nodejs server is using to serve API docs.
  • I configured it to serve on /docs and the module will make 301 redirects to make the request exactly $HOST/docs, for HOST is whatever the request host is (localhost:3000, or localhost).
  • So, when put behind NGINX reverse proxy like I did, every GET http://localhost/auth/docs would hit my nodejs server but will be redirected to http://localhost/docs like I said in bullet point 2.

The solution

  • Configure NGINX to make another proxy_pass when the request match /docs/.
    location /docs/ {
        proxy_pass http://127.0.0.1:3000/docs/;
    }
  • Be careful that the URI being passed to has a trailing slash, because the module swagger-ui-express expects that trailing slash. Otherwise /auth/docs will be redirected to /docs/ and /docs/ will be redirected to /auth/docs again (infinite loop until error). Although it is okay to remove the trailing slash in both the location matching and the proxy pass URL.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-12-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。