TransWikia.com

staticfiles в django + nginx + gunicorn + vps

Stack Overflow на русском Asked by Kirill on January 11, 2021

У меня есть django проект, который я не могу развернуть на сервере
staticfiles не загружаются…
в Гугл Консоли для разработчиков: Status code at "Network": 403 Forbidden к моим staticfiles

/etc/nginx/sites-enabled/default

server {
    listen 80;
    server_name <site_domain>;
    access_log  /var/log/nginx/example.log;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /root/code/3p_site_new/;
    }

    location /media/ {
        root /root/code/3p_site_new/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

/root/code/3p_site_new/gunicorn_config.py

command = '/root/code/3p_site_new'
pythonpath = '/root/code/3p_site_new/3p_site_new'
bind = '127.0.0.1:8001'
workers = 3
user = 'root'
limit_request_fields = 32000
limit_request_field_size = 0
raw_env = 'DJANGO_SETTINGS_MODULE=Site.settings'

Я пытают запустить сайт с помощью команды gunicorn -c "/root/code/3p_site_new/gunicorn_config.py" Site.wsgi

tree:

/root/code/3p_site_new/*
|-- Main
|   |-- __init__.py
|   |-- admin.py
|   |-- apps.py
|   |-- creds.json
|   |-- forms.py
|   |-- migrations
|   |   |-- <files>
|   |-- models.py
|   |-- my_config.py
|   |-- static
|   |   |-- css
|   |   |   |-- <files>
|   |   |-- js
|   |   |   |-- <files>
|   |   `-- media
|   |       `-- img
|   |           |-- <files>
|   |-- templates
|   |   |-- <files>
|   |-- tests.py
|   |-- urls.py
|   `-- views.py
|-- Site
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- db.sqlite3
|-- gunicorn_config.py
|-- manage.py
|-- requirements.txt
`-- static
    |-- admin
    |   |-- css
    |   |   |-- <files>
    |-- css
    |   |-- <files>
    |-- js
    |   |-- <files>
    `-- media
        `-- img
            |-- <files>

/root/code/3p_site_new/Site/settings.py

import os
from Main import my_config

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

SECRET_KEY = '<my_secret_key>'
DEBUG = False

ALLOWED_HOSTS = ['<my_domain>', '<my_vps_ip>', '127.0.0.1', '0.0.0.0']

INSTALLED_APPS = [
    'Main',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'social_django',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
]

ROOT_URLCONF = 'Site.urls'

# https://casesup.com/category/knowledgebase/howtos/python-django-handling-custom-error-page
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, ],
        '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',
                'social_django.context_processors.backends',  # Добавил эту строку
            ],
        },
    },
]

WSGI_APPLICATION = 'Site.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

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',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/Moscow'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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

AUTHENTICATION_BACKENDS = (
    'social_core.backends.vk.VKOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'home'
LOGOUT_URL = 'logout'
LOGOUT_REDIRECT_URL = 'login'

SOCIAL_AUTH_VK_OAUTH2_KEY = my_config.VK_AUTH_APP_ID
SOCIAL_AUTH_VK_OAUTH2_SECRET = my_config.VK_AUTH_SECRET_KEY

SOCIAL_AUTH_VK_OAUTH2_SCOPE = ['notify', 'groups', 'offline']

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'myformatter': {
            'format': '{levelname} {asctime} {module} {message}',
            'style': '{',
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'myformatter',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO',
    },
}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP