Skip to content

Commit

Permalink
Merge pull request #177 from traviscook21/master
Browse files Browse the repository at this point in the history
Add support for NGINX Reverse Proxy
  • Loading branch information
rochacbruno committed Jan 26, 2018
2 parents f213abe + de309b4 commit bb7ebac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,23 @@ Swagger(app, template=template)

The `LazyString` values will be evaluated only when `jsonify` encodes the value at runtime, so you have access to Flask `request, session, g, etc..` and also may want to access a database.

## Behind a reverse proxy

Sometimes you're serving your swagger docs behind an reverse proxy (e.g. NGINX). When following the [Flask guidance](http://flask.pocoo.org/snippets/35/),
the swagger docs will load correctly, but the "Try it Out" button points to the wrong place. This can be fixed with the following code:

```python
from flask import Flask, request
from flask import Swagger, LazyString, LazyJSONEncoder

app = Flask(__name__)
app.json_encoder = LazyJSONEncoder

template = dict(swaggerUiPrefix=LazyString(lambda : request.environ.get('HTTP_X_SCRIPT_NAME', '')))
swagger = Swagger(app, template=template)

```

# Customize default configurations

Custom configurations such as a different specs route or disabling Swagger UI can be provided to Flasgger:
Expand Down
7 changes: 6 additions & 1 deletion flasgger/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ def get(self):
operations[verb] = operation

if len(operations):
srule = str(rule)
try:
# Add reverse proxy prefix to route
prefix = self.template['swaggerUiPrefix']
except (KeyError, TypeError):
prefix = ''
srule = '{0}{1}'.format(prefix, rule)
# old regex '(<(.*?\:)?(.*?)>)'
for arg in re.findall('(<([^<>]*:)?([^<>]*)>)', srule):
srule = srule.replace(arg[0], '{%s}' % arg[2])
Expand Down

0 comments on commit bb7ebac

Please sign in to comment.