Django app + nginx无法服务静态文件

pu3pd22g  于 2023-10-17  发布在  Nginx
关注(0)|答案(3)|浏览(157)

我有一个非常不寻常的问题,没有我的静态文件加载404抛出。需要注意的是,我使用的生产环境设置了DEBUG=False,因此静态文件完全由nginx服务器提供服务。
设置/production.py

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
DEBUG = TEMPLATE_DEBUG = False

nginx/sites-enabled/myapp

server {
    listen   80; ## listen for ipv4; this line is default and implied
    server_name myapp.com;
    access_log /var/log/nginx/myapp.log;
    error_log /var/log/nginx/myapp.log;

    root /home/apps/myapp/;

    location / {
            gzip_static on;
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:3031;
    }

    location /static/ { # STATIC_URL
            alias /home/apps/myapp/static/; # STATIC_ROOT
            expires 30d;
    }
    location /media/ { # MEDIA_URL
            alias /home/apps/myapp/media/; # MEDIA_ROOT
            expires 30d;
    }
}

myapp.log

2014/04/08 04:20:11 [error] 22178#0: *590 open() "/home/apps/myapp/staticgrappelli/stylesheets/mueller/grid/output.css" failed (2: No such file or directory), client: 77.89.196.6, server: myapp.com, request: "GET /static/grappelli/stylesheets/mueller/grid/output.css HTTP/1.1", host: "myapp.com", referrer: "http://myapp.com/admin/"
77.89.196.6 - - [08/Apr/2014:04:20:11 +0200] "GET /static/grappelli/stylesheets/mueller/grid/output.css HTTP/1.1" 404 200 "http://myapp.com/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36"

请注意,在这个例子中,我的静态根/home/apps/myapp/static和应用程序静态文件grappelli/stylesheets/mueller/grid/output.css之间缺少/
有人知道为什么会发生这种情况以及如何解决它吗?

更新(解决方案)

好的,问题是Django's静态文件默认从www.myapp.com/static/文件夹提供,因此没有理由在nginx配置文件中指定/static/文件夹。所以如果其他人也有同样的问题,只需从nginx/sites-enabled/myapp文件中删除以下部分:

location /static/ { # STATIC_URL
        alias /home/apps/myapp/static/; # STATIC_ROOT
            expires 30d;
        }
        location /media/ { # MEDIA_URL
            alias /home/apps/myapp/media/; # MEDIA_ROOT
            expires 30d;
        }
h9a6wy2h

h9a6wy2h1#

好的,问题是Django's静态文件默认从www.myapp.com/static/文件夹提供,因此没有理由在nginx配置文件中指定/static/文件夹。因此,如果其他人也有同样的问题,只需从nginx/sites-enabled/myapp文件中删除以下部分:

location /static/ { # STATIC_URL
        alias /home/apps/myapp/static/; # STATIC_ROOT
            expires 30d;
        }
        location /media/ { # MEDIA_URL
            alias /home/apps/myapp/media/; # MEDIA_ROOT
            expires 30d;
        }
5vf7fwbs

5vf7fwbs2#

你在你的服务器上运行过pythonmanage.py collectstatic吗?
确保文件位于静态根目录中。

roejwanj

roejwanj3#

1 -确保您的主urls.py包含staticmedia路由,它应该看起来像这样:

$ nano /home/myuser/apps/myapp/urls.py
----
urlpatterns = [
    path('admin/', admin.site.urls),
    # here goes your other routes ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

2 -确保settings.py中定义了STATIC_ROOTSTATIC_URLMEDIA_ROOTMEDIA_URL

$ nano `/home/myuser/apps/myapp/settings.py`
----
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = 'static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'

3 -确保您的staticmedia路径对于Nginx是正确的:

`$ sudo nano /etc/nginx/sites-available/myapp`
----
# STATIC_URL
location /static/ { 
    alias /home/myuser/apps/myapp/static/;
    expires 30d;
}

# MEDIA_URL
location /media/ { 
    alias /home/myuser/apps/myapp/media/; # MEDIA_ROOT
    expires 30d;
}

4 -确保正确生成statics

python3 manage.py collectstatic

5 -确保您的staticmedia可用于Nginx:

$ cd myproject
$ sudo chown -R www-data:www-data /home/myuser/apps/myapp/media/
$ sudo chown -R www-data:www-data /home/myuser/apps/myapp/static/
$ sudo chmod -R 755 /home/myuser/apps/myapp/media/
$ sudo chmod -R 755 /home/myuser/apps/myapp/static/

6 -重启Nginx:

$ sudo systemctl restart nginx

祝你好运!

相关问题