タイトルどおり。herokuでLaravel,nginxを使い、Basic認証をかけるまでの手順
前提として、github→CircleCI→herokuという流れを作っている。
環境構築は以下を参照。
LaravelアプリをCircleCIを使ってherokuにデプロイ
手順
herokuやLaravelの準備
このあたりはLaravelアプリをCircleCIを使ってherokuにデプロイでやったので割愛
Procfileを編集
web: vendor/bin/heroku-php-nginx -C nginx_app.conf public/
nginx_app.confを作成
nginx_app.confをプロジェクトルートに作成
nginx_app.conf
location / {
# try to serve file directly, fallback to rewrite
try_files $uri @rewriteapp;
}
location @rewriteapp {
# rewrite all to index.php
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
try_files @heroku-fcgi @heroku-fcgi;
internal;
}
basic認証
環境変数を設定
heroku login
heroku config:set BASIC_AUTH_USERNAME=[ユーザ名]
heroku config:set BASIC_AUTH_PASSWORD=[パスワード]
nginx_app.confを修正
nginx_app.conf
location / {
# try to serve file directly, fallback to rewrite
try_files $uri @rewriteapp;
auth_basic "Restricted";
auth_basic_user_file $document_root/../.htpasswd;
}
.htpasswdを作成する
mkdir etc/nginx
mkdir .profile.d
cd .profile.d
vi gen-htpasswd.sh
gen-htpasswd.sh
#!/usr/bin/env bash
set -ex
echo -e "${BASIC_AUTH_USERNAME}:$(perl -le 'print crypt($ENV{"BASIC_AUTH_PASSWORD"}, rand(0xffffffff));')" > /app/.htpasswd
参考
https://devcenter.heroku.com/articles/custom-php-settings
https://mistymagich.wordpress.com/2016/02/12/nginx-basic-authentication-in-php-on-heroku/