V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JasonLaw
V2EX  ›  程序员

关于 Nginx 和 Swagger UI 的问题

  •  
  •   JasonLaw · 2021-11-03 17:41:35 +08:00 · 1624 次点击
    这是一个创建于 898 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有个 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呢?

    相关资料

    第 1 条附言  ·  2021-11-03 19:03:20 +08:00
    5 条回复    2021-11-03 19:04:30 +08:00
    litchinn
        1
    litchinn  
       2021-11-03 17:51:28 +08:00
    应该是在 swagger 配置里改,Docket.host()
    JasonLaw
        2
    JasonLaw  
    OP
       2021-11-03 18:04:58 +08:00
    @litchinn #1 使用 SWAGGER_2 时,设置 springfox.documentation.swagger.v2.host 为 a.com/appServer 就行了,但是 OAS_30 不行。
    neekeV2
        3
    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
        4
    JasonLaw  
    OP
       2021-11-03 18:37:42 +08:00
    @neekeV2 #3 设置 server.servlet.context-path 为 /appServer ,然后做相应的修改,的确是可以。不过我不想设置 server.servlet.context-path🤐。
    JasonLaw
        5
    JasonLaw  
    OP
       2021-11-03 19:04:30 +08:00   ❤️ 1
    @litchinn #1
    @neekeV2 #3
    @ila

    找到解决方案了,请见附言。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1029 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:14 · PVG 03:14 · LAX 12:14 · JFK 15:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.