我正在为我的应用程序使用最新的https://github.com/pydanny/cookiecutter-django模板,我想使用nginx而不是caddy webserver进行制作。所以我的docker compose与默认值相同: https://github.com/pydanny/cookiecutter-django/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/production.yml
除了我将球童转为nginx:
nginx:
build:
context: .
dockerfile: ./compose/production/nginx/Dockerfile
image: abs_production_nginx
depends_on:
- django
ports:
- "0.0.0.0:80:80"
env_file:
- ./.envs/.production/.nginx
我的nginx泊坞文件:
FROM nginx:latest
ADD ./compose/production/nginx/nginx.conf /etc/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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream app {
server django:5000;
}
server {
listen 80;
charset utf-8;
location / {
try_files $uri @proxy_to_app;
}
# cookiecutter-django app
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
}
}
此配置有效,但我无法提供静态文件。我试着添加
location /media {
autoindex on;
alias /app/media;
}
location /static {
autoindex on;
alias /app/staticfiles;
}
在docker compose中添加:
volumes_from:
- django
尝试将整个项目复制到nginx容器,但是没有用,因为我在collectstatic
容器内执行django
命令
答案 0 :(得分:0)
在VPS上转移项目后,首先使用collectstatic命令:
python manage.py collectstatic
这条路径:/ app / staticfiles应该是静态文件的父目录而不是它自己。
alias /app/staticfiles;
如果它不起作用,请使用:
root /app/staticfiles;
更改nginx文件后,重启nginx
sudo service nginx restart
答案 1 :(得分:0)
这应该在开发环境中起作用;我将此用于我的cookiecutter-django应用程序。但是,全面披露我对Nginx还是陌生的,对于产品环境而言,这可能不是最好的选择。
location /static/ {
proxy_pass http://app/static/;
}
location /media/ {
proxy_pass http://app/media/;
}