使用 nginx gunicorn 在 digitalocean 上部署 django react
deploying django react on digitalocean with nginx gunicorn
我在 django 中构建了 bind react。它在本地主机上工作正常,但在服务器上它的静态文件不起作用。当我 运行 在激活 virtualenv 之后:
gunicorn --bind 64.225.24.226:8000 liveimage.wsgi
其管理面板在没有 css 的情况下工作:
这是我的 nginx 默认配置:
server {
listen 80;
server_name 64.225.24.226;
access_log off;
location /media/ {
alias /root/liveimage/media/; # change project_name
}
location /static {
autoindex on;
alias /root/liveimage/build;
}
location / {
proxy_pass http://127.0.0.1;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
我的seetings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'build')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'build/static')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
gunicorn.service:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/liveimage
ExecStart=/root/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
liveimage.wsgi:application
[Install]
WantedBy=multi-user.target
我的项目在 /root/liveimage 和 virtualenv 在 /root/venv
我不知道出了什么问题
Gunicorn 不会显示样式。
我找到了这个:
Note: The admin interface will not have any of the styling applied since Gunicorn does not know how to find the static CSS content responsible for this.
来自docs
我不太了解 Nginx 配置,但这里的关键是 Django 在生产环境中不提供静态文件,它们必须由 Nginx 提供。
在您的站点上,转到浏览器的开发工具 -> 网络 -> 单击 css,然后查看 url。它们必须以 manage.py collectstatic
生成的静态目录为目标,Nginx 必须从这些 url 提供该目录的内容。
我在 django 中构建了 bind react。它在本地主机上工作正常,但在服务器上它的静态文件不起作用。当我 运行 在激活 virtualenv 之后:
gunicorn --bind 64.225.24.226:8000 liveimage.wsgi
其管理面板在没有 css 的情况下工作:
这是我的 nginx 默认配置:
server {
listen 80;
server_name 64.225.24.226;
access_log off;
location /media/ {
alias /root/liveimage/media/; # change project_name
}
location /static {
autoindex on;
alias /root/liveimage/build;
}
location / {
proxy_pass http://127.0.0.1;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
我的seetings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'build')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
]
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'build/static')]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
gunicorn.service:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/liveimage
ExecStart=/root/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
liveimage.wsgi:application
[Install]
WantedBy=multi-user.target
我的项目在 /root/liveimage 和 virtualenv 在 /root/venv 我不知道出了什么问题
Gunicorn 不会显示样式。
我找到了这个:
Note: The admin interface will not have any of the styling applied since Gunicorn does not know how to find the static CSS content responsible for this.
来自docs
我不太了解 Nginx 配置,但这里的关键是 Django 在生产环境中不提供静态文件,它们必须由 Nginx 提供。
在您的站点上,转到浏览器的开发工具 -> 网络 -> 单击 css,然后查看 url。它们必须以 manage.py collectstatic
生成的静态目录为目标,Nginx 必须从这些 url 提供该目录的内容。