Nginx没有服务我的Flask网站

时间:2017-06-24 01:01:21

标签: python nginx web docker-compose

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

项目结构

(parent1,parent2,parent3,etc)

搬运工-compose.yml

function myParentObj(){
    this.name = 'jordan'
    this.names = ['jordan','danny','cassie'];
    this.init=()=>{
       this.names.forEach((name)=>{
          var childObj = new myChildObj(this,name);
          childObj.updateParentProperty();
       })
    }
}
function myChildObj(parentObj,name){
    this.parent = parentObj;
    this.name = name;
    this.updateParentProperty=()=>{
        this.parent.name = this.name;
    };
}
function init(){
     var parentObj = new myParentObj();
     parentObj.init();
}
document.addEventListener('DOMContentLoaded',init);

mainweb /

      docker-compose.yml
      mainweb/
      nginx/
      README

mainweb / app.py

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 / Dockerfile

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

mainweb /模板/

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 /模板/ home.html做为

FROM python:3.5
MAINTAINER castellanprime

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

mainweb / wsgi.py

   home.html

nginx的

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

的nginx / Dockerfile

from app import app

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

的nginx /站点-enabled.conf

   Dockerfile
   sites-enabled.conf
   static/

的nginx /静态

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

运行命令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; } } 后,我从另一个系统上的另一个Web客户端查看www.XXXXXX.com:8880或www.XXXXXX.com:8080。 我得到了标准的nginx网页。

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

1 个答案:

答案 0 :(得分:6)

退后一步,单独运行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: