LinuxシステムIOモデルの概要Nginxコンパイルのインストールと基本構成

LinuxシステムIOモデルの概要Nginxコンパイルのインストールと基本構成

1.LinuxシステムIOモデル

1.IOの概念

Linuxの世界ではすべてがファイルであり、ソケット、パイプ、端末などはすべてファイルです。Linuxシステムのファイルは、一連のバイナリストリームとして理解することもできます。情報交換の過程で、これらのバイナリストリームを送受信する操作はI / O操作(入力と出力)であり、最も一般的なのはディスクIOとネットワークIOです。

2.カーネルスペースとユーザースペース

Linuxシステムは、メモリ空間をカーネル空間とユーザー空間の2つの部分に分割します。カーネルコードとデータはカーネルスペースに格納され、特権命令を実行したり、ハードウェアへのアクセスなどの外部サービスインターフェイスを提供したりできます。ユーザースペースに格納されているのはユーザープログラムのコードとデータであり、非特権命令のみを実行できます。

ディスクファイルの読み取りと書き込み、ネットワークインターフェイスからのデータの読み取りと書き込みなど、すべてのシステムリソース管理はカーネル空間で実行されます。ユーザーのアプリケーションプログラムは、そのような操作を直接実行することはできず、カーネルによって提供されるインターフェイスを介してのみそのようなタスクを完了することができます。

4.システムIOモデル

システムIOモデルを理解するための例として、ユーザープログラムを使用してデータを読み取ります。ユーザーにはディスクデバイスに直接アクセスする権限がないため、カーネルを介してのみアクセスできます。カーネルがユーザーが必要とするデータをディスクから読み取る場合、カーネルスペースとユーザースペースの厳密な分離によって制限され、カーネルスペースのメモリデータをユーザースペースのプロセスメモリにコピーする必要があります。つまり、簡単に言うと、I / Oは、カーネルがディスクからカーネルスペースにデータを読み取り、カーネルスペースのメモリデータをユーザースペースのプロセスのメモリにコピーするプロセス全体です。システムIOには、同期、非同期、ブロッキング、および非ブロッキングの4つのモデルがあります。

  • 同期非同期

    イベント処理のメッセージ通信メカニズム、つまり、呼び出し先がモノの処理結果を待っている間に完了通知を提供するかどうかに焦点が当てられます。同期:ユーザープロセスが要求呼び出しを行った後、カーネルは通知メカニズムを提供しません。つまり、ファイルIO処理が完了した後、ユーザープロセスは通知されません。ユーザープロセスは、処理が完了したかどうかをカーネルに確認する必要があります。非同期:非同期ユーザープロセスが要求呼び出しを行った後、カーネルは呼び出しが処理された後、呼び出し結果をユーザープロセスに返します。Nginxは非同期です。

  • ブロッキング/非ブロッキング

    結果が返されるのを待つ前に、発信者の状態に注意してください。ブロッキング:ブロッキングとは、ユーザースペースに戻る前に、IO操作を完全に完了する必要があることを意味します。呼び出し結果が返される前に、呼び出し元は一時停止され、他のことはできません。ノンブロッキング:ノンブロッキングとは、IOオペレーションが呼び出された直後に、IOオペレーションがステータス値をユーザーに返すことを意味します。IOオペレーションが完全に完了するまで待つ必要はありません。発信者は、最終的なコール結果が返されるまで中断されず、他のことを実行できます。

例として私を食べさせてください:私は10個のパンを注文しました

  • 同期および非同期:

    私がパンを注文した後、シェフは私に言った:

    • 同期:シェフはパンを作った後、指定された位置にパンを置きますが、パンを作る前に、パンが完成したかどうかを確認する必要があります。シェフはパンを作りません。

    完了したら通知してください。

    • 非同期:シェフがパンを作った後、彼はパンを置く場所を教えてくれます。
  • ブロッキングと非ブロッキング::

    お団子を注文した後の状態:

    • 詰まり:シェフがパンを作っている間、シェフはパンプレートの前で待っていて、他のことはできません。

    • ノンブロッキング:パンを注文した後、買い物に行く、購入するなど、他のことを行うことができます。
  • IOモデルの組み合わせ:

    • 同期ブロッキング:パンを注文した後は他に何もできず、パンが完成したかどうかわかりません。しばらく待って、シェフに何度も尋ねる必要があります。

    番号。

    • 同期ノンブロッキング:バンを注文した後、他のことを行うことはできますが、バンがうまく機能しているかどうかまだわからないため、長い間他のことを行うことはできません。

    私は待っていて、シェフに何度も何度も彼がうまくやっているかどうか尋ねてきたので、私は何か他のことをする時間しか作ることができません。

    • 非同期ブロッキング:パンを注文した後は他のことをすることはできませんが、パンができたらシェフが教えてくれます。つまり、パンを何度も調理する必要はありません。

    できましたか?

    • 非同期ノンブロッキング:パンを注文した後は他のこともできますが、パンができたらシェフが教えてくれるので、いつでも他のことができます。

