簡體   English   中英

盡管日志文件顯示已成功獲取圖像,但 Django 生產服務器不顯示圖像

[英]Django production server not displaying images although log file shows images fetched successfully

我正在使用 Django 3.2

我能夠在本地服務器上正確顯示圖像(DEBUG=True)。 我現在正在嘗試使用 nginx + gunicorn 在生產服務器上進行部署。

這是我的settings.py中的相關部分

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    # ...
    ]

MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'myproj/staticfiles')
STATIC_URL = "/static/"

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, 'myproj/static'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

我正在關注這個在線文檔該文檔顯示了如何使用 gunicorn + nginx +letsencrypt 設置 Django。

我進行了如下操作:

  1. 在生產服務器上運行python manage.py collectstatic
  2. 使用ufw allow 8000在生產服務器上打開端口ufw allow 8000
  3. 運行: python manage.py runserver 0.0.0.0:8000 ,我得到以下控制台輸出:

正在執行系統檢查...

System check identified no issues (0 silenced).
November 10, 2021 - 02:22:01
Django version 3.2.3, using settings 'myproj.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[10/Nov/2021 02:22:06] "GET / HTTP/1.1" 200 16536
[10/Nov/2021 02:22:06] "GET /static/img/photos/photo_01.jpg HTTP/1.1" 200 13914
[10/Nov/2021 02:22:06,531] - Broken pipe from ('1.2.3.4', 53238)
[10/Nov/2021 02:22:07] "GET /static/img/favicon.svg HTTP/1.1" 200 13914

但是,當我查看瀏覽器時,沒有顯示圖像。

這是什么原因造成的,我該如何解決? (注意: Broken pipe from ...輸出中Broken pipe from ...

[[編輯]]

以下是開發人員工具的 Network 選項卡的一些輸出:

Request URL: http://myproject.com:8000/static/img/photos/some_random_image_05.jpg
Request Method: GET
Status Code: 200 OK
Remote Address: XX.XX.XX.XX:8000
Referrer Policy: strict-origin-when-cross-origin
Content-Length: 13914
Content-Type: text/html; charset=utf-8
Date: Wed, 10 Nov 2021 08:58:27 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.10
Vary: Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Connection: keep-alive
Cookie: csrftoken=WrcVyF2wW9fsDqSTz1QChDuPwCp7ocmWik51T5oFtxZtablzYEYVzf3d5oiZyjsq
dnt: 1
Host: myproject.com:8000
Referer: http://myproject.com:8000/
sec-gpc: 1
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Mobile Safari/537.36

嘗試使用 whitenoise 包

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM