AMIMOTO on AWS の WordPress サイトに basic 認証をかける方法

目的

開発中などにかけておく、クローラから拾われたくない時など

作業

/etc/nginx/.htpasswd; というファイルを生成する

$ cd /etc/nginx

USER PASSWORD は任意でつける

$ printf "USER:$(openssl passwd -crypt PASSWORD)\n" | sudo tee .htpasswd

nginx の設定ファイルを編集する

$ sudo vim /etc/nginx/conf.d/default.conf
server {
    listen      80 default;
    server_name _;
    root        /var/www/vhosts/i-fc4c39e5;
    index       index.php index.html index.htm;
    charset     utf-8;

    access_log  /var/log/nginx/i-fc4c39e5.access.log  main;
    error_log   /var/log/nginx/i-fc4c39e5.error.log;

    include     /etc/nginx/drop;

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    #rewrite ^(.*)(index|home|default)\.html? $1 permanent;

    set $mobile '';
    #include /etc/nginx/mobile-detect;

    set $do_not_cache 0;
    if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
        set $do_not_cache 1;
    }
    if ($request_method != GET) {
        set $do_not_cache 1;
    }

    #location ~* \.(js|css|html?|xml|gz|jpe?g|gif|png|swf|wmv|flv|ico)$ {
    #    index   index.html index.htm;
    #    include /etc/nginx/expires;
    #}

    location / {
        try_files $uri @wordpress;

        include /etc/nginx/expires;

        # Pass all .php files onto a php-fpm/php-fcgi server.
        location ~ \.php$ {
            include /etc/nginx/php-fpm;
        }
        # basic認証をかける (今回はここ)
        auth_basic "admin only";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }


    location @wordpress {
        proxy_no_cache     $do_not_cache;
        proxy_cache_bypass $do_not_cache;

        proxy_redirect     off;
        proxy_cache        czone;
        proxy_cache_key    "$scheme://$host$request_uri$mobile";
        proxy_cache_valid  200 0m;
        proxy_pass http://backend;
    }

    #
    # When you use phpMyAdmin, uncomment the line "include /etc/nginx/phpmyadmin;"
    # and delete or comment out the below line "location ~* /(phpmyadmin|myadmin|pma) { }".
    #
    #include     /etc/nginx/phpmyadmin;
    location ~* /(phpmyadmin|myadmin|pma) {
        access_log off;
        log_not_found off;
        return 404;
    }

    #
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

nginx の設定を再起動させて反映させる

$ sudo service nginx restart