【问题标题】:Static Files not Displayed after Deployment to Google Cloud Run [closed]部署到 Google Cloud Run 后未显示静态文件 [关闭]
【发布时间】:2020-11-04 12:18:26
【问题描述】:

我有一个 wagtail web 应用程序,它在 localhost 中完美运行,但在生产中,Debug 设置为 False,并且所有静态文件都未显示在部署的网站中,我在下面附上 Dockerfile 代码:

    # Use an official Python runtime as a parent image
FROM python:3.7
LABEL maintainer="hello@wagtail.io"

# Set environment varibles
ENV PYTHONUNBUFFERED 1
ENV DJANGO_ENV production

COPY ./requirements.txt /code/requirements.txt
RUN pip install --upgrade pip
# Install any needed packages specified in requirements.txt
RUN pip install -r /code/requirements.txt
RUN pip install gunicorn

# Copy the current directory contents into the container at /code/
COPY . /code/
# Set the working directory to /code/
WORKDIR /code/

RUN python manage.py migrate

RUN useradd wagtail
RUN chown -R wagtail /code
USER wagtail

EXPOSE 8000
CMD exec gunicorn myweb_blog.wsgi:application --bind 0.0.0.0:80 --workers 3

Production.py 设置文件如下:

    from .base import *


SECURE_BROWSER_XSS_FILTER=True

FEATURE_POLICY = {
    'geolocation': 'none',

    
    }

#CSP_BLOCK_ALL_MIXED_CONTENT=True
#

        


SECURE_CONTENT_TYPE_NOSNIFF=True
SECURE_FRAME_DENY=True
SECURE_HSTS_SECONDS=2592000
SECURE_HSTS_INCLUDE_SUBDOMAINS=True
SECURE_HSTS_PRELOAD=True
X_FRAME_OPTIONS = 'DENY'
SECURE_REFERRER_POLICY='same-origin'


SECRET_KEY = 'A Secure Secret'
ALLOWED_HOSTS = ['my domain'] 
DEBUG = False

try:
    from .local import *
except ImportError:
    pass

--Base.py--

"""
Django settings for myweb_blog project.

Generated by 'django-admin startproject' using Django 3.0.4.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/


# Application definition

INSTALLED_APPS = [
    'home',
    'search',
    'blog',
    'wagtail.contrib.forms',
    'wagtail.contrib.redirects',
    'wagtail.embeds',
    'wagtail.sites',
    'wagtail.users',
    'wagtail.snippets',
    'wagtail.documents',
    'wagtail.images',
    'wagtail.search',
    'wagtail.admin',
    'wagtail.core',

    'modelcluster',
    'taggit',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',

    'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]



ROOT_URLCONF = 'myweb_blog.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_DIR, 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]







WSGI_APPLICATION = 'myweb_blog.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    #Database details is filled here
}







# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, 'static'),
]

# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# Javascript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
# See https://docs.djangoproject.com/en/3.0/ref/contrib/staticfiles/#manifeststaticfilesstorage
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'


# Wagtail settings

WAGTAIL_SITE_NAME = "myweb_blog"

# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = 'my domain'

请帮助我找到让我的网站正常运行的解决方案。非常感谢

--更新--

问题已解决感谢正确遵循云运行文档和白噪声(http://whitenoise.evans.io/en/stable/django.html)

【问题讨论】:

    标签: django google-cloud-platform gunicorn wagtail google-cloud-run


    【解决方案1】:

    我推荐Whitenoise 用于静态文件。

    https://github.com/GoogleCloudPlatform/django-demo-app-unicodex 是在 Google Cloud Run 上运行 Django(以及 Wagtail)的绝佳起点。它包括一个section on configuring Cloud Storage Buckets

    【讨论】:

    • tomd 非常感谢您的帮助。我能够让它运行。再次感谢。
    • @ManavSengupta 您能否让 Whitenoise 使用 Cloud Run 压缩和提供静态内容?我用 App Engine 尝试过,但没有成功。
    • @franciscojavierarceo 我为 Cloud Run 提出了上述问题,但我使用 App 引擎很长时间并且不需要白噪声,有一个简单(且推荐)的修复方法,只需添加:处理程序:- url: /static static_dir: static/ 在你的 app.yaml 中。我的 app.yaml 看起来像:ibb.co/87CjcR1
    【解决方案2】:

    Django 不是为在生产中提供静态和媒体文件而构建的 Deploying Static Files

    由于您已经在使用 Google Cloud Run,因此解决问题的一个简单方法是通过 Cloud Storage buckets 等 Google Cloud 选项提供静态文件

    或者,您可以在 AWS (Amazon Web Services) 上设置一个免费帐户并使用我发现要容易得多的 S3 存储桶。

    这是Serving Django Media Files on AWS的快速参考指南

    【讨论】:

    • 使用云存储桶完美无瑕!此外,它似乎是根据 youtube [youtu.be/9kfIvlMi798] 上在云运行中托管 django 网站的谷歌云平台频道上的最新视频提供静态文件的推荐方法。顺便感谢您的建议,但我现在已经很好地适应了谷歌云平台并且已经熟悉它,但仍然感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2018-10-17
    • 2021-12-28
    • 1970-01-01
    • 2022-10-24
    相关资源
    最近更新 更多