前提・実現したいこと
Djangoで画像と動画をAWSのS3にアップしたい。
Djangoで画像と動画を投稿できるサイトを作成してます。
無事、AWSにアップしてデプロイが完了しました。
S3の設定をする前まではサイト表示は問題なく表示されていました。
しかし画像と動画をS3にアップさせるところでサイトが表示されなくなってしまいます。
発生している問題・エラーメッセージ
502 Bad Gateway nginx/1.14.0 (Ubuntu)
以下はsettings.pyです。
import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '×××××××××××××××××××××××××××××××××××××××××××××' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False ALLOWED_HOSTS = ['motuni.xyz'] # Application definition INSTALLED_APPS = [ 'accounts.apps.AccountsConfig', 'videos.apps.VideosConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'storages', ] 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', ] # Djangoのキャッシュにセッションデータを保存 # SESSION_ENGINE = 'django.contrib.sessions.backends.cache' ROOT_URLCONF = 'project4.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_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 = 'project4.wsgi.application' # ログイン後のページを指定する LOGIN_REDIRECT_URL = '/' # Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': '××××', 'USER': '×××××', 'PASSWORD': '×××××', 'HOST': '×××××', 'PORT': '', } } # Password validation # https://docs.djangoproject.com/en/2.2/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/2.2/topics/i18n/ LANGUAGE_CODE = 'ja' TIME_ZONE = 'Asia/Tokyo' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.2/howto/static-files/ ###↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓恐らく以下の設定のどこかが間違っている↓↓↓↓↓↓↓↓↓↓↓########################################################## #STATIC_URL = '/static/' #STATICFILES_DIRS = [ # os.path.join(BASE_DIR, 'static') #] # メディアファイル関連 #MEDIA_URL = '/media/' #MEDIA_ROOT = os.path.join(BASE_DIR, 'media') LOGIN_URL = 'login' # 共通の設定 AWS_ACCESS_KEY_ID = '×××××××' AWS_SECRET_ACCESS_KEY = '××××××××' AWS_STORAGE_BUCKET_NAME = 'motuni' AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400', } AWS_LOCATION = 'static' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION) STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' DEFAULT_FILE_STORAGE = 'project4.backends.MediaStorage' ```ここに言語名を入力 Python Django 以下はurls.pyです。
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from storages.backends.s3boto import S3BotoStorage
urlpatterns = [
path('admin/', admin.site.urls),
path('videos/', include('videos.urls')),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', RedirectView.as_view(url='/videos/')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media')
以下はbackends.pyです。backends.pyはsettings.pyと同じ階層(ディレクトリ)に置いてあります。
from storages.backends.s3boto3 import S3Boto3Storage
class MediaStorage(S3Boto3Storage):
location = 'media' # /media というURLで配信
file_overwrite = False # 同名ファイルは上書きせずに似た名前のファイルに
### 試したこと 主に以下のサイトを参考にAWSのS3の設定とDjango側の設定をしました。 [参考サイト](https://narito.ninja/blog/detail/27/) ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。 以下はnginxログ(ログの一番下)の抜粋です。 error.log
2019/10/25 11:43:50 [crit] 3863#3863: *1 connect() to unix:/home/ubuntu/project4/project4.sock failed (2: No such file or directory) while connecting to upstream, client: IPアドレス, server: motuni.xyz, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/project4/project4.sock:/", host: "motuni.xyz"
error.log.1
2019/10/25 06:00:53 [crit] 978#978: *15 connect() to unix:/home/ubuntu/project4/project4.sock failed (2: No such file or directory) while connecting to upstream, client: 52.41.225.107, server: motuni.xyz, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/project4/project4.sock:/", host: "motuni.xyz"
access.log
IPアドレス - - [25/Oct/2019:11:43:50 +0000] "GET / HTTP/1.1" 502 584 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
access.log.1
159.203.201.77 - - [25/Oct/2019:05:07:03 +0000] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 zgrab/0.x"
52.41.225.107 - - [25/Oct/2019:06:00:53 +0000] "GET / HTTP/1.1" 502 182 "-" "Go-http-client/1.1"
問題がある可能性があるための補足です。 sudo pip install django-storages コマンドを打つと以下のエラーが出ました。
WARNING: The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: django-storages in /usr/local/lib/python3.6/dist-packages (1.7.2)
Requirement already satisfied: Django>=1.11 in /usr/local/lib/python3.6/dist-packages (from django-storages) (2.2.6)
Requirement already satisfied: sqlparse in /usr/local/lib/python3.6/dist-packages (from Django>=1.11->django-storages) (0.3.0)
Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from Django>=1.11->django-storages) (2019.3)
sudo pip install boto3 コマンドを打つと以下の同じようなコードが出ます。
WARNING: The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: boto3 in /usr/local/lib/python3.6/dist-packages (1.10.1)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /usr/local/lib/python3.6/dist-packages (from boto3) (0.9.4)
Requirement already satisfied: botocore<1.14.0,>=1.13.1 in /usr/local/lib/python3.6/dist-packages (from boto3) (1.13.1)
Requirement already satisfied: s3transfer<0.3.0,>=0.2.0 in /usr/local/lib/python3.6/dist-packages (from boto3) (0.2.1)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1; python_version >= "2.7" in /usr/local/lib/python3.6/dist-packages (from botocore<1.14.0,>=1.13.1->boto3) (2.8.0)
Requirement already satisfied: urllib3<1.26,>=1.20; python_version >= "3.4" in /usr/lib/python3/dist-packages (from botocore<1.14.0,>=1.13.1->boto3) (1.22)
Requirement already satisfied: docutils<0.16,>=0.10 in /usr/local/lib/python3.6/dist-packages (from botocore<1.14.0,>=1.13.1->boto3) (0.15.2)
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil<3.0.0,>=2.1; python_version >= "2.7"->botocore<1.14.0,>=1.13.1->boto3) (1.11.0)
回答2件
あなたの回答
tips
プレビュー