Django在服务器上找不到静态文件

时间:2018-07-17 12:10:10

标签: 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, '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_ROOT文件夹中,但是随后出现404错误。怎么了,我被困住了。

我的sites-available/default

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;
#       }
#}

1 个答案:

答案 0 :(得分:0)

您可能必须告诉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 / {
        ...
    }
}