Nginnx:ドメイン名に基づく仮想ホスト構成

7985 ワード

1、仮想ホスト概念
  • 仮想ホストとは、Webサービスでは独立したウェブサイトであり、このサイトは独立したドメイン名(IPまたはポートかもしれない)に対応しており、独立したプログラム及びリソースを有しており、独立してユーザアクセスのために外部にサービスを提供することができる。
  • はNgixにおいて、一つのserver{}タグを使用して仮想ホストを識別し、一つのWebサービスに複数の仮想ホストタグペアがあり、すなわち複数の仮想ホスト局を同時にサポートすることができる。
  • 仮想ホストは、ドメイン名ベースの仮想ホスト、ポートベースの仮想ホスト、IPベースの仮想ホストの3つのタイプがある。
  • 2、ドメイン名に基づく仮想ホスト構成
    2.1プロファイルの編集
    [root@localhost conf]# vim nginx.conf
    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  www.abc.com;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
       
        server {
            listen       80;
            server_name  bbs.abc.com;
            location / { 
                root   html/bbs;
                index  index.html index.htm;
            }   
        }   
    
        server {
            listen       80;
            server_name  blog.abc.com;
            location / { 
                root   html/blog;
                index  index.html index.htm;
            }   
        }   
    }
    
    ルール化されたNgixプロファイルは、各仮想ホストを個別のファイルに構成し、統一ディレクトリに配置する(例えば、vhosts)。
  • vhostsディレクトリ
  • を作成します。
    [root@localhost conf]# mkdir -p /usr/local/nginx/conf/vhosts
    
  • nginx.com nfマスタープロファイルを編集する
  • [root@localhost conf]# vim nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        include vhosts/*.conf;
    }
    
  • は、各仮想ホスト構成ファイルを作成する。
  • [root@localhost conf]# vim vhosts/www.abc.com.conf
    server {
            listen       80;
            server_name  www.abc.com;
            location / {
                    root   html/www;
                    index  index.html index.htm;
            }
    }
    [root@localhost conf]# vim vhosts/bbs.abc.com.conf
    server {
            listen       80;
            server_name  bbs.abc.com;
            location / {
                    root   html/bbs;
                    index  index.html index.htm;
            }
    }
    [root@localhost conf]# vim vhosts/blog.abc.com.conf
    server {
            listen       80;
            server_name  blog.abc.com;
            location / {
                    root   html/blog;
                    index  index.html index.htm;
            }
    }
    
    2.2仮想ホスト局に対応するディレクトリとファイルを作成する
    [root@localhost html]# cd /usr/local/nginx/html/
    [root@localhost html]# for n in www bbs blog
    > do
    > mkdir ${n}
    > echo "http://${n}.abc.com" > ${n}/index.html
    > done
    
    2.3編集/etc/hostsファイル、ドメイン名解析
    echo "127.0.0.1 www.abc.com bbs.abc.com blog.abc.com" >> /etc/hosts
    
    またはコマンドで/etc/hostsファイルを開いて情報を編集します。
    2.4 NFIxプロファイルを再読み込みする
    nginxプロファイルは文法チェックでエラーがあるかどうかを確認します。
    [root@localhost conf]# /usr/local/nginx/sbin/nginx -t
        nginx            
    
    nginxサービスの円滑な再開
    [root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
    
    appche、nginxなど、滑らかにリセットできないなら、stopとstartを使って、retartを禁止します。
    [root@localhost conf]# /usr/local/ginx/sbin/nginx -s stop    #stop  -s stop
    [root@localhost conf]# /usr/local/nginx/sbin/nginx    #  nginx  start
    
    inxサービス80ポートが起動されているかどうかを確認します。
    [root@localhost conf]# lsof -i :80
    COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    nginx    6290   root    6u  IPv4  56693      0t0  TCP *:http (LISTEN)
    nginx   10273 nobody    6u  IPv4  56693      0t0  TCP *:http (LISTEN)
    
    linuxでinxサービスが利用可能かどうかを検証します。もちろんwindowsでも検証できます。
  • は、仮想マシンでコマンドを実行して、Hostsファイルが*127..0.1(他のIPアドレスにも変更できます)*のDNS解析ドメインwwww.abc.com、bs.abc.comとblog.abc.comを追加するかどうかを確認します。
  • [root@localhost conf]# cat /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    127.0.0.1 www.abc.com bbs.abc.com blog.abc.com
    
    コマンドを入力することもできます。vim/etc/hostsがhostsファイルを修正します。
    [root@localhost gmz]# vim /etc/hosts
    
    そしてコマンドを入力します。ping wwww.abc.com、blog.abc.comとbbs.abc.comは127.1.0.1のこのIPアドレスに解析できるかどうかを確認してください。以上のコマンドを入力すると、コンソールに以下の情報が表示されなくなり、要求タイムアウト(ping不通)、端末ping操作が欲しいなら、Ctr+Cを押してpingコマンドを終了することができます。pingが通じない場合、ホストhostsファイル+仮想マシンhostsファイルが前のステップの操作を完了したかどうか、すなわちHostsファイルにDNS解析を追加することができます。
    [root@localhost gmz]# ping www.abc.com
    PING www.abc.com (127.0.0.1) 56(84) bytes of data.
    64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.108 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.076 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.060 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.062 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.060 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.059 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.030 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=8 ttl=64 time=0.040 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=9 ttl=64 time=0.061 ms
    ^C
    --- www.abc.com ping statistics ---
    9 packets transmitted, 9 received, 0% packet loss, time 8002ms
    rtt min/avg/max/mdev = 0.030/0.061/0.108/0.022 ms
    [root@localhost gmz]# ping bbs.abc.com
    PING www.abc.com (127.0.0.1) 56(84) bytes of data.
    64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.016 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.047 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.056 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.154 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.059 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.059 ms
    ^C
    --- www.abc.com ping statistics ---
    6 packets transmitted, 6 received, 0% packet loss, time 5000ms
    rtt min/avg/max/mdev = 0.016/0.065/0.154/0.042 ms
    [root@localhost gmz]# ping blog.abc.com
    PING www.abc.com (127.0.0.1) 56(84) bytes of data.
    64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.017 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.067 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.056 ms
    64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.060 ms
    ^C64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.059 ms
    
    64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.062 ms
    ^C
    --- www.abc.com ping statistics ---
    6 packets transmitted, 6 received, 0% packet loss, time 5002ms
    rtt min/avg/max/mdev = 0.017/0.053/0.067/0.018 ms
    
  • windowsブラウザでwindowsホストhostsファイルにdns解析を追加することを検証しました。C:\Windows\System 32\drivers\etc\hostsにホスト解析を追加しました。
    #Nginx              hosts      ,      ping 
    127.0.0.1 www.abc.com bbs.abc.com blog.abc.com
    
    cmdウィンドウに以下のコマンドを入力して、DNSの内容を更新します。
    ipconfig /flushdns
    
    ブラウザにwww.abc.com、bbs.abc.com、blog.abc.comをそれぞれ入力し、ブラウザが対応するトップページの内容を返すことができれば、ドメイン名が追加された仮想ホストが成功したことを説明する。
    2.5アクセステスト
    cul+仮想ホストサイトの名前は、コンソールが対応するサイトの内容(トップページindex)を表示するかどうかを確認します。
    [root@localhost gmz]# curl http://www.abc.com
    http://www.abc.com
    [root@localhost gmz]# curl http://blog.abc.com
    http://blog.abc.com
    [root@localhost gmz]# curl http://bbs.abc.com 
    http://bbs.abc.com
    
    結節、nginx構成仮想ホストステップは以下の通りである(各種の仮想ホストタイプに適合する)。
  • 完全なserverタグを最後まで追加します。注意してください。httpの終了大括弧の前に置いてください。つまりserverタグをhttpタグのようにします。
  • server_を変更します。nameおよび対応ページのrootルートディレクトリは、他のパラメータが必要であれば追加または修正できます。
  • server_を作成します。nameドメイン名は、ウェブページのルートディレクトリに対応し、テストファイルを作成します。indexトップページがないと、アクセスは403エラーが発生します。
  • は、inx設定ファイルの文法を確認し、inxサービスをスムーズに再開し、起動結果を迅速に確認する。
  • クライアントでserver_nameで構成されたドメイン名は、ホスト解析またはdns構成を行い、返信されたipペアをpingドメインで確認する。
  • は、win 32ブラウザにアドレスアクセスを入力するか、またはLinuxクライアントでhosts解析をして、wgetまたはcurlでアドレスにアクセスする。inx仮想ホストのウェブサイトヘルプURLは以下の通りです。http://nginx.org/en/docs/http/request_processingy.