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

Doploy behind a reverse proxy #153

Closed
hectoryo opened this issue Nov 11, 2019 · 6 comments
Closed

Doploy behind a reverse proxy #153

hectoryo opened this issue Nov 11, 2019 · 6 comments

Comments

@hectoryo
Copy link

I'm moving from springfox to springdoc and I'm finding the following problem.

In the doc, you specify that the deploy urls are as follows:

http://server:port/context-path/swagger-ui.html
http://server:port/context-path/v3/api-docs 

In my case, the Spring Project is behind a reverse proxy, and I don't have access to the base url of the server, so my urls look something like:

http://server:port/custom-path/context-path/swagger-ui.html
http://server:port/custom-path/context-path/v3/api-docs 

The problem is when I attempt to enter to the swagger-ui.html I see this error:
image
The point is, the web page is attempting to reach http://server:port/context-path/v3/api-docs instead of http://server:port/custom-path/context-path/v3/api-docs

As a last thing to mention, if I try to load http://server:port/custom-path/context-path/swagger-ui/index.html?url=/custom-path/context-path/v3/api-docs&validatorUrl= the page loads as expected

Is there a way to customize the path it loads by default? or maybe make it able to know the path where you are?

@springdoc
Copy link
Collaborator

Hi,

In general, behind à reverse proxy, your application that ships the springbok-openapi should have the following line in the application.properties:

server.use-forward-headers=true

This will make sure that, the redirects of the swagger-ui, will not go to the server behind the proxy.
Can you please test it, and give us your feedback?

@hectoryo
Copy link
Author

I've tried your suggested property but sadly it seems that only that is not enough. I'll try to check what else is missing if you think it should work without needing to change how the path is built

@springdoc
Copy link
Collaborator

Hi,

First of all, you need to make sure the following header is set in your reverse proxy configuration: X-Forwarded-Prefix
For example, using apache 2, configuration

RequestHeader set X-Forwarded-Prefix "/custom-path"

Then, in your spring-boot application make sure your application handles this header: X-Forwarded-For. There are two ways to achieve this:

  • Since spring-boot 2.2, there is a new property to handle reverse proxy headers:
server.forward-headers-strategy=framework
  • Or you can add the following Bean to your application:
@Bean
ForwardedHeaderFilter forwardedHeaderFilter() {
   return new ForwardedHeaderFilter();
} 

Please don't forget to give a feedback about which option resolved your issue.

@hectoryo
Copy link
Author

Hi, I've been able to fix this issue by adding the @Bean and adding the headers to my nginx proxy.
Thanks

@springdoc
Copy link
Collaborator

Great. Thank you for your feedback.

@usersina
Copy link

usersina commented Dec 7, 2023

For anyone still struggling with this, I've written a "mini guide" here.

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

No branches or pull requests

2 participants