django+nginx部署没有正确加载静态文件
django+nginx deployment not loading static files correctly
server {
listen 80;
server_name 13.xx.xx.xxx;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
location / {
include proxy_params;
include /etc/nginx/mime.types;
proxy_pass http://unix:/home/ubuntu/studykarma/django-react-redux-base/src/djangoreactredux.sock;
}
}
我的 nginx 配置文件
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_dist'),
)
我的settings.py
我的静态文件在 static_dist 文件夹中,我的 reactjs 代码在静态中
文件夹。
我的静态文件没有加载并给我 404,
如果我将路径更改为 /static_dist/ 那么我也会得到空内容。
我正在使用这个模板:https://github.com/Seedstars/django-react-redux-base
关于Nginx配置
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
此配置告诉 Nginx 在通过 say example.com/static/app.js 访问时,如果需要 [=15],请查看 src
文件夹 root
中的 app.js 文件=],则配置更改为
location /static/{
root /home/ubuntu/studykarma/django-react-redux-base/src/path/to/static_dist/;
}
关于 Django 配置
您的 Django 设置在逻辑上是错误的,
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
上面的设置告诉django你期望你所有的静态文件从STATICFILES_DIRS
到STATIC_ROOT
,当你使用collectstatic
django manage 命令时配置是effective/used by django , 这实际上将所有文件从 STATICFILES_DIRS
复制到 STATIC_ROOT
( 但这也意味着,您必须将 /static/
位置 nginx 配置指向 static_root
而不是 static_dist
.
使用这个,
location /static_root/ { root /home/ubuntu/studykarma/django-react-redux-base/src/static_root/; }
它会起作用
server {
listen 80;
server_name 13.xx.xx.xxx;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
location / {
include proxy_params;
include /etc/nginx/mime.types;
proxy_pass http://unix:/home/ubuntu/studykarma/django-react-redux-base/src/djangoreactredux.sock;
}
}
我的 nginx 配置文件
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_dist'),
)
我的settings.py
我的静态文件在 static_dist 文件夹中,我的 reactjs 代码在静态中 文件夹。
我的静态文件没有加载并给我 404, 如果我将路径更改为 /static_dist/ 那么我也会得到空内容。
我正在使用这个模板:https://github.com/Seedstars/django-react-redux-base
关于Nginx配置
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
此配置告诉 Nginx 在通过 say example.com/static/app.js 访问时,如果需要 [=15],请查看 src
文件夹 root
中的 app.js 文件=],则配置更改为
location /static/{
root /home/ubuntu/studykarma/django-react-redux-base/src/path/to/static_dist/;
}
关于 Django 配置
您的 Django 设置在逻辑上是错误的,
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
上面的设置告诉django你期望你所有的静态文件从STATICFILES_DIRS
到STATIC_ROOT
,当你使用collectstatic
django manage 命令时配置是effective/used by django , 这实际上将所有文件从 STATICFILES_DIRS
复制到 STATIC_ROOT
( 但这也意味着,您必须将 /static/
位置 nginx 配置指向 static_root
而不是 static_dist
.
使用这个,
location /static_root/ { root /home/ubuntu/studykarma/django-react-redux-base/src/static_root/; }
它会起作用