skip to Main Content

Here is my nginx conf for my Flask app:

server {
    listen       8888;
    
    location /dev1 {
        proxy_pass  http://127.0.0.1:8000/;
    }

    location /prod1 {
        proxy_pass  http://127.0.0.1:8001/;
    }

    location /prod2 {
        proxy_pass  http://127.0.0.1:8002/;
    }

    location /prod3 {
        proxy_pass  http://127.0.0.1:8003/;
    }
}

Now if I go to myapp/prod1, it is working correctly, however, clicking on any links on the website redirect me to /link1, whereas I want to keep the prefix so that clicking would redirect me to /prod1/link1. I am not sure if this is a problem that I need to fix in Flask or nginx, some of my route look like this:

@app.route("/")
@app.route("/link1")
def link1(....):
    ...

2

Answers


  1. You can do it with Flask Blueprints when your register them.

    app.register_blueprint(simple_page, url_prefix='/pages')
    app.url_map
    Map([<Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>,
     <Rule '/pages/<page>' (HEAD, OPTIONS, GET) -> simple_page.show>,
     <Rule '/pages/' (HEAD, OPTIONS, GET) -> simple_page.show>])
    

    Check the documentation for full example: https://flask.palletsprojects.com/en/2.0.x/blueprints/

    Login or Signup to reply.
  2. When you are using a wsgi server, e.g. gunicorn, you can use this command:

    $ SCRIPT_NAME=/prefix gunicorn app:app
    

    Just use different prefixes for your different Flask apps.

    Also see this wonderful blog post https://dlukes.github.io/flask-wsgi-url-prefix.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search