Python Django can';在服务器上找不到静态文件
所以我在使用Django在服务器上部署静态文件时遇到了这个问题。我正在运行nginx,并成功地在本地测试了我的网站,但是现在,当我在web上部署我的网站时,我无法加载本地文件,尽管我已经按照说明完成了所有操作。所以我一直都犯这个错误Python Django can';在服务器上找不到静态文件,python,django,Python,Django,所以我在使用Django在服务器上部署静态文件时遇到了这个问题。我正在运行nginx,并成功地在本地测试了我的网站,但是现在,当我在web上部署我的网站时,我无法加载本地文件,尽管我已经按照说明完成了所有操作。所以我一直都犯这个错误 "GET /staticold/botadd/anyfile HTTP/1.0" 404 99 我的settings.py静态设置如下所示 STATIC_URL = '/staticold/' STATIC_ROOT = os.path.join(BASE_DIR
"GET /staticold/botadd/anyfile HTTP/1.0" 404 99
我的settings.py
静态设置如下所示
STATIC_URL = '/staticold/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'botadd/static'),
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
我也试过这样做
STATIC_URL = '/staticold/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'botadd/static'),
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
python manage.py collectfiles
命令成功,我的所有文件都被传输到STATIC\u ROOT
文件夹,但随后出现了404错误。那会是什么,我被卡住了
我的站点可用/默认值
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
您可能需要告诉nginx在哪里可以提供静态内容。尝试在nginx站点可用文件中添加“location/static/”块,例如:
server {
server_name <your_server_name>.com;
listen 443;
listen [::]:443;
ssl on;
ssl_certificate /etc/nginx/ssl/cert.crt;
ssl_certificate_key /etc/nginx/ssl/private.key;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
allow all;
alias /your/base/dir/here/.../botadd/static/;
}
location / {
...
}
}
服务器{
server_name.com;
听443;
听[:]:443;
ssl-on;
ssl_certificate/etc/nginx/ssl/cert.crt;
ssl\u证书\u密钥/etc/nginx/ssl/private.key;
location=/favicon.ico{access\u log off;log\u not\u found off;}
位置/静态/{
允许一切;
别名/your/base/dir/here/../botadd/static/;
}
地点/{
...
}
}
您可以添加nginx配置文件和URL。您可以添加两次botadd
:一次在URL中,一次在STATICFILES\u DIRS
中。不确定问题是配置,但这里有问题is@WillemVanOnsem你能说得更具体一点吗?我指的是启用站点中的那个,在那里你可以配置你的应用服务器。您是否让nginx为您的静态和媒体文件提供服务,或者让django像您在本地可能做的那样提供服务?