AWSにデプロイする過程で、つまづいて「nginx」って何?っていう状態から、nginxについてざっと調べてみました。
nginxとは
nginxはapacheのようなwebサーバー。
特徴
- 高速
- 大量処理が得意
- Webサイト利用を向上させる機能が豊富
- 設定は意外に容易
デメリットとなる特徴
- 大量の動的コンテンツの処理に不向き
- 機能追加のしやすさ
- 初心者向けの設定情報の少なさ
一般設定
設定ファイルはモジュール単位で記述する。
モジュール = nginxの構成単位
モジュール名 …説明
- core …プロセス制御、設定ファイル制御、ロギング
- event …イベント処理(パフォーマンス・チューニング)
- http …webサーバ関連の設定
- mail …メール関連の設定
http{
# httpモジュールの設定
}
# coreモジュールの設定は括る必要ない
ディレクティブは他のディレクティブを内包しうる。
ディレクティブ = 命令
http{
server{
・・・
}
}
コンテキスト = モジュールあるいはディレクティブが作るスコープであり、{ }で囲まれた範囲を指す。
coreモジュールのコンテキスト(一番外側)はmainコンテキストと呼ぶ。
webサーバ設定
基本設定
- listen によりIPとポートを指定する
- server_nameによりサーバ名を指定する
- server_name_in_redirectによりリダイレクト時のサーバ名を指定できる
- rootによりドキュメントルートを指定する
- charsetによりレスポンスヘッダのContent-typeを指定する
http{
# listenはポートのみ指定でも可
listen 80;
server_name www.example.com;
root /var/www/html/www.example.com;
charset UTF-8;
}
仮想サーバを構築する
ドメインベース
- serverを復数記述することで、仮想サーバとして動作する
・http内に記述できるディレクティブは、だいたいserver内にも記述できる
http{
server{
server_name alpha.example.com;
}
server{
server_name bravo.example.com;
}
}
IPベース
- listenで別個のIPを指定することで、IPベースの仮想サーバを構築できる
http{
server{
listen 192.168.0.1:80;
server_name alpha.example.com;
}
server{
listen 192.168.0.2:80;
server_name bravo.example.com;
}
}
エイリアスを指定する
- locationにより、パスごと個別のドキュメントルートを設定可能
http{
server{
location /foo{
root /path/to/foo;
}
location /bar{
root /path/to/bar;
}
}
}
indexファイルはindexで指定できる
http{
index index.html index.html;
}
nginxの主要コマンド
起動
$ nginx
通常停止
$ nginx -s quit
高速停止
$ nginx -s stop
再起動
$ nginx -s reload
設定ファイルのチェック
$ nginx -t
バージョンと詳細設定の確認
$ nginx -V
ヘルプの表示
$ nginx -h
webサーバーの起動と起動確認
コンソールから「nginx」コマンドで起動後、ブラウザからlocalhostにアクセスする。
$ sudo nginx
http://localhost
「Welcome to nginx!」が表示されれば成功。
【CentOS7でWebページが表示されない場合】
Firewalldの設定でHTTP通信がデフォルトでは許可されていない。
以下にてHTTP通信を許可することができる。
「/etc/firewalld/zones/public.xml」に設定されている。
サービスにhttpを追加する。
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="dhcpv6-client"/>
<service name="ssh"/>
<service name="http"/>
</zone>
Firewalldを再起動。
$ sudo systemctl restart firewalld
nginxの主要ファイル
nginx.conf
HTTPなどアクセス関連の設定ファイル。
\conf\nginx.conf
/usr/local/etc/nginx/nginx.conf
/etc/nginx/nginx.conf
mime.types
MIME Typeの設定ファイル。
\conf\mime.types
/usr/local/etc/nginx/mime.types
/etc/nginx/mime.types
設定ファイル管理ディレクトリ
サーバ関連の設定ファイルが格納されたディレクトリ。
なし
なし
/etc/nginx/conf.d/
default.conf
デフォルトの設定ファイル。
※「\conf\nginx.conf」に記載
※「/usr/local/etc/nginx/nginx.conf」に記載
/etc/nginx/conf.d/default.conf
server {
listen 8 0;
server_name localhost;
・・・
location / {
root /usr/share/nginx/html;
・・・
}
待ち受けポート番号を変更したい場合はlistenの値を変更する。
ドキュメントルートを変更したい場合はrootの値を変更する。
ドキュメントルートディレクトリ
Webコンテンツの格納ディレクトリ。
\html\
/usr/local/var/www/
/usr/share/nginx/html/
nginx.pid
\logs\nginx.pid
/usr/local/var/run/nginx.pid
/var/run/nginx.pid
access.log
\logs\access.log
/usr/local/var/log/nginx/access.log
/var/log/nginx/access.log
error.log
\logs\error.log
/usr/local/var/log/nginx/error.log
/var/log/nginx/error.log
参考サイト
https://qiita.com/morrr/items/7c97f0d2e46f7a8ec967
https://www.yaz.co.jp/tec-blog/web%E3%82%B5%E3%83%BC%E3%83%93%E3%82%B9/263
https://www.wakuwakubank.com/posts/611-web-server-nginx/
https://heartbeats.jp/hbblog/2012/04/nginx04.html
https://tengine.taobao.org/nginx_docs/ja/docs/http/request_processing.html