如果我尝试使用其余的一些API端点,我的角度应用程序会返回404页面

时间:2017-12-10 12:21:46

标签: docker nginx docker-compose dockerfile

如何配置NGINX发送我的Angular前端应用程序,以及访问我的烧瓶后端休息api。

  

我的堆栈看起来像这样。

- NGINX
- Angular 5 for the front-end.
- Python Flask for the REST API
- Elasticsearch for my database.

如果用户选择默认路由"/",我希望NGINX提供angular 5 dist文件夹,这是我拥有的网站资源文件(HTML,CSS和javascript)

另外,我想配置带有REST端点的NGINX。

我的问题是,当我点击localhost时,我得到了index.html中的所有内容,但是当我点击/ api时,我得到404 Not Found为什么?

app.py


''' Docker + python 3.6.3 '''

from flask import Flask

app = Flask(__name__)

@app.route('/api')
def hello():
    return 'Hello Form Flask'

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

Dockerfile

FROM python:3.6-alpine
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

docker-compose file


# Run docker-compose build
# Run docker-compose up
# Live long and prosper

version: '3'

services:

  nginx_demo:
    image: nginx:1.13.7-alpine
    container_name: nginx
    restart: always
    build:
      context: .
      dockerfile: nginx/Dockerfile
    volumes:
     - ./Client/dist:/usr/share/nginx/html
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - flask_demo

  flask_demo:
    # image: flask
    container_name: flask_demo
    restart: always
    build: ./Server
    volumes:
      - ./Server:/usr/src/app
    ports:
      - "5000:5000"
    depends_on:
      - elasticsearch

  elasticsearch:
    image: elasticsearch:5.6.5
    ports:
      - "9200:9200"

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 {
      include       /etc/nginx/mime.types;
      default_type  application/octet-stream;

      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

      access_log  /var/log/nginx/access.log  main;

      sendfile        on;
      #tcp_nopush     on;

      keepalive_timeout  65;

      gzip  on;

      include /etc/nginx/conf.d/*.conf;


      # Configuration for the server
      server {

        listen 80;
        server_name localhost;

        location / {
          root   /usr/share/nginx/html;
          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 /api {
          proxy_pass          http://flask_demo:5000;
          proxy_set_header        Host $host;
        }

        }



    }

1 个答案:

答案 0 :(得分:0)

查看此post它应该有所帮助。