[6.0.1]Problem with deployment on a subroute proxied by nginx
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Current behavior
I migrated my project’s @nestjs/swagger dependency from 5.2.1 to 6.0.1, everything works locally but not on a subrouting proxied by nginx.
In the minimum reproduction code, I start a nginx server by docker locally, and config as:
server {
listen 3100;
root html;
index index.html index.htm;
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_pass http://10.211.55.2:3000/;
}
It works both http://localhost:3000 and http://localhost:3100/api/ that I can get hello world.
However accessing http://localhost:3100/api/docs not working properly.
The Swagger index.html can be loaded and I can see Swagger UI on the browser tab, the static resources is not loaded correctly:
excpted /api/docs/swagger-ui.css, got /docs/swagger-ui.css.
Everything works when using fastify with @nestjs/swagger@5.2.x before, I found the SwaggerModule.setup function was changed: 5.2.1…6.0.1.
It seems that the finalPath in SwaggerModule.setup function directly replace html template in constants.ts. In that case, how can I set the base static file path to replace with <% baseUrl %>? I have tried useGlobalPrefix: true and app.setGlobalPrefix('api') but the endpoints must accessed by /api/api/. Refer to swagger-module.ts, it seems that the only way to affect <% baseUrl %> is the globalPrefix or path.
I’ve been working on this for hours without finding a solution, can u give me som advice?
Minimum reproduction code
https://github.com/CatsJuice/nest-swagger-on-nginx-path
Steps to reproduce
yarnyarn start:dev- May need to config
.nginx/conf/3100.conf docker-compose up -d- Visit http://localhost:3100/api/docs
Expected behavior
Make it possible to custom <% baseUrl %> prefix in index.html
Package version
6.0.1
NestJS version
9.0.0
Node.js version
16.13.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Issue Analytics
- State:
- Created a year ago
- Comments:14 (14 by maintainers)

Top Related StackOverflow Question
If you enable the
app.setGlobalPrefix('api');in your minimum reproduction code and useproxy_pass http://YOUR_IP:3000/api/;for nginx instead ofproxy_pass http://YOUR_IP:3000/;then everything works without your fix.So it’s basically just a misconfigured nginx, because your server doesn’t know, that you changed its path via nginx and inserted an
/api/.So either your server would need to add that part into all urls of its generated HTML pages (i.e. setting the
globalPrefix) (or make them relative, but then break other stuff) and/or nginx would need to proxy it directly on the same pathIP:PORT/api/.An absolute path would get appended after the hostname e.g.
/api/docswould makehttp://127.0.0.1:3333/api/docs, but./api/docswould makehttp://127.0.0.1:333/SOME_SUB_PATH_WHERE_YOU_ARE/api/docs.So if you’re on the
http://127.0.0.1:3333/api/docspage and use a relative path there like./api/swagger-ui.cssthan it becomeshttp://127.0.0.1:3333/api/api/swagger-ui.css