created: 2024-02-26T01:52:47.600Z

最小限の設定で nginx の設定を動作確認する環境

nginx の設定をいじることがある。あんまりないけどときどきある。

こういうのは docker でやるほうがいいのかなと思ってイメージとかボリュームの設定とかみていたけど、よく考えたら nginx を mac に入れればいいだけなので mac 上で動かすことにした。nginx のデフォルトの設定ファイルとかも気にしないでよい。

docker でやるならたぶん compose の watch とかをいじればいいんじゃないでしょうか。

コード

最小限なのでどれも十行程度で済む。

nginx.conf

worker_processes  1;
error_log /dev/stdout info;

events {
    worker_connections  16;
}

http {
    access_log /dev/stdout;

    server {
        listen 8000;
        location / {
            proxy_pass http://localhost:3000;
        }
    }
}

index.js

const express = require("express");
const app = express();
app.get("/sleep", (req, res) => {
  setTimeout(() => res.send("Sleepy!"), 2000);
});
app.get("/", (req, res) => {
  res.send("Hello World!");
});
app.listen(3000);

pm2(ecosystem.config.js)

これははじめて触るのでここが一番面倒だった。プロセス管理で面倒なら docker-compose を使ったほうがよかったけど。まあ。

module.exports = {
  apps: [
    {
      name: "nginx",
      script: "/opt/homebrew/bin/nginx",
      args: "-g 'daemon off;' -c /Users/path/services/rfp-tools/nginx-try/nginx.conf",
    },
    {
      name: "express",
      script: "/Users/path/.nodebrew/current/bin/node",
      args: "./index.js",
    },
  ],
};

実行

$ npm install express pm2
$ npx pm2 --no-daemon start ./ecosystem.config.js --watch
アレ何? 大事典
[ad] アレ何? 大事典
佐々木 正孝, 篠崎 晃一 (単行本)