16
14

More than 5 years have passed since last update.

Djangoで本番環境にデプロイするときの、staticファイルの扱いについて

Last updated at Posted at 2016-06-14

djangoでローカル環境で作業していたものを本番環境で実行するときに、staticファイルが表示されなくなった。

その対処法。

  • サーバー:nginx
  • django1.9.2
  • python3.5

ルーティング系

nginx.conf
#server{}の中に追記

    location / {
        root /home/hogehoge/hogehoge; #サーバー上でのアプリケーションディレクトリのパス
        proxy_pass http://127.0.0.1:8000;
    }

デプロイの時はこんな感じで設定した。

デバッグモードを切る

settings.pyで、下記の内容を書き直す。

settings.py
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1','任意のDNS']
STATIC_ROOT = "/home/hogehoge/hogehoge/static" #静的ファイルを置きたいパスを指定するみたい
# これに加えて、STATICFILES_DIRS = {…はコメントアウトするっぽい

このままだと、staticfileが非表示になってしまうので、

python manage.py collectstatic

を実行。
その後、nginxで設定を書き換える

nginx.conf
#server{}の中に

location /static/ {
        alias /home/hogehoge/hogehoge/static/; #settings.pyで設定したのと同じ場所を記述
    }

こんな感じで書いておく。以上。

16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14