Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger redirects to port 8080 when using BASE_URL #4385

Open
maximelebastard opened this issue Mar 28, 2018 · 8 comments
Open

Swagger redirects to port 8080 when using BASE_URL #4385

maximelebastard opened this issue Mar 28, 2018 · 8 comments

Comments

@maximelebastard
Copy link

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.

@shockey
Copy link
Contributor

shockey commented Oct 24, 2018

@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/;

@andreyhammer
Copy link

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.

@albernhagen
Copy link

I also would like to see this issued fixed.

@danielpsf
Copy link

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

@webron
Copy link
Contributor

webron commented Oct 22, 2019

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

@TLmaK0
Copy link

TLmaK0 commented Dec 9, 2019

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']

@codesnipers
Copy link

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']

@ablankenship10
Copy link

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/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants