static を S3 から自サーバに戻す

2023/02 の作業


いろいろな理由で、static ファイルを S3 から自サーバに戻す。

media は S3 のままにしておく。


static の向き先を自サーバに変更

(work_venv) $ vi prod.py
#AWS_LOCATION = 'static'
#STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
#STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'


自サーバに static を集める

(work_venv) $ python manage.py collectstatic --settings worksite.settings.prod
Found another file with the destination path 'colorful/arrow.gif'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'colorful/colorPicker.css'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

479 static files copied to '/home/work_user/worksite/static'.


NGINX の設定を変更

$ sudo vi /etc/nginx/sites-available/worksite_nginx.conf
    ・・・・
    types_hash_max_size 4096;

    gzip on;
    gzip_types text/plain text/xml text/css text/javascript application/javascript application/x-javascript application/json application/xml application/xml+rss application/octet-stream;

    include             /etc/nginx/mime.types;
    ・・・・
    location /static {
        # s3 に移動したので来ない
        alias /home/work_user/worksite/static; # collectstatic した static ディレクトリ
        expires 14d;
    }
    ・・・・


gzip_types に text/html があると、ワーニングがでるので、入れなくて良い。

$ sudo nginx -t
nginx: [warn] duplicate MIME type "text/html" in /etc/nginx/sites-available/worksite_nginx.conf:15
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


nginx ユーザが static を見れるか確認

$ sudo su -s /bin/bash - nginx -c "cat /home/wagtanen/vifsite/static/website/css/custom.css"


サービス再起動

$ sudo systemctl restart supervisord.service
$ sudo systemctl restart nginx.service



動作確認

$ curl -I -H 'Accept-Encoding: gzip,deflate' https://www.massolezume.ga/static/puput/js/jquery.min.js
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 28 Feb 2023 07:56:57 GMT
Content-Type: application/javascript; charset=utf-8
    ・・・・
Content-Encoding: gzip


変更した通り動いている。