5.Unixシステムの5つのネットワークIOモデルとApacheの3つの動作モード

UNIXシステムには5つのネットワークIOモデルがあります。

  • 同期ブロッキングIOモデル(ブロッキングIO):ブロッキングIOモデルは最も単純なIOモデルです。カーネルがIO操作を実行すると、ユーザースレッドがブロックされます。
  • 同期非ブロッキングI / Oモデル(非ブロッキングIO):アプリケーションプロセスは、IO要求をカーネルに送信した後、カーネルが応答するのを待機しています。要求されたIO操作を処理するカーネルがIO結果をすぐに返すことができない場合、プロセスは待機せず、他の処理を続行します。要求しますが、カーネルIOが一定期間後に完了したかどうかを確認するプロセスが必要です。
  • IO多重化:システムカーネルはI / Oデータをバッファリングし、単一のプロセスが複数のファイル記述子を監視できるようにします。記述子の準備ができたら、対応する読み取りおよび書き込み操作を実行するようにプログラムに通知できます。
    • Linuxオペレーティングシステムでは、すべてがファイルに抽象化されますが、アプリケーションはシステム内のさまざまなファイルにどのように対応しますか?ファイル記述子(ファイル記述子、fdと呼ばれる)が生まれました。アプリケーションがカーネルにファイルを開く/作成するように要求すると、カーネルは開かれた/作成されたファイルに対応するファイル記述子を返します。
    • select、poll、およびepollは、LinuxシステムでのIO多重化モデルの機能実現です。Select、poll、およびepollは、基本的に同期I / Oです。これは、読み取りと書き込みのイベントの準備ができて、読み取りと書き込みのプロセスがブロックされた後、これらすべてが読み取りと書き込みを担当する必要があるためです。
    • Apacheプリフォークモードはメインプロセス+マルチプロセス/シングルスレッド+選択モードです。Apache作業モードはメインプロセス+マルチプロセス/マルチスレッド+ポーリングモードです。
  • 信号駆動型IO(信号駆動型IO):ユーザープロセスはsigactionシステム呼び出しを介して信号ハンドラーを登録でき、その後プロセスは下向きに実行を継続できます。IO操作の準備ができると、カーネルはSIGIO信号ハンドラーに通知して実行をトリガーします。 、次に、ユーザープロセスに必要なデータをカーネルスペースからユーザースペースにコピーします。
    • Apacheイベントモードは、メインプロセス+マルチプロセス/マルチスレッド+信号駆動モードです。
  • 非同期(非ブロッキング)IO(非同期IO):ユーザープロセスがaio_readシステム呼び出しを行った後、カーネルデータの準備ができているかどうかに関係なく、カーネルデータはユーザープロセスに直接戻り、ユーザーモードプロセスはソケットデータの準備ができるまで他のことを実行できます。 、カーネルはデータをプロセスに直接コピーしてから、カーネルからプロセスに通知を送信します。非同期非ブロッキングIOの2つのフェーズでは、プロセスは非ブロッキングです。

2、Nginxの基本

1. Ubuntu18.04.5システムの最適化

# vim /etc/security/limits.conf   #在文件后补追加
#root账户的资源软限制和硬限制
root soft core unlimited
root hard core unlimited
root soft nproc 1000000
root hard nproc 1000000
root soft nofile 1000000
root hard nofile 1000000
root soft memlock 32000
root hard memlock 32000
root soft msgqueue 8192000
root hard msgqueue 8192000
#其他账户的资源软限制和硬限制
* soft core unlimited
* hard core unlimited
* soft nproc 1000000
* hard nproc 1000000
* soft nofile 1000000
* hard nofile 1000000
* soft memlock 32000
* hard memlock 32000
* soft msgqueue 8192000
# vim /etc/sysctl.conf      #在文件后补追加
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1

# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1

# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0

# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536

# # Controls the maximum size of a message, in bytes
kernel.msgmax = 65536

# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736

# # Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296

# TCP kernel paramater
net.ipv4.tcp_mem = 786432 1048576 1572864
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1

# socket buffer
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 20480
net.core.optmem_max = 81920

# TCP conn net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 15

# tcp conn reuse
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_tw_reuse = 0
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syncookies = 1

# keepalive conn
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.ip_local_port_range = 10001 65000

# swap
vm.overcommit_memory = 0
vm.swappiness = 10

#net.ipv4.conf.eth1.rp_filter = 0
#net.ipv4.conf.lo.arp_ignore = 1
#net.ipv4.conf.lo.arp_announce = 2
#net.ipv4.conf.all.arp_ignore = 1
#net.ipv4.conf.all.arp_announce = 2

# reboot

2.Nginxをコンパイルしてインストールします

2.1オペレーティングシステムソフトウェアのバージョン情報

4.15.0-112-generic#113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU / Linux

2.2nginxソースコードを入手する

公式ウェブサイト:http//nginx.org/、ページの右側にダウンロードエントリがあります。

画像-20201118205917571

画像-20201118210054867

2.3環境準備のコンパイルとインストール
  • nginxアカウントを作成する
# groupadd -g 2020 nginx
# useradd -u 2020 -g 2020 -r -s /bin/bash nginx
  • Nginxディレクトリのアクセス許可設定
# mkdir -p /app/nginx
# chown -R nginx:nginx /app
  • インストールパラメータを編集および定義する
# mkdir  /app
# tar xvf nginx-1.18.0.tar.gz
# cd nginx-1.18.0
# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

  --build=NAME                       set build name
  --builddir=DIR                     set build directory

  --with-select_module               enable select module
  --without-select_module            disable select module
  --with-poll_module                 enable poll module
  --without-poll_module              disable poll module

  --with-threads                     enable thread pool support

  --with-file-aio                    enable file AIO support

  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-http_v2_module              enable ngx_http_v2_module
  --with-http_realip_module          enable ngx_http_realip_module
  --with-http_addition_module        enable ngx_http_addition_module
  --with-http_xslt_module            enable ngx_http_xslt_module
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module    enable ngx_http_image_filter_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module
  --with-http_flv_module             enable ngx_http_flv_module
  --with-http_mp4_module             enable ngx_http_mp4_module
  --with-http_gunzip_module          enable ngx_http_gunzip_module
  --with-http_gzip_static_module     enable ngx_http_gzip_static_module
  --with-http_auth_request_module    enable ngx_http_auth_request_module
  --with-http_random_index_module    enable ngx_http_random_index_module
  --with-http_secure_link_module     enable ngx_http_secure_link_module
  --with-http_degradation_module     enable ngx_http_degradation_module
  --with-http_slice_module           enable ngx_http_slice_module
  --with-http_stub_status_module     enable ngx_http_stub_status_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module
  --without-http_auth_basic_module   disable ngx_http_auth_basic_module
  --without-http_mirror_module       disable ngx_http_mirror_module
  --without-http_autoindex_module    disable ngx_http_autoindex_module
  --without-http_geo_module          disable ngx_http_geo_module
  --without-http_map_module          disable ngx_http_map_module
  --without-http_split_clients_module disable ngx_http_split_clients_module
  --without-http_referer_module      disable ngx_http_referer_module
  --without-http_rewrite_module      disable ngx_http_rewrite_module
  --without-http_proxy_module        disable ngx_http_proxy_module
  --without-http_fastcgi_module      disable ngx_http_fastcgi_module
  --without-http_uwsgi_module        disable ngx_http_uwsgi_module
  --without-http_scgi_module         disable ngx_http_scgi_module
  --without-http_grpc_module         disable ngx_http_grpc_module
  --without-http_memcached_module    disable ngx_http_memcached_module
  --without-http_limit_conn_module   disable ngx_http_limit_conn_module
  --without-http_limit_req_module    disable ngx_http_limit_req_module
  --without-http_empty_gif_module    disable ngx_http_empty_gif_module
  --without-http_browser_module      disable ngx_http_browser_module
  --without-http_upstream_hash_module
                                     disable ngx_http_upstream_hash_module
  --without-http_upstream_ip_hash_module
                                     disable ngx_http_upstream_ip_hash_module
  --without-http_upstream_least_conn_module
                                     disable ngx_http_upstream_least_conn_module
  --without-http_upstream_random_module
                                     disable ngx_http_upstream_random_module
  --without-http_upstream_keepalive_module
                                     disable ngx_http_upstream_keepalive_module
  --without-http_upstream_zone_module
                                     disable ngx_http_upstream_zone_module

  --with-http_perl_module            enable ngx_http_perl_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-perl_modules_path=PATH      set Perl modules path
  --with-perl=PATH                   set perl binary pathname

  --http-log-path=PATH               set http access log pathname
  --http-client-body-temp-path=PATH  set path to store
                                     http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store
                                     http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store
                                     http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store
                                     http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store
                                     http scgi temporary files

  --without-http                     disable HTTP server
  --without-http-cache               disable HTTP cache

  --with-mail                        enable POP3/IMAP4/SMTP proxy module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module

  --with-stream                      enable TCP/UDP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_realip_module        enable ngx_stream_realip_module
  --with-stream_geoip_module         enable ngx_stream_geoip_module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --without-stream_limit_conn_module disable ngx_stream_limit_conn_module
  --without-stream_access_module     disable ngx_stream_access_module
  --without-stream_geo_module        disable ngx_stream_geo_module
  --without-stream_map_module        disable ngx_stream_map_module
  --without-stream_split_clients_module
                                     disable ngx_stream_split_clients_module
  --without-stream_return_module     disable ngx_stream_return_module
  --without-stream_upstream_hash_module
                                     disable ngx_stream_upstream_hash_module
  --without-stream_upstream_least_conn_module
                                     disable ngx_stream_upstream_least_conn_module
  --without-stream_upstream_random_module
                                     disable ngx_stream_upstream_random_module
  --without-stream_upstream_zone_module
                                     disable ngx_stream_upstream_zone_module

  --with-google_perftools_module     enable ngx_google_perftools_module
  --with-cpp_test_module             enable ngx_cpp_test_module

  --add-module=PATH                  enable external module
  --add-dynamic-module=PATH          enable dynamic external module

  --with-compat                      dynamic modules compatibility

  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU                 build for the specified CPU, valid values:
                                     pentium, pentiumpro, pentium3, pentium4,
                                     athlon, opteron, sparc32, sparc64, ppc64

  --without-pcre                     disable PCRE library usage
  --with-pcre                        force PCRE library usage
  --with-pcre=DIR                    set path to PCRE library sources
  --with-pcre-opt=OPTIONS            set additional build options for PCRE
  --with-pcre-jit                    build PCRE with JIT compilation support

  --with-zlib=DIR                    set path to zlib library sources
  --with-zlib-opt=OPTIONS            set additional build options for zlib
  --with-zlib-asm=CPU                use zlib assembler sources optimized
                                     for the specified CPU, valid values:
                                     pentium, pentiumpro

  --with-libatomic                   force libatomic_ops library usage
  --with-libatomic=DIR               set path to libatomic_ops library sources

  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

  --with-debug                       enable debug logging

nginxモジュールの機能説明は、公式ウェブサイトのドキュメントで確認できます。

画像-20201118211423656

画像-20201118211504074

  • ソフトウェア環境の準備をコンパイルしてインストールします

    • apt-getをアップグレードする # apt update
    • gccをインストールします # apt install -y gcc
    • C ++依存関係ライブラリをインストールする # apt install build-essential # apt install libtool
    • pcre依存関係ライブラリをインストールする # apt install libpcre3 libpcre3-dev
    • zlib依存関係ライブラリをインストールします # apt install zlib1g-dev
    • ssl依存関係ライブラリをインストールする #apt install libssl-dev
  • カスタムインストールパラメータを決定する

    公式ウェブサイトhttp://nginx.org/en/docs/configure.htmlで、カスタムパラメータの意味と最終的なインストールオプションパラメータを確認してください。

    $ sudo ./configure --prefix = / app / nginx --user = nginx --group = nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module- with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

