django - Nginx为Django应用提供静态文件

标签 django web-services nginx deployment docker

我有一个要部署的Django应用。在这个阶段,我似乎无法从Nginx容器中提供我的静态文件。

我的项目设置为here

我已将图像放入{% static "minimal/theme/assets/img/pic.jpg"%}目录。

我的网络应用程序的文件结构为:

.
├── Dockerfile
├── docker_django
│   ├── __init__.py
│   ├── apps
│   │   ├── __init__.py
│   │   └── todo
│   │       ├── __init__.py
│   │       ├── admin.py
│   │       ├── fixtures
│   │       │   ├── foodprice.json
│   │       │   ├── location.json
│   │       │   └── menuitem.json
│   │       ├── forms.py
│   │       ├── models.py
│   │       ├── templates
│   │       │   ├── _base-original.html
│   │       │   ├── feedback_template.txt
│   │       │   ├── home-original.html
│   │       │   └── todo
│   │       │       ├── base-original.html
│   │       │       ├── base-template.html
│   │       │       ├── base.html
│   │       │       ├── detail.html
│   │       │       ├── email.html
│   │       │       ├── feedback.html
│   │       │       ├── home.html
│   │       │       ├── home_old.html
│   │       │       ├── location.html
│   │       │       ├── menu.html
│   │       │       ├── results.html
│   │       │       ├── test.html
│   │       │       └── thanks.html
│   │       ├── tests.py
│   │       ├── urls.py
│   │       └── views.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── requirements.txt
└── static
    ├── main.css
    └── minimal
        └── theme
            ├── assets
            │   ├── css
            │   ├── img
            │   │   ├── pic1.jpg
            └── index.html

在我的Nginx容器中,在启用了sites的文件夹中有一个配置文件,看起来像:
server {

listen 80;
server_name localhost;
charset utf-8;

location /static {
    alias /usr/src/app/static;
}

location / {
    proxy_pass http://web: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;
}

}

我的settings.py相关部分的摘要如下:
BASE_DIR =     os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print "base dir path", BASE_DIR
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
....
STATIC_URL = '/static/'
# STATICFILES_DIRS = (
#     os.path.join(BASE_DIR, 'static'),
# )
# print(STATICFILES_DIRS)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

production.yml就像:
web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
    - redis:redis
  env_file: .env
  command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000

nginx:
  restart: always
  build: ./nginx/
  ports:
    - "80:80"
  volumes:
    - /www/static
  volumes_from:
    - web
  links:
    - web:web

postgres:
  restart: always
  image: postgres:latest
  ports:
    - "5432"
  volumes:
    - pgdata:/var/lib/postgresql/data/

redis:
  restart: always
  image: redis:latest
  ports:
    - "6379"
  volumes:
    - redisdata:/data

当我在计算机上本地运行时,静态文件夹中的所有JavaScript和图像都会加载并正常运行。当我部署到 digital ocean 中时,不提供静态文件,并且我被困在如何让Nginx提供我的静态文件上。

最佳答案

您需要将文件夹/usr/src/app/static挂载为Web容器中的卷。这样,ngnix将能够对其进行访问。

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  volumes:
    - /usr/src/app/static
  links:
    - postgres:postgres
    - redis:redis
  env_file: .env
  command: /usr/local/bin/gunicorn docker_django.wsgi:application -w 2 -b :8000

nginx配置中,您要告诉nginx从该目录/usr/src/app/static提供静态文件,但是nginx容器没有链接,除非您将其链接。几乎可以做到,您拥有volumes_from: web,但是web容器未安装任何卷,因为您需要添加volumes: - /usr/src/app/static

关于django - Nginx为Django应用提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38979525/

相关文章:

Django:如果用于查找,最好将 slug 保存到数据库或动态生成?

java - 该客户端请求使用哪种 HTTP 方法?

nginx - DNS 问题 : NXDOMAIN looking up A for www. exampl.com - 检查此域是否存在 DNS 记录

html - Django WeasyPrint CSS 集成警告 : Relative URI reference without a base URI: <link href ="/static/css/bootstrap.min.css"> at line None

django - 一对一和外键关系之间的区别?

python - 在 django 中指定应用程序相关设置的位置

php file_get_contents 无法在 Linux 托管的 asmx 链接中工作

java - HTTPs 可以实现 Restful Web 服务吗?

wordpress - 如何使用漂亮的永久链接(SEO 友好的 URL)在 Nginx 上与 Laravel 一起安装 WordPress?

php - 如何在docker上同时运行多个项目