使用docker+nginx+django/angularjs提供静态文件时出现404

使用docker+nginx+django/angularjs提供静态文件时出现404

我遵循以下配置存储库。我决定将我的 docker 配置和实际代码保存在单独的存储库中 - 我将使用 Dockerfile 克隆代码存储库。我正在使用 docker-machine(本地,使用 Virtualbox 驱动程序)和 docker-compose 设置。

代码仓库的目录结构大致如下:

|-- bower.json
|-- CONTRIBUTORS
|-- defsec
|   |-- defsec
|   |   |-- aws_settings.py
|   |   |-- heroku_settings.py
|   |   |-- __init__.py
|   |   |-- settings.py
|   |   |-- urls.py
|   |   |-- views.py
|   |   `-- wsgi.py
|   |-- manage.py
|   |-- quiz_restful
|   |   |-- __init__.py
|   |   |-- permissions.py
|   |   |-- serializers.py
|   |   |-- services.py
|   |   |-- tests.py
|   |   `-- views.py
|   `-- users
|       |-- __init__.py
|       |-- models.py
|       |-- permissions.py
|       |-- serializers.py
|       `-- views.py
|-- extras
|-- gulpfile.js
|-- package.json
|-- Procfile
|-- README.md
|-- requirements.txt
|-- scripts
|   `-- postInstall.sh
|-- static
|   |-- javascripts
|   |   |-- app.js
|   |   |-- controllers
|   |   |   `-- controllers.js
|   |   |-- directives
|   |   |   `-- directives.js
|   |   `-- services
|   |       `-- services.js
|   |-- partials
|   |   |-- eval.html
|   |   |-- exam.html
|   |   |-- exam-partials
|   |   |   |-- exam-view.html
|   |   |   `-- sidebar.html
|   |   |-- login.html
|   |   `-- register.html
|   `-- stylesheets
|       `-- styles.css
|-- templates
|   |-- index.html
|   |-- javascripts.html
|   |-- navbar.html
|   `-- stylesheets.html

docker-compose.yml文件与我上面列出的存储库中的文件几乎相同,只有一点不同,那就是我将其挂载django到卷上.:/root- 如果没有它,[8] System error: no file or directory则会抛出错误。这可能是因为/usr/src/app在克隆之前不存在(供参考,是原始文件)。Dockerfile(用于服务django)具有以下内容:

FROM ubuntu:14.04

ENV DJANGO_CONFIGURATION Docker

# First, we need to get git, and clone our repository
# Additionally, get everything else here too, such as nodejs and npm

RUN apt-get update
RUN apt-get install -y ca-certificates git-core ssh nodejs npm python-pip libpq-dev python-dev
RUN ln -s /usr/bin/nodejs /usr/bin/node

ENV HOME /root

# Add custom ssh keypair - usually Bitbucket deployment keys
ADD ssh/ /root/.ssh/

# Fix permissions
RUN chmod 600 /root/.ssh/*

# Avoid first connection host confirmation
RUN ssh-keyscan bitbucket.org > /root/.ssh/known_hosts

# Clone the repo
WORKDIR /usr/src/app
RUN git clone [email protected]:username/defsec-exam-app.git

# Install requirements
WORKDIR /usr/src/app/defsec-exam-app
RUN pip install -r requirements.txt
RUN npm install -g bower
RUN bower --allow-root install

# Remember to perform migrations on your own, and also create DB when needed.
# S3 Storage
...

# DB Settings
...

WORKDIR /usr/src/app/defsec-exam-app/defsec
CMD ["gunicorn", "defsec.wsgi", "-w", "2", "-b", "0.0.0.0:8000", "--log-level", "-"]

最后,nginx.conf提供静态文件的方法如下:

worker_processes 1;

events {

    worker_connections 1024;

}

http {

    server {
        listen 80;
        server_name example.org;

        access_log /dev/stdout;
        error_log /dev/stdout info;

        location /static/ {
            alias /usr/src/app/defsec-exam-app/static;
        }

        location /static/javascripts/ {
          default_type text/javascript;
          alias /usr/src/app/defsec-exam-app/static/javascripts/;
        }

        location /static/stylesheets/ {
          default_type text/css;
          alias /usr/src/app/defsec-exam-app/static/stylesheets/;
        }

    location /static/bower_components/ {
      alias /usr/src/app/defsec-exam-app/static/bower_components/;
    }

        location / {
            proxy_pass http://django:8000;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

但是,我根本无法让 nginx 提供静态文件 - 它们都返回 404。我想知道nginxdjango容器服务(如 中定义docker-compose.yml)之间的链接是否不正确,但似乎不是。我还检查了容器/etc/hosts中的文件nginx,因为链接会创建主机文件条目。

172.17.0.136    151ca02e891a
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.134    defsecdocker_django_1 eb900ed9600c
172.17.0.135    defsecdocker_nginx_1 eec99206076a
172.17.0.134    django eb900ed9600c defsecdocker_django_1
172.17.0.134    django_1 eb900ed9600c defsecdocker_django_1
172.17.0.135    nginx eec99206076a defsecdocker_nginx_1
172.17.0.135    nginx_1 eec99206076a defsecdocker_nginx_1
172.17.0.116    defsecdocker_postgres_1
172.17.0.134    defsecdocker_django_1
172.17.0.135    defsecdocker_nginx_1.bridge
172.17.0.136    defsecdocker_nginx_run_10.bridge
172.17.0.135    defsecdocker_nginx_1
172.17.0.136    defsecdocker_nginx_run_10
172.17.0.116    defsecdocker_postgres_1.bridge
172.17.0.134    defsecdocker_django_1.bridge

我不确定这是否正确,因为只有 3 个容器,但 hosts 文件中却有大量条目,其中一些是重复的。这是可重现的行为 - 每次使用 docker-compose 构建和运行容器时,我都会得到这个确切的文件。docker-compose 日志可nginx确认 404。

任何指点都将不胜感激。

答案1

看起来 docker-compose 配置不正确。nginx 抛出这些 404 错误是因为它无法访问/usr/src/app/defsec-exam-app/static

这是正确的 docker-compose 配置-:

# Nginx
nginx:
    build: ./nginx
    volumes_from:
        - django
    links:
        - django
    ports:
        - "80:80"

# This defines a service for the Django app
# Will include the Angular frontend
django:
    build: .
    volumes:
        - .:/root
        - /usr/src/app
    expose:
        - "8000"
    links:
        - postgres

# This defines a service for the Postgres database
postgres:
    image: postgres:latest

volumes_from从 django 服务获取卷。我已经将其公开usr/src/app为卷。这似乎很管用。如果有任何建议,我欢迎!

此外,这是新的 nginx 配置 -:

worker_processes 1;

events {

    worker_connections 1024;

}

http {

    server {
        listen 80;
        server_name example.org;

        access_log /dev/stdout;
        error_log /dev/stdout info;

        location /static/ {
            alias /usr/src/app/defsec-exam-app/static;
        }

        location /static/javascripts/ {
          default_type text/javascript;
          alias /usr/src/app/defsec-exam-app/static/javascripts/;
        }

        location /static/stylesheets/ {
          default_type text/css;
          alias /usr/src/app/defsec-exam-app/static/stylesheets/;
        }

    location /static/bower_components/ {
      types {
        text/css css;
        text/javascript js;
      }
      alias /usr/src/app/defsec-exam-app/static/bower_components/;
    }

    location /static/partials/ {
      types {
        text/html html;
      }
      alias /usr/src/app/defsec-exam-app/static/partials/;
    }

    location /static/admin/ {
          alias /usr/src/app/defsec-exam-app/static/admin/;
    }

    location /static/admin/css {
      default_type text/css;
      alias /usr/src/app/defsec-exam-app/static/admin/css;
    }

    location /static/admin/js {
      default_type text/javascript;
      alias /usr/src/app/defsec-exam-app/static/admin/js;
    }

    location /static/admin/img {
      types {
        image/png png;
        image/jpeg jpg;
      }
      alias /usr/src/app/defsec-exam-app/static/admin/img;
    }

        location / {
            proxy_pass http://django:8000;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

注意:我必须将 django admin css/js/img 单独添加到 docker/deployment 存储库,以便 django admin 正常工作。(django Dockerfile 中的简单 ADD 命令即可解决问题)

相关内容