![](/img/trans.png)
[英]Django application with nginx and gunicorn only showing welcome page rest all pages showing 404 error
[英]Use Docker, Gunicorn, Nginx in django development environment but can only see nginx welcome page?
我在django开发环境中使用Docker,Gunicorn,Nginx,但只能看到nginx欢迎页面。
我的泊坞文件:
Django的:
FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN groupadd -r django \
&& useradd -r -g django django
COPY ./requirements.txt /requirements.txt
RUN pip install --no-cache-dir -r /requirements.txt \
&& rm -rf /requirements.txt
COPY ./compose/django/gunicorn.sh /
RUN sed -i 's/\r//' /gunicorn.sh \
&& chmod +x /gunicorn.sh \
&& chown django /gunicorn.sh
COPY . /app
RUN chown -R django /app
RUN mkdir /static
RUN chown -R django /static
USER django
WORKDIR /app
nginx的:
FROM nginx:latest
ADD nginx.conf /etc/nginx/sites-enabled/django_blog.conf
gunicorn.sh:
#!/bin/sh
python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn blogproject.wsgi -w 4 -b 127.0.0.1:8000 --chdir=/app
nginx.conf:
server {
charset utf-8;
listen 80 default_server;
location /static {
alias /app/static;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8000;
}
}
泊坞窗,compose.yml:
version: '3'
services:
django:
build:
context: .
dockerfile: ./compose/django/Dockerfile
command: /gunicorn.sh
nginx:
build: ./compose/nginx
depends_on:
- django
ports:
- "80:80"
我运行的命令:
docker-compose build
码头工人组成
似乎Nginx没有向gunicorn传递请求?
更新:
根据@Robert的建议,事情很有效。 但是,由于静态文件在django容器中,所以我不知道如何让nginx托管它们? 我尝试根据Volume配置参考来设置卷,如下所示:
version: '3'
services:
django:
build:
context: .
dockerfile: ./compose/django/Dockerfile
volumes:
- static-volume:/app/static
command: /gunicorn.sh
nginx:
build: ./compose/nginx
volumes:
- static-volume:/app/static
depends_on:
- django
ports:
- "0.0.0.0:80:80"
volumes:
static-volume:
但它似乎不起作用? 我该如何让nginx访问django容器中的静态文件? 谢谢!
在nginx.conf中正确指向django:
proxy_pass http://django:8000;
感谢docker网络。
而且,我建议你覆盖默认的nginx conf。 所以改变这个:
ADD nginx.conf /etc/nginx/sites-enabled/django_blog.conf
对此:
ADD nginx.conf /etc/nginx/conf.d/default.conf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.