【发布时间】:2018-05-24 04:52:11
【问题描述】:
NGINX 不与烧瓶休息 API 通信。 当我点击 localhost/api 时 如果我点击本地主机,我会从 index.html 收到消息,但在点击 /api 时不会收到 json 响应
我想使用 NGINX 为我的 Angular 5 dist 文件夹提供服务。
什么是分布?
dist 是我运行 ng build --prod 后 Angular 5 为我创建的 index.html、main.css 和所有其他文件的文件夹
我的问题是什么?
- 我希望前端使用 Angular 5
- 我希望我的烧瓶成为我的休息 api
- 我希望 nginx 提供所有静态文件(HTML、CSS 和 javascript)
我希望用户输入 URL (http://localhost) NGINX 给他所有的静态文件,并让我的 SPA 应用程序为除 API 路由之外的 ROUTES 去站点
如果用户键入 (http://localhost/api),我希望 FLASK 能够控制并给用户 JSON 响应,无论我说什么,flask 都会响应
我的问题是当用户点击 ## /api 我得到 404 page not found WTF 我在 nginx 中设置了 if 是 get /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 文件:
docker-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 up 后的日志
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" "-"
【问题讨论】:
-
在
docker-compose.yml中,您发布了错误的端口。还可以考虑使用links指定 nginx 的主机。看看这里:docs.docker.com/compose/compose-file/#links -
谢谢@Ehsan 在 docker 文档中他们没有提及 NGINX 和 Flask .only Flask 和 REDIS。我应该使用什么端口
-
您已将两个容器的端口映射到主机上的 80。将
flask_api的发布端口更改为"5000:5000",因为这是 nginx 期望连接的地方。 -
另外
proxy_pass http://localhost:5000/;在 nginx 配置中是错误的,因为它指的是 nginx 容器本身。您应该在 docker-compose 文件中的nginx_demo的links部分中指定flask_api,然后您可以使用flask_api引用flask 应用程序的地址。 -
不工作我要发布我所有的代码你能看看我的问题是什么请给我 5 分钟
标签: python angular docker nginx flask