メモ > サーバ > 構築: Webサーバ設定 > nginx
nginx
※Apacheの代替になり得るとされているWebサーバ。
※Apacheよりも大量のアクセスを捌くことができるとされている。
※Basic認証やリダイレクトの方法がApacheと同じではないので注意。
nginxの設定ファイルに記述する必要があり、SSHで設定再読み込みやミドルウェア再起動が発生する。
NginxとApacheって何が違うの?? - Qiita
https://qiita.com/hiroaki-u/items/f2455d62f8a4017663cb
Re: NginxとApacheって何が違うの?? - inductor's blog
https://blog.inductor.me/entry/2022/05/31/150707
Apacheからnginxへ移行する際に気になったことメモ - Qiita
https://qiita.com/nayuneko/items/a3b326e2e9466e4117d6
Nginxとは?Apacheとの違いについてエンジニアに聞いてみた
https://academy.gmocloud.com/qa/20160616/2761
EC2(Amazon Linux)に Nginx をインストールしてBasic認証する
http://dev.classmethod.jp/etc/amazon_linux_nginx_basic_auth/
Nginxセキュリティ設定
http://qiita.com/hideji2/items/1421f9bff2a97a5e5794
nginxのパラメータチューニングとh2o
http://qiita.com/cubicdaiya/items/235777dc401ec419b14e
Nginx導入時やること - Qiita
https://qiita.com/kidach1/items/985efebba639713c562e
Nginxのアーキテクチャを理解する #nginx - Qiita
https://qiita.com/kamihork/items/296ee689a8d48c2bebcd
令和にふりかえる C10K 問題
https://8-p.info/ja/c10k.html
C10K 問題、実は理解していない | blog.ojisan.io
https://blog.ojisan.io/c10k-wakaran/
シングルプロセスのNginxがマルチプロセスのApacheより高速という意味について考えてみた
https://zenn.dev/tmikada/articles/performance-c10kproblem
nginxの設定ファイル nginx.conf の読み方 超入門 - 或る阿呆の記
https://hack-le.com/nginx/
Nginx設定のまとめ - Qiita
https://qiita.com/syou007/items/3e2d410bbe65a364b603
nginxについてまとめ(設定編) - Qiita
https://qiita.com/morrr/items/7c97f0d2e46f7a8ec967
nginx.confが読めるようになる | 魔法使いの卵
http://raichel.tech/web/read-nginx-conf
nginxでは、.htaccessを使用できない。
設定の変更にはnginxの設定ファイル調整が伴うので注意。(FTP経由で調整を…というわけにはいかない。)
Apacheのように: .htaccess | NGINX
https://mogile.web.fc2.com/nginx_wiki/start/topics/examples/likeapache-htaccess/
nginxに.htaccessを移行する。 - Qiita
https://qiita.com/rdonster/items/a49b1861003645e7054b
Nginxなはずのエックスサーバーで.htaccessが有効な理由 | けーちゃんの個人サイト
https://affi-sapo.com/1264/
htaccessファイルはnginxのに変換
https://winginx.com/ja/htaccess
なおApache+PHP-FPMでも.htaccessでのPHP設定が行えないようだが、
対象のディレクトリ内に .user.ini というファイルを作成することで、PHPの設定ができるみたい。(未検証。)
PHP-FPM 環境で .htaccess に php_value は書ける? - mattintosh note
https://mattintosh.hatenablog.com/entry/20200605/1591359561
PHP-FPM 環境で .htaccess に php_value を使用したら 500 Internal Server Error - Crieit
https://crieit.net/posts/php-fpm-php-value-error-20211124
■インストール
※Amazon Linux 2にインストールする場合の手順は後述。
# yum -y install nginx … nginxをインストール。
# vi /etc/nginx/nginx.conf … nginxの設定ファイルを編集。
アクセスログとエラーログは以下の場所にある。
/var/log/nginx/access.log
/var/log/nginx/error.log
ログフォーマットも /etc/nginx/nginx.conf で変更できる。以下は変更例。
http {
server_tokens off; … 追加。レスポンスにミドルウェア名とバージョンを含めないようにする。
# service nginx start … nginxを起動。
# chkconfig nginx on … nginxの自動起動を設定。
# nginx -v … nginxのインストールを確認。
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
↓以下のように変更
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $http_x_forwarded_for $http_x_forwarded_proto';
ELB経由のnginxでアクセス元IPアドレスをアクセスログの$remote_addrとして書くようにする - Qiita
https://qiita.com/toshihirock/items/1de2613bb7c95d486dc1
nginx: ログ出力形式の変更と、ログにレスポンスタイムを出力する方法 - エラーの向こうへ
https://tech.mktime.com/entry/259
■インストール(Amazon Linux 2 の場合)
Amazon Linux 2 の場合、Extrasリポジトリからインストールする必要がある。
# amazon-linux-extras list
# amazon-linux-extras install nginx1.12 -y
# systemctl start nginx
# systemctl enable nginx
AmazonLinux2にNginxを導入する - Qiita
https://qiita.com/ashbrain/items/4de51ff33e4bddf1278b
■ファイヤーウォール設定(iptablesを使用している場合の設定)
# vi /etc/sysconfig/iptables … ファイヤーウォールを設定。(80ポートを開ける。)
■動作確認
#HTTPを許可
-A MY-FIREWALL -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables restart … ファイヤーウォールを再起動。
# vi /usr/share/nginx/html/index.html … テストページ作成。(もとのファイルを上書き。)
http://refirio.net/ のようにブラウザからアクセスを確認。
■公開ディレクトリの変更
公開ディレクトリを変更したい場合、以下の設定を行う。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>テスト</title>
</head>
<body>
<p>テスト。</p>
</body>
</html>
# vi /etc/nginx/nginx.conf … nginxの設定ファイルを編集。
※設定ファイルの構文チェックは、/etc/nginx/conf.d/https.conf などinclude対象のファイルをチェックすると文法エラーになる。
/etc/nginx/nginx.conf など、大元のファイルをチェックする必要がある。
■ロードバランサーとして使う
以下にある「nginxをロードバランサーにする」を参照。
Etcetera.txt server {
listen 80 default_server;
listen [::]:80 default_server;
#server_name localhost;
server_name refirio.net; … 必要に応じてサーバー名を指定。
#root /usr/share/nginx/html;
root /var/www/html; … 公開ディレクトリを変更。
# mkdir -p /var/www/html/ … 公開用ディレクトリを作成。(存在しない場合。)
# chown nginx. /var/www/ … 公開用ディレクトリの所有者をnginxに変更。
# chmod 775 /var/www/ … nginxグループのユーザに読み書き実行を許可。
# chown nginx. /var/www/html/ … ドキュメントルートの所有者を「nginx」に変更。
# chmod 775 /var/www/html/ … nginxグループのユーザに読み書き実行を許可。
# ll /var/www/ … ドキュメントルートの所有者を確認。
合計 0
drwxrwxr-x 2 nginx nginx 6 2月 3 13:58 html
# vi /var/www/html/index.html … トップページを作成。(適当な内容を記述しておく。)
# service nginx configtest … 設定ファイルの構文をチェック。(「nginx -t -c /etc/nginx/nginx.conf」でも可。)
# service nginx restart … nginxを再起動。