我想在 Linux vps 上部署我的 Django 项目,但我有一个问题,问题是我的 nginx 拒绝打开静态文件的权限我发现这个错误 在 error.log 文件中:
open() "/home/sam/testdjango/static/admin/js/nav_sidebar.js" failed (13: Permission denied>)
这是我的 nginx.conf 文件:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/jav>
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
N/B:我登录的用户是“sam”。
这是我的 nginx 站点 - 可用:
server {
listen 80;
server_name 185.8.174.180;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sam/testdjango;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
我在gunicorn服务中的用户:'sam'和组:'www-data' 我的目录的权限:
total 140
-rw-r--r-- 1 sam sam 131072 Apr 25 21:54 db.sqlite3
-rwxrwxr-x 1 sam sam 666 Apr 25 11:11 manage.py
drwxrwxrwx 3 sam www-data 4096 Apr 25 13:16 static (this is my statics` directory) i think its 777 code
drwxrwxr-x 3 sam sam 4096 Apr 25 21:05 testdjango
如果我没记错的话,这就是全部,如果你们帮助我,我将不胜感激,Ty <3
您必须授予所有文件权限,但还有一种更好的方法来管理静态文件。在 django 设置中,您可以指定 STATIC_ROOT 和 STATICFILES_DIRS。第一个是您想要存储静态文件的位置。最好将其放在 /var/www/html 目录中,因为 nginx 应该能够访问该目录。在 STATICFILES_DIRS 中,您指定包含静态文件的所有目录。您还必须更改 nginx 配置中的静态文件位置。在你的情况下,它会是这样的
location /static/ {
root /var/www/html;
}
在您的设置文件中您应该添加以下内容:
STATIC_ROOT = "/var/www/html"
STATICFILES_DIRS =[put there all directories with static files from root]