nginxの設定メモ(作成中)

 2025.10.28  Debian trixie

webサーバソフト nginx(えんじんえっくす)の設定方法のメモ

1.インストール

$ sudo apt-get install  nginx


2. ユーザディレクトリ /home/*/public_html/の公開


nginxでは/etc/nginx/sites-available/defaultに記述したあと、reloadすれば 有効となる


例/etc/nginx/sites-available/default
server {
    listen 80;
    server_name trixie.local;

    location ~ ^/~([^/]+)(.*)$ {
        alias /home/$1/public_html$2;
        autoindex on;
    }
}

上の設定ファイルをチェックする方法
$ sudo nginx -t


上の設定ファイルで
server_name に記述されたアドレスにアクセスすると
rootで記述された /home/tako/public_html/下のindex.htmlが表示される。

もしLan内でipアドレス(192.168.1.204)でアクセスで直接アクセスされるとき反応できるようにするには
次のようなファイルを作る

/etc/nginx/conf.d/192.168.1.204.conf
server {
        listen 80;
        listen [::]:80;

        server_name 192.168.1.204;

        root /home/tako/public_html;
        index index.html;

        location / {
                try_files $uri $uri/ =404;
        }
}

3. basic認証の設定

 平文でパスワード認証をもとめるページを追加する場合、パスワードを設定する。
$ cd /home/tako
$ sudo htpasswd -c .htpasswd   tako
$ sudo chmod 600 .htpasswd
  これでユーザーtakoのパスワードファイルが/home/tako/.htpasswdに作成され る。

  もし  例えば/home/tako/public_html/webcamera 下のアクセスをベーシック認証する場合、/etc/nginx/conf.d下の
設定ファイルに次のように記述を追加する。

/etc/nginx/conf.d/tako.confに赤い部分を追加
server {
    listen 80;
    listen [::]:80;

    server_name www.hogehoge.co.jp;

    root /home/karappi/public_html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
   #basic認証のページ
    location /webcamera/ {
            auth_basic "Restricted";
            auth_basic_user_file /home/tako/.htpasswd;
    }


}


4. PerlのCGIを動かす

(1)FastCGIをインストールする。

# apt-get install fcgiwrap

(2)/etc/nginx/conf.d/tako.confに赤い部分を追加

server {
    listen 80;
    listen [::]:80;

    server_name www.hogehoge.co.jp;

    root /home/karappi/public_html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
   #basic認証のページ+追加でCGIも動くようにする
    location /webcamera/ {
            auth_basic "Restricted";
            auth_basic_user_file /home/tako/.htpasswd;
           fastcgi_pass unix:/var/run/fcgiwrap.socket;
           fastcgi_index index.cgi;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
        }


}

 もしcgiがログファイル等を書き出すときは、ログファイルのオーナーとグループをwww-dataにしてパーミションを
chmod で644にしておく
# chown www-data   counter.log
# chgrp www-data   counter.log
# chmod 644  counter.log




5 アンインストール


 Debian jessie でnginxをアンインストールしてもnginxは消えない。次のように手動で消す。
# apt-get remove --purge nginx
# rm -i /etc/init.d/nginx
# rm -fr /usr/share/nginx