我是部署專案的新手,我的靜態檔案不與 nginx 一起使用。
那是我的站點可用/myprject 檔案
server{
listen 80;
server_name mydomain;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
autoindex on;
alias /home/user/project/static;
}
location /media/ {
autoindex on;
alias /home/user/project/media;
}
location / {
proxy_pass myIp:myPort;
}
}
我的靜態檔案和媒體有這個路徑:
/home/user/project/staict files and media files
這就是我的 settings.py 配置的外觀
STATIC_URL = '/static/'
STATIC_ROOT =os.path.join(BASE_DIR,'static')
我的除錯變數是假的
我運行collectstatic。
uj5u.com熱心網友回復:
在您的 settings.py 中嘗試以下操作:
STATIC_URL = '/staticfiles/'
STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]
uj5u.com熱心網友回復:
這是我看到的與我作業的 Django 網站不同的地方:
server_name mydomain;```
should be->
```listen 80;
server_name myIP;```
```alias /home/user/project/static;```
should be ->
```alias /home/user/project/static/;```
```location / {
proxy_pass myIp:myPort;```
should be ->
```locatinon /{
include proxy_params;
proxy_pass http://myIp:myPort; ```
then
```STATIC_URL = '/static/'
STATIC_ROOT =os.path.join(BASE_DIR,'static')```
should be ->
```STATIC_URL = '/static/'
STATIC_ROOT = /home/user/project/static;
STATICFIES_DIRS = [os.path.join(BASE_DIR, 'static/'),
]```
This is the way my website is setup and it is working totally fine. However. if you change your static files, you will need to rerun "collecstatic."
uj5u.com熱心網友回復:
我這樣解決了這個問題:
首先,您必須在 urls.py 中添加您的 urlpatterns
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在 /var/www/html/static 中創建目錄
sudo chmod -R 777 /var/www/html/static
運行 collectstatic
在站點可用/專案中設定您的位置(您的 nginx 配置)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511190.html
下一篇:nginx位置塊不回傳靜態內容
