简体   繁体   中英

Nginx not serving my Flask website

I am following this example and this answer on stackoverflow and I am stuck.
I am running this example on a digitalocean VPS. My file structure is as follows:

project structure

      docker-compose.yml
      mainweb/
      nginx/
      README

docker-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/templates/

   home.html

mainweb/templates/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/sites-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/static

   css/
   js/

After I run the command docker-compose up -d , I check the www.XXXXXX.com:8880, or www.XXXXXX.com:8080 from another web client on another system.
I get the standard nginx web page.

How do I redirect it to the home.html?

Take a step back and run the Flask app alone.

You have some syntax errors.

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)

Then in a Docker container, and without 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"]

And run it, and see if it works.

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

Open http://server:5000

Then start on docker-compose with just that container and define nginx if you want.

nginx/Dockerfile

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

nginx/flask.conf (I changed this based on a file that I have in a project)

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;
    }
}

And finally, the compose. You don't want to have your site exposing both 5000 and 80 (you don't want people to bypass nginx), so just don't expose 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM