Flask url_for在由docker运行时生成http而不是https

时间:2018-07-11 13:57:53

标签: docker nginx flask

我有一个在docker中运行的flask应用,并使用Let's Encrypt设置了nginx。我已将proxy_set_header X-Forwarded-Proto $scheme;添加到我的nginx配置中,但是url_for仍返回http url而不是https url。但是仅在docker中运行时才会出现此问题。

这是我的nginx配置:

server {
    listen 443 ssl default_server;
    ssl_certificate /etc/letsencrypt/live/markote.app/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/markote.app/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
        proxy_pass http://127.0.0.1:5000;
        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;
    }
}

和我的docker文件:

FROM python:3.6-slim

WORKDIR /app

ADD . /app

RUN apt update
RUN apt install -y curl sudo gnupg libcairo2-dev
RUN pip install gunicorn
RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
RUN sudo apt-get install -y nodejs

RUN python setup.py install
RUN npm install
RUN npm run build

EXPOSE 5000

CMD [ "gunicorn", "-c", "gunicorn.py", "wsgi:app" ]

如果我运行gunicorn -c gunicorn.py wsgi:app &而不是sudo docker run -d -p 5000:5000 my/repo,则url_for可以成功生成https。我想念什么吗?

1 个答案:

答案 0 :(得分:1)

我知道已经有一段时间了,但是您可以尝试url_for('endpoint', _schema='https')