1.はじめに
Webサーバーnginxを普段使いする機会が増えてきたので、頻繁に行う内容を覚え書きしてます。
(随時追記)
試行環境
- Ubuntu Linux 22.04
$ uname -a Linux hostname 5.19.0-42-generic #43~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Apr 21 16:51:08 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
2.インストール
$ sudo apt install nginx -y
# 依存しているパッケージを調べる $ apt-cache depends nginx nginx |依存: nginx-core |依存: nginx-full |依存: nginx-light 依存: nginx-extras |依存: nginx-core |依存: nginx-full |依存: nginx-light 依存: nginx-extras 破壊: <libnginx-mod-http-lua>
主な関連ファイル・ディレクトリの場所
| ディレクトリ | ファイル | 目的 |
|---|---|---|
| /etc/nginx/ | nginx.conf | nginx自体の設定 |
| /var/www/html | *.htmlなど | Webコンテンツの置き場 |
| /etc/nginx/sites-available/ | default | /var/www/htmlの表示等の設定 |
| /etc/nginx/sites-enabled/ | default | 上記へのシンボリックリンク※ |
| /var/log/nginx/ | access.log | ログ・正常 |
| /var/log/nginx/ | error.log | ログ・異常 |
(※このシンボリックリンクの内容が実際の運用に適用される)
3.設定のいろいろ
(1)書き換えた設定の反映
# 構文チェック $ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # 設定の反映 $ sudo systemctl reload nginx # 確認 $ systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2023-05-20 23:31:26 JST; 2 days ago Docs: man:nginx(8) Process: 118846 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 118847 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 121854 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS) Main PID: 118939 (nginx) Tasks: 5 (limit: 38132) Memory: 5.8M CPU: 146ms CGroup: /system.slice/nginx.service ├─118939 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" (…省略…)
以降は、設定ファイル/etc/nginx/sites-available/defaultに記述する。
(2)Index of ... を表示する
Apacheのようにファイル一覧を表示する場合は、下記の記述を追加。
location /hoge/ {
autoindex on;
autoindex_localtime on;
}
locationのなかに記載する。(具体的な書き方は後述します)
(3)特定のディレクトリを公開する
公開するディレクトリは、通常は下記の位置に指定。
server {
root /var/www/html;
異なるディレクトリを、個別に公開する際は、下記のように指定します。
(例:/home/share/を公開)
server {
listen 80 default_server;
listen [::]:80 default_server;
(…省略…)
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
############################## ここを追記
# /home/share/ディレクトリを公開、ファイル一覧表示を有効化
location /share/ {
alias /home/share/;
autoindex on;
autoindex_localtime on;
}
(…省略…)
(4)文字化け防止
文字コードを強制指定します。
設定ファイル/etc/nginx/sites-available/defaultの先頭のほうに下記を記述。
(server {の記述よりも前に書くこと)
(…省略…)
charset UTF-8;
charset_types text/css text/plain application/javascript;
server {
(…省略…)
4.保全
$ journalctl -xeu nginx.service (…省略…) 5月 29 13:37:41 dicom-sv11 systemd[1]: Reloading A high performance web server and a reverse proxy server... ░░ Subject: A reload job for unit nginx.service has begun execution ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ A reload job for unit nginx.service has begun execution. ░░ ░░ The job identifier is 22703. 5月 29 13:37:41 dicom-sv11 systemd[1]: Reloaded A high performance web server and a reverse proxy server. ░░ Subject: A reload job for unit nginx.service has finished ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ A reload job for unit nginx.service has finished. ░░ ░░ The job identifier is 22703 and the job result is done.