question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[6.0.1]Problem with deployment on a subroute proxied by nginx

See original GitHub issue

Is 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:

CleanShot 2022-07-09 at 08 05 51@2x

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

  1. yarn
  2. yarn start:dev
  3. May need to config .nginx/conf/3100.conf
  4. docker-compose up -d
  5. 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:closed
  • Created a year ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
Panzer1119commented, Jul 11, 2022

If you enable the app.setGlobalPrefix('api'); in your minimum reproduction code and use proxy_pass http://YOUR_IP:3000/api/; for nginx instead of proxy_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 path IP:PORT/api/.

1reaction
Panzer1119commented, Jul 11, 2022

An absolute path would get appended after the hostname e.g. /api/docs would make http://127.0.0.1:3333/api/docs, but ./api/docs would make http://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/docs page and use a relative path there like ./api/swagger-ui.css than it becomes http://127.0.0.1:3333/api/api/swagger-ui.css

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoiding the Top 10 NGINX Configuration Mistakes
We help you avoid the 10 most common NGINX configuration errors, explaining the problems caused by each and how to fix them.
Read more >
Deployment guidance of Hosted Blazor WASM under a sub-path
I have a hosted blazor wasm app, and I want to deploy it under a sub-path like https://xxx.mydomain.com/blazor , with nginx.
Read more >
NGINX settings - GitLab Docs
Once enabled, NGINX only accepts PROXY protocol traffic on these listeners. Ensure to also adjust any other environments you might have, like monitoring...
Read more >
How to deploy Angular and Spring Boot apps behind Nginx ...
How to deploy Angular and Spring Boot apps behind Nginx proxy server. NGINX is a free, open-source, high-performance HTTP server and reverses proxy,...
Read more >
Host ASP.NET Core on Linux with Nginx - Microsoft Learn
A reverse proxy server may reside on a dedicated machine or may be deployed alongside an HTTP server. For the purposes of this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found