gpt4 book ai didi

Django app + nginx fail to server static files(Django app+nginx无法为静态文件提供服务器)

转载 作者:bug小助手 更新时间:2023-10-24 23:19:47 131 4
gpt4 key购买 nike



I have a verry unusual problem, none of my static files are loaded 404 is thrown. To note, I use my production environment with DEBUG=False set so the static files are entirely served by the nginx server.

我有一个非常不寻常的问题,我的静态文件都没有加载,404被抛出。请注意,我使用的生产环境设置了DEBUG=FALSE,因此静态文件完全由nginx服务器提供服务。




settings/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"


Please note that missing / between my static root /home/apps/myapp/static and application static files grappelli/stylesheets/mueller/grid/output.css in this example.

请注意,在本例中,我的静态根目录/home/apps/myapp/Static和应用程序静态文件Grappelli/style heets/mueller/grid/output.css之间缺少/。



Does anyone have a clue regarding why this happens and how to fix it?

有谁知道为什么会发生这种情况,以及如何修复它?



UPDATE (SOLUTION)

更新(解决方案)



OK, the problem is that Django's static files are served by default from www.myapp.com/static/ folder so there is no reason specifying the /static/ folder in the nginx configuration file. So if anyone else has the same problem, just remove the following part from nginx/sites-enabled/myapp file:

好吧,问题是Django的静态文件默认是从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;
}

更多回答

Just to clarify - myapp.log is Nginx log file?

只是想澄清一下-myapp.log是Nginx日志文件吗?

Yes, it is the nginx log file. However figured it out, will post an update with a solution.

是的,它是nginx日志文件。无论如何,都会发布更新并提供解决方案。

优秀答案推荐

OK, the problem is that Django's static files are served by default from www.myapp.com/static/ folder so there is no reason specifying the /static/ folder in the nginx configuration file. So if anyone else has the same problem, just remove the following part from nginx/sites-enabled/myapp file:

好的,问题是Django的静态文件默认从www.myapp.com/静态/文件夹提供,所以没有理由在nginx配置文件中指定/Static/文件夹。因此,如果其他任何人有同样的问题,只需从nginx/Sites-Enable/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;
}


Have you ran python manage.py collectstatic on your server?

您是否在您的服务器上运行了pythonManage.py收集器?



Make sure the file is in the static root directory.

确保该文件位于静态根目录中。



1 - Make sure your main urls.py contains static & media routes, it should look like this:

1-确保您的主urls.py包含静态和媒体路由,它应该如下所示:


$ 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 - Make sure STATIC_ROOT, STATIC_URL, MEDIA_ROOT, MEDIA_URL are defined in your settings.py :

2-确保在settings.py中定义了STATIC_ROOT、STATIC_URL、MEDIA_ROOT和MEDIA_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 - Make sure your static and media paths are correct for Nginx:

3-确保您的静态路径和媒体路径对于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 - Make sure your statics were generated properly:

4-确保您的静态数据已正确生成:


python3 manage.py collectstatic

5 - Make sure your static and media are accessible for Nginx:

5-确保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 - Restart Nginx:

6 -重启Nginx:


$ sudo systemctl restart nginx

Good Luck !

祝你好运!


更多回答

131 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com