NGINX不与烧瓶restAPI通信。 docker

NGINX不与烧瓶restAPI通信。 当我打localhost / api如果我打localhost我从index.html但不是JSON响应时,我打/ / API

我想用NGINX来服务我的Angular 5 dist文件夹。

什么是dist?

dist是文件夹,我有我的index.html,main.css,和所有其他文件Angular 5为我创build后,我运行生成 – 生产

我的问题是什么?

  • 我想要Angular 5的前端
  • 我想我的烧瓶是我的restAPI
  • 我希望nginx能够提供所有的静态文件(HTML,CSS和javascript)

我想要的用户如果input的URL( http:// localhost )NGINX给他所有的静态文件,让我的SPA应用程序的ROUTES除了API路线

如果用户input( http:// localhost / api ),我希望FLASK控制并给予用户JSON响应,无论我说烧瓶响应

我的问题是当用户点击## / api我得到404页面没有find跆拳道我已经设置内部Nginx如果得到/ API让烧瓶采取行动看:

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; proxy_pass http://localhost:5000/; } # location /api { # proxy_pass http://flask_api:5000; # proxy_set_header Host $host; # } } } 

我有一个docker-compose文件,看起来像这样:

泊坞窗,compose.yml

 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" depends_on: - flask_api flask_api: # image: flask container_name: flask_api restart: always build: ./Server volumes: - ./Server:/usr/src/app ports: - "5000:80" 

这是在Server / api.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) 

这是我运行docker-compose之后的日志

 Creating flask_api ... Creating flask_api ... done Creating nginx ... Creating nginx ... done Attaching to flask_api, nginx flask_api | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) flask_api | * Restarting with stat flask_api | * Debugger is active! flask_api | * Debugger PIN: 718-970-762 nginx | 172.20.0.1 - - [10/Dec/2017:14:16:53 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-" nginx | 172.20.0.1 - - [10/Dec/2017:14:16:55 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-" nginx | 172.20.0.1 - - [10/Dec/2017:14:16:56 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-" nginx | 2017/12/10 14:16:59 [error] 5#5: *2 open() "/usr/share/nginx/html/api" failed (2: No such file or directory), client: 172.20.0.1, server: localhost, request: "GET /api HTTP/1.1", host: "localhost" nginx | 172.20.0.1 - - [10/Dec/2017:14:16:59 +0000] "GET /api HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-" nginx | 172.20.0.1 - - [10/Dec/2017:14:17:01 +0000] "GET /api HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-" nginx | 2017/12/10 14:17:01 [error] 5#5: *2 open() "/usr/share/nginx/html/api" failed (2: No such file or directory), client: 172.20.0.1, server: localhost, request: "GET /api HTTP/1.1", host: "localhost" nginx | 2017/12/10 14:17:02 [error] 5#5: *2 open() "/usr/share/nginx/html/api" failed (2: No such file or directory), client: 172.20.0.1, server: localhost, request: "GET /api HTTP/1.1", host: "localhost" nginx | 172.20.0.1 - - [10/Dec/2017:14:17:02 +0000] "GET /api HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-" 

这应该工作:

泊坞窗,compose.yml

 # Run docker-compose up 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" depends_on: - flask_api links: - flask_api flask_api: container_name: flask_api restart: always build: context: . dockerfile: ./Server/Dockerfile volumes: - ./Server:/usr/src/app 

Nginx的/ Dockerfile

 FROM nginx:1.13.7-alpine # remove the old nginx.conf RUN rm /etc/nginx/nginx.conf # Copy custom nginx config COPY ./Nginx/nginx.conf /etc/nginx/nginx.conf COPY ./Client/dist /usr/share/nginx/html EXPOSE 80 

服务器/ Dockerfile

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

Nginx的/ 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; keepalive_timeout 65; gzip on; # Configuration for the server server { listen 80; location /api { proxy_pass http://flask_api:5000; } 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/ =404; } } }