'NGINX reverse proxy and swagger-ui.html
I have been trying to get swagger to work behind a NGINX reverse proxy, but I just cannot get it right. My services are running in docker containers and I would like to access them with these sample urls:
My service:
http://service-host.example.com/my-service1
Swagger:
http://service-host.example.com/my-service1/api
My nested location blocks look like this:
location /my-service1 {
resolver 127.0.0.11 valid=30s;
set $upstream my-host.example.com;
proxy_pass http://$upstream/home;
location ~ /api {
resolver 127.0.0.11 valid=30s;
set $upstream my-host.example.com;
proxy_pass http://$upstream/swagger-ui.html;
}
location ~ /webjars {
resolver 127.0.0.11 valid=30s;
set $upstream my-host.example.com;
proxy_pass http://$upstream/;
}
location ~ /v2 {
resolver 127.0.0.11 valid=30s;
set $upstream my-host.example.com;
proxy_pass http://$upstream/;
}
location ~ /swagger-resources {
resolver 127.0.0.11 valid=30s;
set $upstream my-host.example.com;
proxy_pass http://$upstream/;
}
}
My NGINX log looks something like this:
GET /my-service1/api/ HTTP/2.0" 200
GET /my-service1/api/webjars/springfox-swagger-ui/springfox.css?v=2.9.2 HTTP/2.0" 200
GET /my-service1/api/webjars/springfox-swagger-ui/swagger-ui-bundle.js?v=2.9.2 HTTP/2.0" 200
GET /my-service1/api/webjars/springfox-swagger-ui/swagger-ui.css?v=2.9.2 HTTP/2.0" 200
GET /my-service1/api/webjars/springfox-swagger-ui/springfox.js?v=2.9.2 HTTP/2.0" 200
GET /my-service1/api/webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js?v=2.9.2 HTTP/2.0" 200
Testing this directly, without going via NGINX, the process proceeds further, with GETs to /v2/api and /swagger-resources. Using the reverse proxy gets stuck just before that.
Any suggestions on how to resolve this?
EDIT: Here is a NGINX log when not trying to reverse proxy, but going directly to http://myservice.example.com/swagger-ui.html where it works perfectly!
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:44 +0000] "GET /swagger-ui.html HTTP/2.0" 200 3318 "-" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:44 +0000] "GET /webjars/springfox-swagger-ui/springfox.css?v=2.9.2 HTTP/2.0" 200 2894 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:44 +0000] "GET /webjars/springfox-swagger-ui/swagger-ui.css?v=2.9.2 HTTP/2.0" 200 154488 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:45 +0000] "GET /webjars/springfox-swagger-ui/swagger-ui-bundle.js?v=2.9.2 HTTP/2.0" 200 1448844 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:45 +0000] "GET /webjars/springfox-swagger-ui/springfox.js?v=2.9.2 HTTP/2.0" 200 96207 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:45 +0000] "GET /webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js?v=2.9.2 HTTP/2.0" 200 440850 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:46 +0000] "GET /swagger-resources/configuration/ui HTTP/2.0" 200 450 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:46 +0000] "GET /swagger-resources/configuration/security HTTP/2.0" 200 2 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:46 +0000] "GET /swagger-resources HTTP/2.0" 200 90 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
myservice.example.com 192.168.1.30 - - [12/Feb/2019:09:59:46 +0000] "GET /v2/api-docs HTTP/2.0" 200 8387 "https://myservice.example.com/swagger-ui.html" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
Solution 1:[1]
swagger working in my server (NET CORE) with this conf
location / {
#root /srv/ftp;
#index index.html index.htm;
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Viacheslav |
