Nginxは「エンジンエックス」のように発音し、フリーでオープンソースのWebサーバーである。2020年現在においてはシェアを増やしている。Apacheと比較して、高速に動作するよう設計されている。ここでは主にLinux Mint上で動作させるものとして解説する。
端末で以下のコマンドを実行してインストールする。
wget https://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
sudo xed /etc/apt/sources.list
これで表示されたエディタに以下の2行を追加して保存する。
deb http://nginx.org/packages/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/ubuntu/ bionic nginx
apt update
apt install -y nginx
これでnginxが起動するが、VirtualBoxの場合はゲストOSの設定がまだなので外からWebアクセスすることができない。再起動してアクセス可能になる。
インストールするとnginxと名の付いたファイルは以下が作成される。
/etc/nginx/nginx.conf(サービス起動時引数に使われる)
/etc/init.d/nginx
/etc/init.d/nginx-debug
/etc/rc1.d/K(0,1,6)nginx > /etc/init.d/nginx
/etc/rc1.d/K(2~5)nginx > /etc/init.d/nginx
/usr/sbin/nginx
/usr/sbin/nginx-debug
/lib/systemd/system/nginx.service
/lib/systemd/system/nginx-debug.service
/var/run/nginx.pid
/lib/systemd/system/nginx.serviceの内容は以下のとおりである。
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
また、インストールすると /etc/nginx ディレクトリが作成され、そのディレクトリ内に設定ファイルが置かれる。/etc/nginxディレクトリには以下が配置される。
conf.d📁
L default.conf
modules(リンク)
fastcgi_params
koi-utf
koi-win
mime.types
nginx.conf
scgi_params
uwsgi_params
win-utf
default.confの内容は以下のとおりである。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
nginx.confファイルは設定ファイルである。この中の「include /etc/nginx/conf.d/*.conf;」の部分は上のconf.d/default.confを読み込んでいる。
nginx.confを示す。これはMXLinuxのnginx version1.14.2である。
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
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;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 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/*;
}
バージョン確認はnginx -vで確認できる。例を示す。
$nginx -v
nginx version: nginx/1.18.0
さらなる詳細はnginx -Vで確認できる。
$ nginx -V
nginx version: nginx/1.18.0
built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.18.0/debian/debuild-base/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
デフォルト設定ではChromeブラウザから調べることができる。
1.右クリック>検証をクリックする。(Ctrl+Shift+I)
2.Networkタブをクリックし、Ctrl+Rキーを押す。
3.Nameペインの要素をクリックし、Headersタブをクリックする。
4.Respons Headersのサーバーに「nginx/1.18.0」などと表示される。
Xserverの場合は「nginx」と表示される。
nginxのプロセスを確認したい場合はps axuコマンドを実行する。以下のように表示される。
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 994 0.0 0.0 33336 872 ? Ss 02:55 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 995 0.0 0.0 38104 4408 ? S 02:55 0:00 nginx: worker process
外からWebページにアクセスできない(ERR_CONNECTION_REFUSED)
systemctl list-units --type serviceコマンドでサービスの一覧を表示すると、networking.serviceが起動していない。
systemctl status networking.serviceで起動しようとすると、「Failed to start Raise network interfaces.」でエラーが出ている
また、「Cannot find device "eth0"」メッセージもある
/etc/network/interface ファイルをすべてコメントアウトし保存する
リブートし、network(systemctl start network.serviceコマンド)とnginx(systemctl start nginx.serviceコマンド)を起動する。
nginxを起動しようとsudo systemctl restart nginxコマンドを実行したら、
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
エラーが発生した。systemctl status nginxコマンドでは以下のような表示になった。
● nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-07-27 03:41:37 JST; 17s ago
Docs: http://nginx.org/en/docs/
Process: 9629 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 9630 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Main PID: 6777 (code=exited, status=0/SUCCESS)
7月 27 03:41:37 cpageT40 systemd[1]: Starting nginx - high performance web server...
7月 27 03:41:37 cpageT40 nginx[9630]: nginx: [emerg] invalid number of arguments in "sendfile" directive in /etc/nginx/nginx.conf:27
7月 27 03:41:37 cpageT40 systemd[1]: nginx.service: Control process exited, code=exited status=1
7月 27 03:41:37 cpageT40 systemd[1]: nginx.service: Failed with result 'exit-code'.
7月 27 03:41:37 cpageT40 systemd[1]: Failed to start nginx - high performance web server.
/etc/nginx/nginx.confなどの設定ファイルでセミコロンがないなどのエラーがある。「nginx: [emerg] invalid number of arguments in "sendfile" directive in /etc/nginx/nginx.conf:27」とあるので、この場合は/etc/nginx/nginx.confファイルの27行目付近にあると推測できる。