前提・実現したいこと
djangoアプリをAWS EC2にデプロイ(Nginx, gunicorn, postgresql)しました。
しかし、CSS,JSなどは反映されておらず、staticファイルが読み込めていない状況です。
発生している問題・エラーメッセージ
Chromeのデベロッパーツールのconsoleのところに、以下のメッセージが表示されました。
エラーメッセージ GET http://52.194.250.14:8000/static/myapp/css/main.css net::ERR_ABORTED 404 (Not Found) GET http://52.194.250.14:8000/static/myapp/js/main.js net::ERR_ABORTED 404 (Not Found)
該当のソースコード
構造は以下です(不要そうなところは省略してあります)。
myproject
├─myapp
│ ├─static
│ │ └─myapp
│ │ ├─css
│ │ │ └─main.css
│ │ └─js
│ │ └─main.js
│ └─templates
│ └─myapp
│ └─base.html
├─myproject
| └─settings.py
│ └─wsgi.py
│ └─urls.py
└─manage.py
●settings.py
python
1 2DEBUG = False 3 4ALLOWED_HOSTS = ['52.194.250.14'] 5 6STATICFILES_DIRS = [os.path.join(BASE_DIR, 'myapp/static'),] 7STATIC_URL = '/static/' 8STATIC_ROOT = os.path.join(BASE_DIR, 'static') 9
●/etc/nginx/nginx.conf
server { listen 80; server_name 52.194.250.14; client_max_body_size 6G; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://127.0.0.1:8000; } location /static { alias /home/es2-user/myproject/static; }
●css読み込み (base.html)
html
1<link rel="stylesheet" href="{% static 'myapp/css/main.css' %}">
試したこと
主に以下の2つのページを見て、設定しました。
■AWSにDjangoアプリケーションをデプロイ(Nginx, gunicorn, postgresql)
https://qiita.com/pokotsun/items/1272479e36c5146c6609
■Djangoで本番環境にデプロイするときの、staticファイルの扱いについて
https://qiita.com/usodamasijp/items/2d5e9040548f7b52f5e8
・manage.py collectstatic はしてあります。
・nginx.conf を設定後、nginx の再起動もしてあります。
・gunicorn myproject.wsgi --bind=0.0.0.0:8000 でwsgiと接続しました。
以下のページを見て、urls.pyに下記のコードを追加しました。
■Djangoで画像が表示されない(staticエラー)
https://qiita.com/tn1117/items/6f8d8e1039a2e8ed2b5e
●myproject/myproject/urls.py
python
1urlpatterns += static(settings.STATIC_URL, 2 document_root=settings.STATIC_ROOT)
以下のページを見て、python manage.py findstatic . を実行したところ、
https://www.ganbaruyo.net/details/static-http-404-error/
ーーーーーーーーーーーーーーーーーーーーーーーーーー
/myproject/myapp/static
/usr/bin/.pyenv/versions/3.6.6/lib/python3.6/site-packages/django/contrib/admin/static
/usr/bin/.pyenv/versions/3.6.6/lib/python3.6/site-packages/nested_inline/static
/myproject/myapp/static
ーーーーーーーーーーーーーーーーーーーーーーーーーー
と出てきました。しかし、何が原因なのがよく分かりません。
以上となります。
原因がお分かりになる方、何卒宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー