使用 docker-compose 从 nginx + uwsgi 服务时,为所有编译的 Angular 文件获取 404
Getting 404 for all compiled Angular files when serving from nginx + uwsgi using docker-compose
我正在尝试使用以下技术创建 Web 应用程序:
- Angular
- Flask
- Nginx
- uwsgi
- Docker
问题是除了 index.html.
之外的所有编译的 Angular Javascript 文件我都得到 404
但是,从 Flask 端开始的路由工作正常,如下图所示:
我在 nginx.conf 和 docker 文件中尝试了一些配置更改,但没有成功。我是 Docker 的新手,无法弄清楚为什么 Angular 应用程序加载失败。如果我能在这方面得到一些帮助,那就太好了。
I have following code structure and it's contents:
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
docker-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 设置,但仍然遇到同样的问题。还搜索了类似的问题,但找不到。如果有人可以指导我,那将非常有帮助。谢谢!
我差点忘了 post 回答我自己的问题。对不起。
所以我弄清楚了问题,它与方式有关,我正在使用 nginx.conf
访问我的 angular 和后端请求。我经历了几次 blogs/github 回购并最终取得了成功。我用一个工作框架项目创建了一个 github 回购协议,并在本地阅读了 运行 该项目。请在这里结帐:Angular-Flask-Docker-Skeleton
我正在尝试使用以下技术创建 Web 应用程序:
- Angular
- Flask
- Nginx
- uwsgi
- Docker
问题是除了 index.html.
之外的所有编译的 Angular Javascript 文件我都得到 404但是,从 Flask 端开始的路由工作正常,如下图所示:
我在 nginx.conf 和 docker 文件中尝试了一些配置更改,但没有成功。我是 Docker 的新手,无法弄清楚为什么 Angular 应用程序加载失败。如果我能在这方面得到一些帮助,那就太好了。
I have following code structure and it's contents:
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
docker-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 设置,但仍然遇到同样的问题。还搜索了类似的问题,但找不到。如果有人可以指导我,那将非常有帮助。谢谢!
我差点忘了 post 回答我自己的问题。对不起。
所以我弄清楚了问题,它与方式有关,我正在使用 nginx.conf
访问我的 angular 和后端请求。我经历了几次 blogs/github 回购并最终取得了成功。我用一个工作框架项目创建了一个 github 回购协议,并在本地阅读了 运行 该项目。请在这里结帐:Angular-Flask-Docker-Skeleton