Skip to content

Instantly share code, notes, and snippets.

@anson-vandoren
Last active April 6, 2022 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anson-vandoren/ca4a3d3f19d85ae96f24f5c5110cd60b to your computer and use it in GitHub Desktop.
Save anson-vandoren/ca4a3d3f19d85ae96f24f5c5110cd60b to your computer and use it in GitHub Desktop.
Flask-RESTPlus with Nginx reverse proxy on Docker

This setup does not require the "Flask snippet #35" solution that is most often given for flask-restplus answers to this question.

Using the "snippet 35" solution means that the "Try it!" commands from the SwaggerUI display the wrong endpoint, which adds confusion to others looking at your documentation.

from flask import Blueprint
from flask_restplus import Api, Resource
game_blueprint = Blueprint("game", __name__, url_prefix="/game")
api = Api(game_blueprint, doc="/docs/")
@api.route("/ping")
def PingPong(Resource):
def get(self):
return {"status": "success", "message": "pong!"}, 200
server {
listen 80;
# Below is the key that most other solutions are missing. The reverse proxy rule
# needs to pass along (to Flask) both the desired endpoint ('game' in this case)
# as well as the 'swaggerui' path in order to serve the Swagger CSS/JS correctly
location ~ ^/(game|swaggerui) {
# game is the Docker service running the Flask/Flask-RESTPlus app
proxy_pass http://game:5000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment