Properly mount Flask app at location using Nginx r

2019-08-04 08:40发布

问题:

I have a Flask app, which runs inside a Docker container and should be exposed under a specific URL: myserver.com/mylocation. I want to use another container running Nginx as a reverse proxy to achieve the routing. I am following an awesome tutorial that got me quite far already.

My Nginx-config (the relevant part) reads:

server {
    server_name myserver.com;

    location /mylocation {
        proxy_pass http://myapp:5000;
        proxy_set_header Host $host;
        rewrite ^/mylocation(.*)$ $1 break;
    }
}

My docker-compose.yml reads:

version: '2'
  services:
    nginx:
      image: nginx:latest
      container_name: production_nginx
      volumes:
        - ./nginx.conf:/etc/nginx/nginx.conf
      ports:
        - 80:80
        - 443:443

    myapp:
      build: .
      image: app_image
      container_name: app_container
      expose:
        - "5000"

Now, when I run this, I successfully get my applications' index.html from myserver.com/mylocation, but subsequent requests (the CSS, JS etc) are being fired at myserver.com without the location part (/mylocation), and so Nginx does not route them to the container and they 404. The references to CSS, JS and such are all relative, they do not (and should not) contain the server name and location.

How can I achieve this? Am I missing something in my NGinx config that would let the app know it should run at /mylocation?