繁体   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