TheMulittle
TheMulittle

Reputation: 25

How to correctly reverse proxy swagger with shared server

Folks,

I have a server running both front-end (angular) and back-end (spring-boot) of an application. I was able to configure nginx 1.14 to redirect calls under /api to port 9091 (localhost:9091, where the API is listening to) and let other calls to proceed the normal flow with the snippet:

   server {
                listen 80 default_server;
                server_name example_server example_server;


                location / {
                   root /opt/myapp/html;
                   try_files $uri $uri/ /index.html;
                }

                location /api {
                   proxy_pass http://localhost:9091;
                }
        }

Now I would like to let swagger accessible in the server, locally it runs fine. I tried to write the two location blocks below. In my understanding it will route the call to /swagger-ui to the starter page of swagger and all the /webjars calls made by swagger to the respective in localhost:9091.

But the page does not load. I see an error in the console of my browser: "Uncaught (in promise) TypeError: Cannot read property '1' of null springfox.js:1 ". I think I'm routing things in the wrong way, but can't figure it out.

                location /swagger-ui {
                  proxy_pass http://localhost:9091/swagger-ui.html;
                }

                location /webjars {
                   proxy_pass http://localhost:9091;
                }

I'm including swagger.version = 2.9.0 to my pom.xml:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger.version}</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger.version}</version>
    </dependency>

Anyone has suffered the same?

Best Regards

Upvotes: 1

Views: 4098

Answers (1)

Hopefully not too late: I had similar issue when tried to achieve the same. The issue is that the baseUrl is being taken by the springfox.js from the window.location.href. It does a regex match for swagger-ui.html and takes whatever is before it (second element of the array). Since your browser URL has only /swagger-ui, the regex will not match correctly, the result array be null, hence can not find the second element of it.

Check line 128 for more details: https://github.com/springfox/springfox/blob/master/springfox-swagger-ui/src/web/js/springfox.js

Upvotes: 2

Related Questions