Nginxインストール後の共通機能の設定方法

王林
リリース: 2023-05-15 21:19:11
転載
1099 人が閲覧しました

1. 本体設定ファイルと仮想ホストの分離

仮想ホストの数が多い場合、機能やサービスごとに分けることもできますので、分けておくと便利です。例として 2 つの仮想ホストを取り上げます。

空白行とコメントを削除して構成ファイルを完成させます:

[root@nginx-01 conf]# egrep -v "#|^$" nginx.conf.bak 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
ログイン後にコピー

/app/nginx/conf ディレクトリの下に仮想ホスト構成ディレクトリを作成します

mkdir extra
ログイン後にコピー

サーバー モジュールを使用して、 www と bbs の 2 つの仮想サイトを作成します

[root@nginx-01 conf]# cat -n nginx.conf
[root@nginx-01 conf]# sed -n '10,20p' nginx.conf
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
ログイン後にコピー

www サイト

[root@nginx-01 conf]# cat extra/www.conf 
    server {
        listen       80;
        server_name  www.yygg.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
ログイン後にコピー

bbs サイト

[root@nginx-01 conf]# cat extra/bbs.conf 
    server {
        listen       80;
        server_name  bbs.yygg.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html/bbs;
        }
    }
ログイン後にコピー

メイン設定ファイルの設定 (nginx.conf)

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
include extra/www.conf;
include extra/bbs.conf;
}
ログイン後にコピー

チェック設定

[root@nginx-01 conf]# /app/nginx/sbin/nginx -t
nginx: the configuration file /app/nginx-1.18.0//conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.18.0//conf/nginx.conf test is successful
ログイン後にコピー

サイト ディレクトリの作成

[root@nginx-01 conf]# mkdir /app/nginx/html/{www,bbs}
[root@nginx-01 conf]# echo "http://www.yygg.com" >>/app/nginx/html/www/index.html
[root@nginx-01 conf]# echo "http://bbs.yygg.com" >>/app/nginx/html/bbs/index.html
[root@nginx-01 conf]# echo "192.168.1.5 www.yygg.com bbs.yygg.com" >>/etc/hosts
ログイン後にコピー

サービスを開始してテスト

[root@nginx-01 conf]# /app/nginx/sbin/nginx
[root@nginx-01 conf]# curl www.yygg.com
http://www.yygg.com
[root@nginx-01 conf]# curl bbs.yygg.com
http://bbs.yygg.com
ログイン後にコピー

2.仮想ホスト エイリアス設定

www サイトを例に挙げます。エイリアスを設定します
いわゆるエイリアスとは、メインのドメイン名

www.yygg.com

に加えて 1 つ以上の追加のドメイン名 を設定し、エイリアス # を設定することです。 ##yygg.com

[root@nginx-01 conf]# cat extra/www.conf 
    server {
        listen       80;
        server_name  www.yygg.com yygg.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html/www;
        }
    }
ログイン後にコピー

Restart nginx test

[root@nginx-01 conf]# /app/nginx/sbin/nginx -s reload
[root@nginx-01 conf]# cat /etc/hosts
192.168.1.5 www.yygg.com bbs.yygg.com yygg.com
[root@nginx-01 conf]# curl yygg.com
http://www.yygg.com
ログイン後にコピー

3.Nginx ステータスステータス情報の設定

ステータス情報の記録は `ngx_http_stub_status_module` モジュールを使用して実装されます

Input

/app/nginx/sbin/nginx -V上記のモジュールがコンパイルに含まれているかどうかを確認します:

nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/app/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module
ログイン後にコピー

ステータス仮想ホストを作成します。方法についてはタイトル 1 を参照してください。

status .confConfiguration ファイルは次のとおりです:

    server {
        listen       80;
        server_name  status.yygg.com;
        location / {
            stub_status on;
            access_log off;
        }
    }
ログイン後にコピー

メイン構成ファイル

nginx.confステータス仮想ホスト構成を追加

sed -i '11 i include extra/status.conf;' nginx.conf
ログイン後にコピー

構文を確認してnginxを再起動

/app/nginx/sbin/nginx -t
/app/nginx/sbin/nginx -s reload
ログイン後にコピー

ホスト解像度の構成

192.168.1.5 status.yygg.com

アクセス

status.yygg.com表示

[root@nginx-01 conf]# curl status.yygg.com
Active connections: 1 
server accepts handled requests
 4 4 4 
Reading: 0 Writing: 1 Waiting: 0
ログイン後にコピー
表示結果分析:

アクティブな接続: 1 ##処理中の接続の数は 1です

server ##合計 4 つの接続が処理されました
accepts ##合計 4ハンドシェイクが作成されました
処理されたリクエスト ##合計 4 つのプロセスが処理されました リクエスト
Reading ##クライアントに読み取られたヘッダー情報の数
Writing ##クライアントに返されたヘッダー情報の数
待機中 ##NGinx は、次のリクエスト コマンド番号

4 を待機している常駐接続の処理を終了しました。エラー ログを追加します

error_log 構文:

error_log ファイル レベル;

キーワードは変更されません。ファイルはログの保存場所、レベルはエラー ログ レベルです。

通常は、warn|error|crit の 3 つのレベルのみです。使用されます

nging.conf でエラー ログ構成を構成しますファイルに、error_loglogs/error_log;

を追加します。

worker_processes 1 の下;
はい、これがその行です。

以上がNginxインストール後の共通機能の設定方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:yisu.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!