Django 无法在 ec2 中提供静态文件?
Django fails to serve static file in ec2?
我正在尝试使用 nginx 和 uwsgi 在 ec2 实例上部署一个 django 项目。
该项目在本地 pc 的开发模式下运行良好,可以轻松地在本地主机中提供静态文件。但问题是在 ec2 实例上部署后它无法加载静态文件 (css) 文件。
我的项目结构:
设置文件在设置目录中:
部分设置内容:
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages", "django.contrib.staticfiles",
"control",
]
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
nginx 内容:
upstream url_shortener {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name mahbubcseju.com ;
charset utf-8;
location = /favicon.ico {access_log off; log_not_found off; }
location = /static/ {
autoindex on;
alias /home/ubuntu/projects/url_shortener/static/;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass url_shortener;
}
}
uwsgi内容:
[uwsgi]
project = url_shortener
uid = ubuntu
base = /home/%(uid)
home = %(base)/projects
chdir = %(home)/%(project)
module = %(project).wsgi:application
master = true
processes = 5
chown-socket = %(uid):www-data
chmod-socket = 666
socket = 127.0.0.1:8000
vacuum = true
plugins = python3,http
我运行pythonmanage.py runserver collectstatic
在启动nginx服务器之前。
如果我尝试从浏览器访问 css 文件,它会出现以下错误:
要求:
http://mahbubcseju.com/static/css/home.css
回应:
Not Found
The requested resource was not found on this server.
用户 ubuntu 的静态目录权限:
drwxrwxrwx 4 ubuntu ubuntu 4096 年 11 月 7 日 07:50 静态
从 nginx 配置中的位置块中删除 =
,假设已在目录上设置了正确的权限以允许 nginx 用户访问,这应该可以工作:
location /static/ {
# autoindex on;
alias /home/ubuntu/projects/url_shortener/static/;
}
我正在尝试使用 nginx 和 uwsgi 在 ec2 实例上部署一个 django 项目。 该项目在本地 pc 的开发模式下运行良好,可以轻松地在本地主机中提供静态文件。但问题是在 ec2 实例上部署后它无法加载静态文件 (css) 文件。
我的项目结构:
设置文件在设置目录中:
部分设置内容:
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages", "django.contrib.staticfiles",
"control",
]
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
nginx 内容:
upstream url_shortener {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name mahbubcseju.com ;
charset utf-8;
location = /favicon.ico {access_log off; log_not_found off; }
location = /static/ {
autoindex on;
alias /home/ubuntu/projects/url_shortener/static/;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass url_shortener;
}
}
uwsgi内容:
[uwsgi]
project = url_shortener
uid = ubuntu
base = /home/%(uid)
home = %(base)/projects
chdir = %(home)/%(project)
module = %(project).wsgi:application
master = true
processes = 5
chown-socket = %(uid):www-data
chmod-socket = 666
socket = 127.0.0.1:8000
vacuum = true
plugins = python3,http
我运行pythonmanage.py runserver collectstatic
在启动nginx服务器之前。
如果我尝试从浏览器访问 css 文件,它会出现以下错误:
要求:
http://mahbubcseju.com/static/css/home.css
回应:
Not Found The requested resource was not found on this server.
用户 ubuntu 的静态目录权限:
drwxrwxrwx 4 ubuntu ubuntu 4096 年 11 月 7 日 07:50 静态
从 nginx 配置中的位置块中删除 =
,假设已在目录上设置了正确的权限以允许 nginx 用户访问,这应该可以工作:
location /static/ {
# autoindex on;
alias /home/ubuntu/projects/url_shortener/static/;
}