2.4コンパイルとインストールを開始します
  • 構成、設定

    #  ./configure --prefix=/app/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
    
  • 作る

    # make
  • インストールする

    # make install
  • コンパイルしてインストールした後、nginxを起動します

    # /app/nginx/sbin/nginx
  • コンパイルおよびインストールされたnginxWebインターフェイスにアクセスします。

    画像-20201118230138056

3.Nginxの一般的な構成と最適化

3.1環境パラメータにnginxパスを追加する
root@dl-homework:~# pwd
/root
root@dl-homework:~# ll
total 64
drwx------  5 root root  4096 Nov 19 10:03 ./
drwxr-xr-x 24 root root  4096 Nov 18 21:05 ../
-rw-------  1 root root 10257 Nov 19 09:46 .bash_history
-rw-r--r--  1 root root  3140 Nov 19 09:44 .bashrc
drwx------  2 root root  4096 Nov 14 11:28 .cache/
drwx------  3 root root  4096 Nov 14 11:28 .gnupg/
-rw-r--r--  1 root root   148 Aug 17  2015 .profile
drwxr-xr-x  2 root root  4096 Nov 14 11:55 .vim/
-rw-------  1 root root 12836 Nov 19 09:45 .viminfo
-rw-r--r--  1 root root    17 Nov 19 09:45 .vimrc
-rw-------  1 root root   134 Nov 19 10:03 .Xauthority
root@dl-homework:~# vim .bashrc

100 export PATH=$PATH:/app/nginx/sbin   #在文件最后一行添加

# . .bashrc
3.2起動スクリプトを使用して自己起動nginxサービスを実現する
# cd /lib/systemd/system
# vim nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking   
PIDFile=/run/nginx.pid   #要与nginx.conf里面的配置保持一致
ExecStart=/app/nginx/sbin/nginx -c /app/nginx/conf/nginx.conf  #更改为nginx的实际路径
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

# systemctl daemon-reload

# systemctl start nginx.service
## systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
3.3Nginxの基本構成

すべてのパラメーターは、nginx.com Webサイトのドキュメントに記載されています。nginx.confのデフォルトパラメーター:

# cd /app/nginx/conf
# grep -v "#"  nginx.conf |grep -v "^$"
worker_processes  1;
pid        /run/nginx.pid;
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;
        }
    }
}
# vim /app/nginx/conf/nginx.conf

 worker_processes  auto;       #进程数量
 worker_cpu_affinity  auto;    #进程与CPU绑定

 pid        /run/nginx.pid;    #文件位置要与nginx.service里的配置保持一致

 worker_rlimit_nofile   65535;   #允许一个工作进程打开的文件数

 events {
      worker_connections  102400;  #单个进程最大并发连接数
      accept_mutex on;   #避免群惊
      multi_accept on;   #允许接受多个新连接
  }

  http {
    include       mime.types;   #支持的文件类型
    default_type  application/octet-stream;  #无法识别就下载文件

     sendfile        on;  #实现文件零拷贝MMAP
     tcp_nopush      on;  #合并请求后统一发送给客户端,降低服务器端负载,配合sendfile使用
     gzip  on;  #开启文件压缩
     keepalive_timeout  65;  #长连接时间65秒
     server {
        listen       80;
        server_name  localhost;
        charset utf-8;   #更改中文字符集
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

  }

# nginx -t
nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx/conf/nginx.conf test is successful
# systemctl reload nginx.service
3.4PCサイトとモバイルサイトを作成する
# mkdir /app/nginx/conf/conf.d
# vim /app/nginx/conf/conf.d/pc.conf
server {
    listen 80;
    server_name pc.home.net;

    location / {
        root /app/nginx/html/pc;
        }
    }

# vim /app/nginx/conf/conf.d/mobile.conf 
server {
    listen 80;
    server_name mobile.home.net;

    location / {
        root /app/nginx/html/mobile;
        }
    }

# mkdir /app/nginx/html/{pc,mobile}
# echo "mobile web" > /app/nginx/html/mobile/index.html
# echo "pc web" > /app/nginx/html/pc/index.html

# nginx -t
nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx/conf/nginx.conf test is successful

# systemctl reload nginx.service

Windowsシステムの下のC:\ Windows \ System32 \ drivers \ etc \ hostsファイルを変更し、172.20.2200.138 pc.home.net mobile.home.netを追加して、次のサイトにアクセスします。

画像-20201119194403714

画像-20201119194444533

おすすめ

転載: blog.51cto.com/12302225/2552422