Swagger-ui: Swagger redirects to port 8080 when using BASE_URL

Created on 28 Mar 2018  路  8Comments  路  Source: swagger-api/swagger-ui

| Q | A
| ------------------------------- | -------
| Bug or feature request? | Bug
| Which Swagger/OpenAPI version? | 3
| Which Swagger-UI version? | 3.13.1/latest
| How did you install Swagger-UI? | With the docker image, behind an nginx reverse proxy
| Which browser & version? | Chrome, Safari...
| Which operating system? | MacOS X

Configuration (browser query string, constructor, config.yaml)

Docker-compose.yml

version: '3.5'

services:
  gateway:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    links:
      - swaggerui

  swaggerui:
    image: swaggerapi/swagger-ui
    restart: always
    environment:
      - BASE_URL=/swagger

nginx.conf

# Documentation server
server {
    listen 443 ;

    include /etc/nginx/proxy.conf;
    include /etc/nginx/ssl.conf;

    location ^~ /swagger {
        proxy_pass              http://swaggerui:8080;
    }
}
curl -v http://doc.dev/swagger

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to doc.dev (127.0.0.1) port 80 (#0)
* Server auth using Basic with user 'test'
> GET /swagger HTTP/1.1
> Host: doc.dev
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Wed, 28 Mar 2018 10:50:30 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: http://doc.dev:8080/swagger/
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: GET, POST, OPTIONS
< Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>
* Connection #0 to host doc.dev left intact

BUT when I add a final slash

curl -v http://doc.dev/swagger/
# I get the swagger app

Expected Behavior

It should work for http://doc.dev/swagger and http://doc.dev/swagger/ (with final slash) as well

Current Behavior

When not adding a final slash, we are redirected to 8080

Context

I have to explain this strange behavior to my teammates and to take care of the links I send.

docker support

Most helpful comment

@danielpsf please don't kill yourself over software, it's not worth it.

All 8 comments

@maximelebastard, I think this is an issue with your proxy setup - it should handle rewriting Swagger UI's URLs based on what your user is seeing.

Per this Server Fault answer, can you try changing your proxy_pass directive to:

        proxy_pass              http://swaggerui:8080/;

Same issue. Without docker.

nginx config like:

location / {
proxy_pass http://example.com:80;
}

If i request https://example.com/swagger i get redirect to https://example.com:80/ui/swagger/ui/index

Please fix issue that was reported almost one year ago.

I also would like to see this issued fixed.

Almost killed myself because I thought it was my API Gateway that was doing this.

@danielpsf please don't kill yourself over software, it's not worth it.

Add port_in_redirect off; to the location section. This will remove the port in the redirect.

As workaround for docker-compose you can use command to replace it and start nginx:

version: '3.5'

services:
  gateway:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    links:
      - swaggerui

  swaggerui:
    image: swaggerapi/swagger-ui
    restart: always
    environment:
      - BASE_URL=/swagger
    command: ['/bin/sh', '-c', 'sed -i "s|location \/ {|location \/ {\nport_in_redirect off;\n|g" /etc/nginx/nginx.conf && /usr/share/nginx/run.sh']

Add port_in_redirect off; to the location section. This will remove the port in the redirect.

As workaround for docker-compose you can use command to replace it and start nginx:

version: '3.5'

services:
  gateway:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    links:
      - swaggerui

  swaggerui:
    image: swaggerapi/swagger-ui
    restart: always
    environment:
      - BASE_URL=/swagger
    command: ['/bin/sh'. '-c', 'sed -i "s|location \/ {|location \/ {\nport_in_redirect off;\n|g" /etc/nginx/nginx.conf && /usr/share/nginx/run.sh']

not (.) -> (,) between sh and -c
command: ['/bin/sh', '-c', 'sed -i "s|location \/ {|location \/ {\nport_in_redirect off;\n|g" /etc/nginx/nginx.conf && /usr/share/nginx/run.sh']

I'm having the same issue and not using docker, just straight node locally. Why is it doing a redirect to /docs/ from /docs? Just load the content? Also this breaks things in a Lambda environment as the deployment stage is required after the baseUrl (e.g. https://host.com/dev/docs, it redirects to https://host.com/docs/)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrecarlucci picture andrecarlucci  路  3Comments

insideshell picture insideshell  路  3Comments

ankon picture ankon  路  4Comments

songtianyi picture songtianyi  路  3Comments

fehguy picture fehguy  路  3Comments