ただのメモ書きです。
- 環境
- Ubuntu 18.04.3 LTS
- Vagrant 2.2.5
Install
sudo apt install nginx
インストールするとすぐ動く
curl localhost
Config
path
/etc/nginx/nginx.conf
Configuration File’s Structure
文法は以下のような感じで、events, worker_connections とかが "directive"
events {
worker_connections 768;
# comment
}
どの {} にも囲まれていない directive は main {} の中となる。
main, events, http, server, location directives は特殊で、以下のような包含関係にある。
main {
events {
}
http {
server {
location {
}
}
}
}
Simple server
静的 HTML, image を返す簡単なサーバー設定をしてみる。
- ファイルを置く
- デフォルトの設定の無効化
- 設定ファイル作成
- 設定ファイル読み込み
1. ファイルを置く
mkdir -p /var/www/my/html
echo hello > /var/www/my/html/index.html
mkdir -p /var/www/my/images
# put some images in /var/www/my/images
2. デフォルトの設定の無効化
実態ファイルが /etc/nginx/sites-available にあり、/etc/nginx/sites-enabled にシンボリックリンクを貼ることで設定を有効化するしくみにしているみたい。
デフォルトの設定が /etc/nginx/sites-available/default にあるので無効化
unlink /etc/nginx/sites-available/default
3. 設定ファイル作成
こんな感じ
$ cat /etc/nginx/sites-available/myconf
server {
location / {
root /var/www/my/html;
}
location /images/ {
root /var/www/my;
}
}
server で port を指定しないと 80 ポート設定になる。
4. 設定ファイル読み込み
systemctl reload nginx
Proxy server
server {
listen 8080;
root /data/up1;
location / {
}
}
server 直下に root があるが、この root は location で root 指定がない場合に適用される。
複数の location directive が定義されていた場合の適用順は、
"最長マッチ" -> "正規表現"
Command
systemctl
sudo systemctl start nginx
sudo systemctl reload nginx
sudo systemctl enable nginx
nginx process operation
nginx -s stop # fast shutdown
nginx -s quit # shutdown
nginx -s reload # reloading the configuration file
nginx -s reopen # reopening the log files
設定出力
複数ファイルを include している場合も、これで全部出力できる。
ただし、ファイルを gathering しているだけで、オンメモリの設定を読んでいるわけではない。
nginx -T