栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Nginx不为我的Flask网站提供服务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nginx不为我的Flask网站提供服务

退后一步,单独运行Flask应用。

您有一些语法错误。

from flask import Flask, render_templateapp=Flask(__name__)@app.route('/')def home():   # Remove double brackets    return render_template('home.html')  # The templates folder is already picked upif __name__=="__main__":    app.run(host="0.0.0.0", port=5000)

然后放在一个Docker容器中,不带枪

FROM python:3.5RUN mkdir /mainwebCOPY . /mainwebWORKDIR /mainwebRUN pip install -r requirements.txtEXPOSE 5000CMD ["python3","/mainweb/app.py"]

并运行它,看看它是否有效。

cd mainappdocker build -t flask:test .docker run --rm -p 5000:5000 flask:test

开启http:// server:5000

然后从docker-compose仅使用该容器开始,并根据需要定义nginx。

nginx / Dockerfile

FROM nginx:1.13.1-alpineADD flask.conf /etc/nginx/conf.d/EXPOSE 8080

nginx / flask.conf(我根据项目中的文件进行了更改)

server {    listen 8080;    # This is the port to EXPOSE in nginx container    server_name app;  # You can change this, but not necessary    charset utf-8;    location ^~ /static/ {        alias /usr/share/nginx/html/;     }    location / {        try_files $uri $uri/ @flask;    }    location @flask {        proxy_pass http://app:5000;   # This is the port Flask container EXPOSE'd        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X_Forwared-For $proxy_add_x_forwarded_for;    }}

最后,撰写。您不想让您的网站同时暴露5000和80(您不希望人们绕过Nginx),所以不要暴露5000

version: '2'services:    app:        restart: always        build: ./mainweb        networks: - mainnet    nginx:        restart: always        build: ./nginx        networks: - mainnet        links: - app        volumes: - ./mainweb/static:/usr/share/nginx/html        ports: - "80:8080"networks:    mainnet:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661448.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号