1

I have a Flask application that is currently being served on a machine (call it first_vm) using the following command in supervisord:

gunicorn run:app -b 0.0.0.0:8002 -k socketio.sgunicorn.GeventSocketIOWorker --workers 1

This application is also reverse proxied locally on port 80 by nginx, such that http://first_vm/ will serve the application as expected.

What I would like to do is to reverse proxy the application on a different server (say second_vm), such that http://second_vm/my_app serves the application by hitting gunicorn on first_vm:8002.

This is the nginx configuration I currently have on second_vm:

location /my_app {
    proxy_pass http://first_vm:8002/;

    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-Host $server_name;
}

This configuration works fine in that it serves my application on http://second_vm/my_app, but static files (e.g. JS/CSS) don't get served properly because the browser tries to fetch them from http://second_vm/ instead of http://second_vm/my_app.

For example, Chrome will tell me that it Failed to load resource: the server responded with a status 404 in the following scenario:

http://second_vm/blueprint/static/css/bootstrap.min.css   <-- what Chrome fails to fetch
http://second_vm/my_app/blueprint/static/css/bootstrap.min.css   <-- what it should fetch

Could anyone help me make this work?

ncocacola
  • 111
  • 1
  • 5

1 Answers1

2

After many trials and tribulations I discovered a blog post that solved the issue for me as well. http://albertoconnor.ca/blog/2011/Sep/15/hosting-django-under-different-locations It mentions Django, but otherwise it's the same software (namely gunicorn and nginx) and the fix is the same as the root cause is the same.

Edit:

Essentially the flask app is missing the SCRIPT_NAME header which tells it where the app lives so adding proxy_set_header SCRIPT_NAME /my_app; is the first start, but then you need to fix the redirect as well, as there is some under the hood fixing of urls as well, so proxy_redirect http://first_vm:8002/my_app http://$host/my_app; should prevent it from doing things like returning relative addresses between the first_vm and the second_vm.