Skip to content

Instantly share code, notes, and snippets.

@yasainet
Last active January 21, 2021 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasainet/c8f61cb25ff59dd3349e to your computer and use it in GitHub Desktop.
Save yasainet/c8f61cb25ff59dd3349e to your computer and use it in GitHub Desktop.
ubuntu nginx basic認証を特定のパスにかける
  1. */etc/nginx/*に移動する
  2. 出力先のディレクトリオーナーがrootになっているのでteeコマンドを使ってリダイレクトする

$ cd /etc/nginx/

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

  1. .confファイルに.htpasswdへのパスを指定
  2. BASIC認証をかけるパスを指定する

$ cd /etc/nginx/sites-available

$ sudo vim hoge.com.conf

server {
    listen       80;
    server_name  hoge.com;
    root /var/www/hoge.com/root/htdocs/;
    index index.html index.php;
    access_log      /var/log/nginx/hoge.com.access.log;
    error_log       /var/log/nginx/hoge.com.error.log;

    location / {
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php?$uri&$args;
    }

    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;
    }

    location /PATH/ {
      auth_basic "admin only";
      auth_basic_user_file /etc/nginx/.htpasswd;
    }
}
  1. nginxを再起動して設定を反映させる

$ sudo service nginx restart

終わり

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment