Apache VS Nginx
Nginxは軽量と聞いたので、低スペックサーバーであるラズパイはこっちの方が向いてるんじゃないかと思ってNginxを選択。
また、Nginxの方が難易度が高いらしいので勉強になるんじゃないかと考えた。
Nginx導入
# apt install -y nginx
バージョンは1.18.0でした。
# nginx -V
nginx version: nginx/1.18.0
デフォルトで自動起動設定。
# systemctl is-enabled nginx
enabled
インストールしたらすぐ起動する。
# 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 Tue 2023-01-03 16:39:34 JST; 5min ago
Docs: man:nginx(8)
Process: 3393 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 3394 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 3529 (nginx)
Tasks: 5 (limit: 1596)
CPU: 169ms
CGroup: /system.slice/nginx.service
├─3529 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─3532 nginx: worker process
├─3533 nginx: worker process
├─3534 nginx: worker process
└─3535 nginx: worker process
1月 03 16:39:34 raspberrypi systemd[1]: Starting A high performance web server and a reverse proxy server...
1月 03 16:39:34 raspberrypi systemd[1]: Started A high performance web server and a reverse proxy server.
設定ファイルを確認
Nginxの設定ファイルはいくつかある。
まずはNginxの基本設定が記載されているファイルを見てみる。
less
やview
コマンドで、/etc/nginx/nginx.conf
を開いてみる。
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
〜〜〜省略!!!〜〜〜
}
設定 | 説明 |
---|---|
user | workerプロセス所有者の設定 |
worker_processes | workerプロセスの数(autoを指定するとCPUのコア数で設定される) |
pid | PIDの番号を設定(catで中身見れます) |
include |
/etc/nginx/modules-enabled/ にある.confファイルをすべて読み込む |
events | 接続に関する設定を記述する |
worker_connections | workerプロセスの同時接続最大数 |
(参考)Nginxのプロセスを確認
# ps aux | grep nginx
root 3529 0.0 0.8 49524 7912 ? S 16:39 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 3532 0.0 0.2 49660 2524 ? S 16:39 0:00 nginx: worker process
www-data 3533 0.0 0.2 49660 2524 ? S 16:39 0:00 nginx: worker process
www-data 3534 0.0 0.2 49660 2524 ? S 16:39 0:00 nginx: worker process
www-data 3535 0.0 0.2 49660 2524 ? S 16:39 0:00 nginx: worker process
root 4500 0.0 0.0 4024 548 pts/4 S+ 16:53 0:00 grep nginx
4コアなので4プロセス動いてる
# lscpu
Architecture: armv7l
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 4
〜〜〜省略!!!〜〜〜
PID:3529でプロセス起動していることが確認できる
# cat /run/nginx.pid
3529
httpコンテキスト(Basic Settings)
Basic Settingsの部分だけ解説していく。
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
〜〜〜省略!!!〜〜〜
}
設定 | 説明 |
---|---|
http | httpコンテキスト。webサーバーに関する設定を入れていく。 |
sendfile | ファイル読み込み時にsendfileシステムコールを使用するかどうかの設定。onにすると高速化されるようなのでon。 |
tcp_nopush | onにすることで、レスポンスヘッダとファイルの内容を極力ひとまとめにして送信してくれるらしい。 |
types_hash_max_size | ハッシュテーブルの最大サイズを設定。ファイル拡張子からMIMEタイプを効率よく取得できるようになるらしい。 |
default_type |
mime.types に、MIMEタイプと拡張子との紐付けが設定にない場合に、指定されるMIMEタイプを設定。 |
(参考)MIMEタイプ設定ファイル
MIMEタイプ(text/htmlなど)と、拡張子(htmlなど)との紐付きを記述している。
# cat /etc/nginx/mime.types
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
〜〜〜省略!!!〜〜〜
}
httpコンテキスト(SSL Settings以降)
SSL Settings以降を解説していく。
http {
〜〜〜省略!!!〜〜〜
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
設定 | 説明 |
---|---|
ssl_protocols | 指定されたSSL/TLSプロトコルを有効にする設定 |
ssl_prefer_server_ciphers | サーバー側が指定した暗号化スイートを優先的に使用する設定。onにしないとクライアント側の好きな暗号化スイートが使用されるという脆弱性になるので基本的にon。 |
access_log | アクセスログの保存先 |
error_log | エラーログの保存先 |
gzip | 送信するデータをgzip圧縮させるかどうかの設定。onにすると圧縮する不可は増えるが通信量は減る。 |
gzip_types | gzip圧縮するMIMEタイプを設定する。デフォルトでオフになっていた。text/htmlは設定書かなくても圧縮してくれるらしい。 |
暗号化スイートのスイートって、組み合わせって意味なのね。sweetじゃなくて、suite。
サーバーの設定
nginx.conf
に、include /etc/nginx/sites-enabled/*;
という記述があった。
/etc/nginx/sites-enabled/default
を読み込んでるようである。(このディレクトリにはdefaultというファイルしかなかった)
ファイルの中を開いてみたところ、このファイルにサーバー設定が記述されているようであった。
ちなみに、include /etc/nginx/conf.d/*.conf;
でも.confファイルを読み込む設定になっているが、デフォルトだと何もファイルが存在しなかった。
less /etc/nginx/sites-enabled/default
で開く。
ほとんどコメントアウトされているが、serverコンテキスト内にサーバーの設定が記述されている。
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
設定 | 説明 |
---|---|
listen | 待ち受けポートを設定。default_server を設定すると、デフォルトのwebサーバーになる。ssl を指定すると、SSL/TSLを使用する。ポートを省略すると80番ポートを使用する。[::]と書いてあるのはIPv6の設定。 |
root | ルートディレクトリの設定。 |
index | ファイル名を省略できる設定。例えばindex.html を設定しておけば、http://fqdn/index.html と書かなくても、http://fqdn/ でアクセスできるようになる。 |
server_name | サーバーの名前を記述する。デフォルトは_ になっている。スペース区切りで複数指定できる。 |
location | アクセス制御に関する設定を記述していく。 |
try_files | 左から順にファイルを探してきて、あれば表示する。$uri/ =404 をつけると、ファイルがなかった場合に404を返す。 |
ファイアウォールを開け忘れずに!
あれ?http://fqdn/
で接続できない!?
あ・・・そういえばさっきufwの設定でsshポート以外閉じてたんだった。
# ufw allow http
Rule added
Rule added (v6)
これでデフォルトページが表示された。
index index.html index.htm index.nginx-debian.html;
の設定で、index.nginx-debian.html
は記述を省略できる。
ルートディレクトリに、index.nginx-debian.html
があるので、このファイルが表示される。
# ls /var/www/html/
index.nginx-debian.html
とりあえずここまで
デフォルト設定を読み解くところまで進めた。あとは設定をしていくだけ。
今はまだhtmlを表示するくらいしかできないので、phpを処理できるようにしてWordPressでも導入して遊びたい。