关于 Nginx 和 Swagger UI 的问题

2021-11-03 17:41:35 +08:00
 JasonLaw

我有个 web 应用,使用 Nginx 进行代理,Nginx 的配置如下(已简化):

http {
   upstream appServerBackend {
      server localhost:8081;
   }

   server {
      listen 443 ssl http2;
      server_name a.com;
      
      location /appServer/ {
         proxy_pass http://appServerBackend/;
      }
   }
}

web 应用使用 Swagger 产生文档,以下是 Swagger UI 展示的 Servers ,其值为http://appServerBackend:80,导致在 Swagger 执行请求时会请求http://appServerBackend:80,而不是期望的https://a.com/appServer。怎样操作才能实现“在 Swagger 执行请求时请求https://a.com/appServer”呢?


尝试解决

我尝试过修改 Nginx 的配置,在 proxy_pass 下面加上如下代码块。

proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;

Servers 变成了https://a.com:443,还不是我期望的https://a.com/appServer,我应该怎么操作才能让 Servers 变成https://a.com/appServer呢?

相关资料

1631 次点击
所在节点    程序员
5 条回复
litchinn
2021-11-03 17:51:28 +08:00
应该是在 swagger 配置里改,Docket.host()
JasonLaw
2021-11-03 18:04:58 +08:00
@litchinn #1 使用 SWAGGER_2 时,设置 springfox.documentation.swagger.v2.host 为 a.com/appServer 就行了,但是 OAS_30 不行。
neekeV2
2021-11-03 18:08:53 +08:00
同样问题,我是这么处理的。
nginx:
server {
listen 80;
server_tokens off;
charset utf-8;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/javascript application/x-javascriptapplication/xml text/javascript application/json text/css;
location /api/ {

proxy_pass http://127.0.0.1:10000/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;
}
}
springboot:

server:
port: 10000
servlet:
context-path: /api
JasonLaw
2021-11-03 18:37:42 +08:00
@neekeV2 #3 设置 server.servlet.context-path 为 /appServer ,然后做相应的修改,的确是可以。不过我不想设置 server.servlet.context-path🤐。
JasonLaw
2021-11-03 19:04:30 +08:00
@litchinn #1
@neekeV2 #3
@ila

找到解决方案了,请见附言。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/812805

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX