解决nginx访问不到swagger

项目框架:springboot1.5.15

swagger2:2.9.2

问题:

swagger2默认访问路径为:http://domain.com/swagger-ui.html

因在项目中所有接口中都加上前缀:项目名及模块名(/xxxx/xxxx),如http://domain.com/xxxx/xxxx/requestUrl,然后就...

解决方法:修改Nginx配置,新增swagger的映射,配置如下

    server {
        listen        80;
        server_name  domain.com;

        location / {
            root /home/ui;
            index index.html;
        }

        location /xxxx/xxxx {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_pass http://127.0.0.1:1901/xxxx/xxxx;
        }

       location /swagger-ui.html{
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_pass http://127.0.0.1:1901/swagger-ui.html;
        }
        location /swagger-resources {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_pass http://127.0.0.1:1901/swagger-resources;
        }
        location /v2/api-docs {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_pass http://127.0.0.1:1901/v2/api-docs;
        }
        location /webjars{
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Port $server_port;
                proxy_pass http://127.0.0.1:1901/webjars;
        }
    }

这样就可以打开swagger了(前提是程序中都配置好了swagger)

另在项目中加上swagger开关,针对不同环境(生产环境不生效)进行控制:

小记一下,不喜勿喷,谢谢

你可能感兴趣的:(springboot)