Skip to content

Instantly share code, notes, and snippets.

@wakita
Last active May 29, 2022 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakita/61ef305269133487547d1dc74976a84b to your computer and use it in GitHub Desktop.
Save wakita/61ef305269133487547d1dc74976a84b to your computer and use it in GitHub Desktop.
nginx のインストールと初期設定 (macOS 用): install.sh を実行するだけ。docroot は ~/Sites
#!/bin/sh
# nginx について、研究室の scrapbox に書きました。
# https://scrapbox.io/smartnovax/nginx#60b5828d93b2e9000065e3e4
brew list nginx >& /dev/null || (echo "nginx が見つからないのでインストールします。"; brew install nginx)
# nginx の設定ファイルのなかの環境依存の部分を envsubst コマンドで変換したい。
# Homebrew の gettext パッケージは envsubst コマンドを含んでいる。
brew list gettext >& /dev/null || (echo "envsubst コマンドが見つからないので、gettext をインストールします。"; brew install gettext)
# nginx.conf.template 中の環境変数を置換したものを nginx.conf に保存
envsubst < server.conf.template '$HOME' > server.conf
echo "あなたの環境にカスタマイズしたサーバー設定ファイル (server.conf) を作成しました。"
# このディレクトリにある server.conf を nginx サーバの設定に追加
echo
echo "include `pwd`/server.conf;" > `brew --prefix`/etc/nginx/servers/server.conf
echo "以下のサーバ設定を"`brew --prefix`"/etc/nginx/servers/server.conf に保存しました。"
cat `brew --prefix`"/etc/nginx/servers/server.conf"
echo
# nginx の設定を確認(設定内容に問題があったらエラーが表示される)
nginx -t
# サービスの設定ファイルに追加登録される (`$HOME/Library/LaunchAgents/homebrew.mxcl.nginx.plist`)
# サービスに登録。brew services list コマンドで状況を確認できる。
brew services list | grep nginx || brew services start nginx
server {
# 80番は http のデフォルトポート。
# 以下の設定で http://localhost/ で自分のウェブサーバにアクセスできるようになる。
# ほかの番号を設定した場合は http://localhost:<listen の番号>/ でアクセスするこになる。
listen 80;
index index.html;
location / {
# root においたコンテンツが http://localhost:80/ で見られる
root ${HOME}/Sites;
allow 127.0.0.1;
# ローカルネットワークからのアクセスを許可
allow 192.168.2.0/24;
deny all;
# nginx のキャッシュ機能を無効化
# Disable cache: https://stackoverflow.com/questions/40243633
add_header Last-Modified $date_gmt;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
if_modified_since off;
expires off;
etag off;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment