ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

python – Nginx没有服务我的Flask网站

2019-10-09 02:10:16  阅读:257  来源: 互联网

标签:python nginx docker-compose web


我正在关注这个exampleanswer on stackoverflow,我被困住了.
我在digitalocean VPS上运行此示例.我的文件结构如下:

项目结构

      docker-compose.yml
      mainweb/
      nginx/
      README

泊坞窗,compose.yml

version: '2'
services:
    app:
        restart: always
        build: ./mainweb
        command: gunicorn -w 2 -b :5000 wsgi:app
        networks:
            - mainnet
        expose:
            - "5000"
        ports:
            - "5000:5000"
    nginx:
        restart: always
        build: ./nginx
        networks:
            - mainnet
        links:
            - app
        volumes:
            - /www/static
        expose:
            - 8080
        ports:
            - "8880:8080"
networks:
    mainnet:

mainweb /

  app.py
  Dockerfile
  requirements.txt
  templates/
  wsgi.py

mainweb / app.py

from flask import Flask, render_template

app=Flask(__name__)

@app.route('/')
def home()():
    return render_template('templates/home.html')


if __name__=="__main__":
    app.run(host="0.0.0.0", port=5000)

mainweb / Dockerfile

FROM python:3.5
MAINTAINER castellanprime

RUN mkdir /mainweb
COPY . /mainweb
WORKDIR /mainweb
RUN pip install -r requirements.txt

mainweb /模板/

   home.html

mainweb /模板/ home.html做为

<!doctype html>
<html>
<head>
    <title> My website </title>
</head>
<body>
    <h1> I am here </h1>
</body>
</html>

mainweb / wsgi.py

from app import app

if __name__=="__main__":
    app.run()

nginx的

   Dockerfile
   sites-enabled.conf
   static/

nginx的/ Dockerfile

FROM nginx:1.13.1-alpine
MAINTAINER castellanprime
ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf
ADD static/ /www/static/

nginx的/网站,enabled.conf

server{

    listen 8080;
    server_name app;  #  Should I put my actual www.XXXXXX.XXXXX address here
    charset utf-8;

    location /static{
        alias /www/static/;
    }

    location / {
        proxy_pass http://app:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X_Forwared-For $proxy_add_x_forwarded_for;
    }
}

nginx的/静

   css/
   js/

运行命令docker-compose up -d后,我从另一个系统上的另一个Web客户端检查www.XXXXXX.com:8880或www.XXXXXX.com:8080.
我得到了标准的nginx网页.

如何将其重定向到home.html?

解决方法:

退后一步,单独运行Flask应用程序.

你有一些语法错误.

from flask import Flask, render_template

app=Flask(__name__)

@app.route('/')
def home():   # Remove double brackets
    return render_template('home.html')  # The templates folder is already picked up

if __name__=="__main__":
    app.run(host="0.0.0.0", port=5000)

然后在Docker容器中,没有gunicorn

FROM python:3.5

RUN mkdir /mainweb
COPY . /mainweb
WORKDIR /mainweb
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python3","/mainweb/app.py"]

运行它,看它是否有效.

cd mainapp
docker 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-alpine
ADD 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:

标签:python,nginx,docker-compose,web
来源: https://codeday.me/bug/20191009/1875830.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有