使用docker-compose从nginx + uwsgi提供时,为所有已编译的Angular文件获取404

时间:2017-05-30 03:36:52

标签: angular nginx flask docker-compose uwsgi

我正在尝试使用以下技术创建Web应用程序:

- Angular
- Flask
- Nginx
- uwsgi
- Docker

问题是我为除了index.html之外的所有编译的Angular Javascript文件获得了404。

enter image description here

但是,Flask端的路由工作正常,如下面的快照所示:

enter image description here

我在nginx.conf和docker文件中尝试了一些配置更改但是徒劳无功。我是Docker的新手,无法弄清楚为什么Angular应用程序无法加载。如果我能得到一些帮助,那就太好了。

  

我有以下代码结构及其内容:

app/project/main/ - static (Contains 'dist' dir that holds the output of ng build) - app.py - Dockerfile - requirements.txt - uwsgi.ini nginx - nginx.conf - Dockerfile docker-compose.yml

  

app.py

from flask import Flask

app = Flask(__name__)


@app.route('/hello')
def hello_world():
    return "Hello World from Flask using Python 2.7"
@app.route("/")
def main():
    return send_file('./static/dist/index.html')


if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)
  

Dockerfile(uwsgi)

FROM python:2.7

MAINTAINER Sanjiv Kumar

ENV PYTHONUNBUFFERED 1

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

ADD requirements.txt /usr/src/app/

RUN pip install --no-cache-dir -r requirements.txt

ADD . /usr/src/app

ADD uwsgi.ini /etc/uwsgi.ini

CMD uwsgi --ini /etc/uwsgi.ini
  

Dockerfile(nginx)

FROM nginx:alpine

MAINTAINER Sanjiv Kumar

# Copy custom nginx config
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf

EXPOSE 80 443

ENTRYPOINT ["nginx"]
CMD ["-g", "daemon off;"]
  

requirements.txt

flask
uwsgi
  

uwsgi.ini

[uwsgi]
http-socket = 0.0.0.0:8000
chdir = /usr/src/app/project/main
wsgi-file = app.py
callable = app
master = true
uid = 1
gid = 1
die-on-term = true
processes = 4
threads = 2
  

搬运工-compose.yml

    version: '2.0'

services:

  nginx:
    container_name: nginx_container
    restart: always
    container_name: nginx
    image: nginx
    build:
      context: .
      dockerfile: nginx/Dockerfile
    volumes_from:
      - app
    links:
      - "app"
    ports:
      - "8080:80"
      - "443:443"
    depends_on:
      - "app"

    networks:
      - app-network

  app:
    container_name: flask_container
    restart: always
    build: app
    volumes:
      - /usr/src/app/project
      - ./app/project/main/static/dist:/usr/src/app/project/main/static/dist
    ports:
      - "8000:8000"
    networks:
      - app-network

networks:
  app-network:
    driver: bridge
  

nginx.conf

user  nginx;
worker_processes 1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events { worker_connections 1024; }

http {
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 30m;

        #See http://blog.argteam.com/coding/hardening-node-js-for-production-part-2-using-nginx-to-avoid-node-js-load
        #proxy_cache_path        /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m
        #inactive=600m;
        proxy_temp_path         /var/tmp;
        include                 mime.types;
        default_type            application/octet-stream;
        sendfile                on;
        keepalive_timeout       65;

        gzip                    on;
        gzip_comp_level         6;
        gzip_vary               on;
        gzip_min_length         1000;
        gzip_proxied            any;
        gzip_types              text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_buffers            16 8k;

    upstream flask {
       server app:8000;
    }

    server {
      listen       80;
      server_name  localhost;

      location /static {
          root   /usr/src/app/project/main/static;
          index  index.html;
          expires -1;
          add_header Pragma "no-cache";
          add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0,
          pre-check=0";
          try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
      }

      location / {
        #include    uwsgi_params;
        uwsgi_pass flask;

        uwsgi_param Host $host;
        uwsgi_param X-Real-IP $remote_addr;
        uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
        uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
      }
    }


}

更新

  • Angular + Nginx + docker工作正常,但问题是当我介绍Flask + uwsgi时,我开始得到404s。
  • 我尝试更改docker中的一些内容以及nginx conf设置,但仍然遇到同样的问题。还搜索了类似的问题,但无法找到一个。如果有人可以指导我,这将是非常有帮助的。谢谢!

1 个答案:

答案 0 :(得分:1)

我差点忘了为自己的问题发帖回答。对不起。 所以我想出了问题,它与方式有关,我正在使用nginx.conf访问我的角度和后端请求。我经历了几个博客/ github回购并最终取得了成功。我创建了一个带有工作骨架项目的github仓库,并读了我在本地运行该项目。请在此处结帐:Angular-Flask-Docker-Skeleton