Nginxでベーシック認証をかける方法!
Nginxでベーシック認証を設定するときは、直接、 /etc/nginx/sites-available/default に設定を書く必要があります。
※ Nginxは 「.htaccess」 が使えません。
1. サイト設定ファイルを修正
21-22行目の2行を追加します。"Auth" は、"My Site" などに置き換えても大丈夫です。
# vi /etc/nginx/sites-available/default を修正します。
下記は既に修正済みです。
# You may add here your
server {
listen 80;
server_name example.com;
rewrite ^/(.*) http://www.example.com/$1 permanent;
}
server {
listen 80;
root /home/hoge/example.com/wordpress;
index index.php index.html index.htm;
server_name www.example.com;
access_log /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log;
location / {
auth_basic "Auth";
auth_basic_user_file $document_root/.htpasswd;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?.xml$ "/index.php?xml_sitemap=params=$2" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?.xml.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^.+?($/wp-.*) $1 last;
rewrite ^.+?(/.*.php)$ $1 last;
rewrite ^ /index.php last;
}
}
location /wp-admin {
set $do_not_cache 1;
}
set $do_not_cache 1;
location ~ .php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass_header "X-Accel-Redirect";
fastcgi_pass_header "X-Accel-Expires";
fastcgi_no_cache $do_not_cache;
fastcgi_cache_bypass $do_not_cache;
fastcgi_cache wpcache;
fastcgi_cache_key "$scheme://$host$request_uri";
fastcgi_cache_valid 200 10m;
fastcgi_cache_valid 404 1m;
}
location ~ .(jpg|png|gif|swf|jpeg|ico)$ {
expires 30d;
}
location ~ .(css|js)$ {
charset UTF-8;
expires 30d;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
log_not_found off;
access_log off;
}
location ~ /(.ht|.user.ini|.git|.hg|.bzr|.svn) {
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
今回は、サイト全体がベーシック認証の対象になりますが、
location /wp-admin {の中にベーシック認証の記述を書けば、WordPressの管理画面にベーシック認証を設定することが出来ます。特定のディレクトリだけも可能です。
2. パスワードファイルの作成と設置
パスワードファイルは、Apache の付属ツール htpasswd で作るのがいちばん確実です。Nginx だけの環境でも、apache2-utils パッケージを入れれば使えます(Apache 本体は入りません)。
# apt install apache2-utils
# 1 人目(-c はファイルを新しく作る。既存のファイルがあると上書きされる)
# htpasswd -c /etc/nginx/.htpasswd hoge
# 2 人目以降は -c を付けない
# htpasswd /etc/nginx/.htpasswd fuga
# 権限を絞る(nginx の実行ユーザーが読めればよい)
# chown root:www-data /etc/nginx/.htpasswd
# chmod 640 /etc/nginx/.htpasswd
パスワードはコマンドの実行中に 2 回聞かれます。コマンドの引数に書かないので、シェルの履歴にも残りません。
上の設定例では $document_root/.htpasswd(公開ディレクトリの中)を読んでいますが、置き場所は公開ディレクトリの外にしておくほうが安全です。/etc/nginx/.htpasswd に作った場合は、設定を次のように書き換えます。
auth_basic "Auth";
auth_basic_user_file /etc/nginx/.htpasswd;
公開ディレクトリの中に置く場合は、設定例の最後にある location ~ /(.ht|...) の deny all; を必ず残してください。これが無いと、/.htpasswd をブラウザで開くだけでファイルの中身(ユーザー名とハッシュ)が見えてしまいます。
設定を変えたら、構文を確かめてから読み込み直します。
# nginx -t
# systemctl reload nginx
パスワードファイルは、以前は WEB の生成ツールでも作れましたが、紹介していたツールのサイトは閉鎖されています。パスワードを外部のサイトに入力しないためにも、サーバー上のコマンドで作成してください。
この記事の初版(2014年)では openssl passwd -crypt でパスワードを作っていましたが、-crypt は OpenSSL 3.0 で廃止されたため、現在の Debian・Ubuntu ではエラーになります。htpasswd が使えない環境では、代わりに openssl passwd -apr1 を使います。
$ printf "hoge:%s\n" "$(openssl passwd -apr1)" >> /etc/nginx/.htpasswd
| 項目 | 2014年の初版 | 2026年11月時点 |
|---|---|---|
| パスワードの作り方 | openssl passwd -crypt | htpasswd(apache2-utils)か openssl passwd -apr1 |
| ハッシュの形式 | DES crypt(パスワードは先頭 8 文字だけ有効) | APR1-MD5 か bcrypt(htpasswd -B) |
PHP-FPM のソケット(設定例の fastcgi_pass) | /var/run/php5-fpm.sock | Debian 13 は /run/php/php8.4-fpm.sock、Ubuntu 24.04 は /run/php/php8.3-fpm.sock |
⚠ htpasswd -B(bcrypt)で作ったハッシュを Nginx が照合できるかは、OS の crypt 関数しだいです。Debian 13 の libcrypt(libxcrypt 4.4 系)は bcrypt に対応していますが、古い OS では照合に失敗することがあります。迷ったら APR1-MD5(htpasswd の既定)を使ってください。
これでベーシック認証の設定は完了です!
サイトにアクセスすると認証画面が出るので、設定したユーザー名とパスワードを入力してログインが出来るか試してくださいね~。