Nginx組態虛擬機器


在上一節中,我們學習了Nginx+PHP7+MySQL的安裝組態,在這一篇文章中,我們來學習如何在一個Nginx伺服器中組態多個站點。

假要你所在的公司由於預算問題,現在只能提供一台伺服器,但是有以下幾個網站:www.yourmall.com, m.yourmall.comwww.yourcrm.com需要部署,並且你已經根據我們上一篇文章中安裝組態了,實現以下需求。

  • www.yourmall.com是公司的電子商務平台。
  • m.yourmall.com 是公司的電子商務平台子站,用於行動端使用者的存取。
  • www.yourcrm.com 是公司的使用者關係管理系統。

現在為以上網站在伺服器上規劃目錄,建立一個目錄:/var/sites,為上面三個站點分別建立一個目錄:

  • www.yourmall.com對應建立的目錄為: /var/sites/yourmall
  • m.yourmall.com 對應建立的目錄為: /var/sites/m_yourmall
  • www.yourcrm.com 對應建立的目錄為: /var/sites/yourcrm

如下圖所示 -

[root@localhost sites]# mkdir /var/sites/yourmall
[root@localhost sites]# mkdir /var/sites/m_yourmall
[root@localhost sites]# mkdir /var/sites/yourcrm
[root@localhost sites]# pwd
/var/sites
[root@localhost sites]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 28 04:23 m_yourmall
drwxr-xr-x. 2 root root 6 Apr 28 04:23 yourcrm
drwxr-xr-x. 2 root root 6 Apr 28 04:23 yourmall
[root@localhost sites]#

1. 第一個域名/網站組態

開啟Nginx的組態檔案:/usr/local/nginx/conf/nginx.conf ,並在 http 塊下加入以下子塊server,如下組態內容所示 -

    #  vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log  logs/yourmall.access.log  main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

現在,完整的Nginx組態檔案:/usr/local/nginx/conf/nginx.conf的內容如下所示 -


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.html;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.html;
    #    }
    #}

    #  vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log  logs/yourmall.access.log  main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

在目錄 /var/sites/yourmall 下建立一個檔案:index.html用於測試網站的組態情況,其程式碼如下 -

<!DOCTYPE html>
<html>
<head>
<title>Welcome To Yourmall.com</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>歡迎您存取:Yourmall.com </h1>
<p>這是 Yourmall.com 的首頁,僅用於Nginx的虛擬機器組態測試演示。</p>

<p>
<a href="https://www.tw511.com/">yourmall.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

現在,重新載入Nginx組態檔案,在載入組態檔案之前,最好先測試一下組態檔案語法是否正確,使用以下命令來測試 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sites]#

然後執行重新載入Nginx組態檔案 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到了這一步,我們來看看網站 www.yourmall.com 是否組態成功,接下來我們要存取這個域名,看能不能看到我們在上面寫入的 index.html 檔案中的內容。在測試存取www.yourmall.com 域名之前,我們還要對這個域名進行對映(外網稱為解析)。在區域網內的一台Windows計算機上開啟檔案 C:\Windows\System32\drivers\hosts,在這個檔案的最後一行加入以下內容 -

192.168.0.134    www.yourmall.com
192.168.0.134    yourmall.com

注意:在這裡,192.168.0.134 是組態Nginx伺服器的IP,如果你的伺服器不是這個IP,請寫上對應的伺服器IP。

現在,開啟瀏覽器,存取以下網站域名: www.yourmall.comyourmall.com ,沒有錯誤,應該會看到以下介面 -

就這樣,第一個網站的組態完成了!

2. 第二個域名/網站組態

現在,我們來組態第二個網站:m.yourmall.com, 假設 m.yourmall.com 這個網站它是 www.yourmall.com 的二級域名網站,主要用於服務主站 www.yourmall.com 的行動端存取使用者。

開啟Nginx的組態檔案:/usr/local/nginx/conf/nginx.conf ,並在 http 塊下加入以下子塊server,如下組態內容所示 -

    #  手機/行動端網站 vhost for m.yourmall.com configure.
    server {
        listen       80;
        server_name  m.yourmall.com;

        #charset koi8-r;

        #access_log  logs/m_yourmall.access.log  main;

        location / {
            root   /var/sites/m_yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/m_yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

現在,完整的Nginx組態檔案:/usr/local/nginx/conf/nginx.conf的內容如下所示 -


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }



    #  vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log  logs/yourmall.access.log  main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }


    #  手機/行動端網站 vhost for m.yourmall.com configure.
    server {
        listen       80;
        server_name  m.yourmall.com;

        #charset koi8-r;

        #access_log  logs/m_yourmall.access.log  main;

        location / {
            root   /var/sites/m_yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/m_yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

在目錄 /var/sites/m_yourmall 下建立一個檔案:index.html用於測試網站的組態情況,其程式碼如下 -

<!DOCTYPE html>
<html>
<head>
<title>Welcome To Yourmall.com</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>歡迎您存取:m.yourmall.com </h1>
<p>這是 Yourmall.com 主站的子域名:<a href="https://www.tw511.com/">m.yourmall.com</a> ,僅用於Nginx的虛擬機器組態測試演示。</p>

<p>
<a href="https://www.tw511.com/">m.yourmall.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

現在,重新載入Nginx組態檔案,在載入組態檔案之前,最好先測試一下組態檔案語法是否正確,使用以下命令來測試 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sites]#

然後執行重新載入Nginx組態檔案 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到了這一步,我們來看看網站 m.yourmall.com 是否組態成功,接下來我們要存取這個域名,看能不能看到我們在上面寫入的 index.html 檔案中的內容。在測試存取m.yourmall.com 域名之前,我們還要對這個域名進行對映(外網稱為解析)。在區域網內的一台Windows計算機上開啟檔案 C:\Windows\System32\drivers\hosts,在這個檔案的最後一行加入以下內容 -

192.168.0.134    m.yourmall.com

注意:在這裡,192.168.0.134 是組態Nginx伺服器的IP,如果你的伺服器不是這個IP,請寫上對應的伺服器IP。

現在,開啟瀏覽器,存取以下網站域名: m.yourmall.com 沒有錯誤,應該會看到以下介面 -

就這樣,第二個網站(子域名)的組態完成了!

3. 第三個域名/網站組態

接下來,我們來組態第三個網站:www.yourcrm.com, 假設 www.yourcrm.com 這個網站它是一個客戶管理系統,主要記錄客戶資訊的管理網站。

注意:組態這個網站與第一個網站類似,只是指定/使用檔案目錄不太一樣。

開啟Nginx的組態檔案:/usr/local/nginx/conf/nginx.conf ,並在 http 塊下加入以下子塊server,如下組態內容所示 -

    # 客戶管理系統 vhost for yourcrm.com configure.
    server {
        listen       80;
        server_name  www.yourcrm.com yourcrm.com;

        #charset koi8-r;

        #access_log  logs/yourcrm.access.log  main;

        location / {
            root   /var/sites/yourcrm;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/yourcrm;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

現在,完整的Nginx組態檔案:/usr/local/nginx/conf/nginx.conf的內容如下所示 -


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }



    #  vhost for yourmall.com configure.
    server {
        listen       80;
        server_name  www.yourmall.com yourmall.com;

        #charset koi8-r;

        #access_log  logs/yourmall.access.log  main;

        location / {
            root   /var/sites/yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }


    #  手機/行動端網站 vhost for m.yourmall.com configure.
    server {
        listen       80;
        server_name  m.yourmall.com;

        #charset koi8-r;

        #access_log  logs/m_yourmall.access.log  main;

        location / {
            root   /var/sites/m_yourmall;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/m_yourmall;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }


    # 客戶管理系統 vhost for yourcrm.com configure.
    server {
        listen       80;
        server_name  www.yourcrm.com yourcrm.com;

        #charset koi8-r;

        #access_log  logs/yourcrm.access.log  main;

        location / {
            root   /var/sites/yourcrm;
            index  index.html index.html;
        }

        error_page  404              /404.html;

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

        location ~ \.php$ {
            root           /var/sites/yourcrm;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

在目錄 /var/sites/yourcrm 下建立一個檔案:index.html用於測試網站的組態情況,其程式碼如下 -

<!DOCTYPE html>
<html>
<head>
<title>Welcome To YourCrm.com</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>歡迎您存取:yourcrm.com </h1>
<p>這是 yourcrm.com 網站的首頁 ,僅用於Nginx的虛擬機器組態測試演示。</p>

<p>
<a href="https://www.tw511.com/">yourcrm.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

現在,重新載入Nginx組態檔案,在載入組態檔案之前,最好先測試一下組態檔案語法是否正確,使用以下命令來測試 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sites]#

然後執行重新載入Nginx組態檔案 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到了這一步,我們來看看網站 www.yourcrm.com 是否組態成功,接下來我們要存取這個域名,看能不能看到我們在上面寫入的 index.html 檔案中的內容。在測試存取www.yourcrm.com 域名之前,我們還要對這個域名進行對映(外網稱為解析)。在區域網內的一台Windows計算機上開啟檔案 C:\Windows\System32\drivers\hosts,在這個檔案的最後一行加入以下內容 -

192.168.0.134    www.yourcrm.com
192.168.0.134    yourcrm.com

注意:在這裡,192.168.0.134 是組態Nginx伺服器的IP,如果你的伺服器不是這個IP,請寫上對應的伺服器IP。

現在,開啟瀏覽器,存取以下網站域名: www.yourcrm.comyourcrm.com ,沒有錯誤,應該會看到以下介面 -

就這樣,第三個網站的組態完成了!

4. 優化組態

經過了上面三個網站的組態,相信你可能覺得 Nginx 的組態檔案:/usr/local/nginx/conf/nginx.conf 內容有點多了,這還只是最簡單的組態(還未包函重寫,紀錄檔記錄等規則),從頭看到尾多少有點不太方便,能不能有更好的辦法解決這個問題? 當然可以。我們可以把每個網站的組態獨立成一個組態檔案,然後在主組態檔案中使用 include 指令包函進來。

現在來看看怎麼修改組態,首先分別建立三個網站的組態檔案:

  • www.yourmall.com -> /usr/local/nginx/conf/vhosts/yourmall.conf
  • m.yourmall.com -> /usr/local/nginx/conf/vhosts/m_yourmall.conf
  • www.yourcrm.com -> /usr/local/nginx/conf/vhosts/yourcrm.conf

再將上面組態中對應每個網站的檔案包括放入主組態檔案。例如,對於檔案 /usr/local/nginx/conf/vhosts/yourmall.conf 放置的檔案內容如下:

#  vhost for yourmall.com configure.
server {
    listen       80;
    server_name  www.yourmall.com yourmall.com;

    #charset koi8-r;

    #access_log  logs/yourmall.access.log  main;

    location / {
        root   /var/sites/yourmall;
        index  index.html index.html;
    }

    error_page  404              /404.html;

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

    location ~ \.php$ {
        root           /var/sites/yourmall;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
        include        fastcgi_params;
    }

}

其它兩個網站也同樣放入對應組態。

在主組態檔案:/usr/local/nginx/conf/nginx.conf 中,包函上述三個檔案即可。現在檔案:/usr/local/nginx/conf/nginx.conf的內容如下所示 -


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

    # 網站組態
    include /usr/local/nginx/conf/vhosts/yourmall.conf;

    include /usr/local/nginx/conf/vhosts/m_yourmall.conf;

    include /usr/local/nginx/conf/vhosts/yourcrm.conf;

}

重新載入Nginx組態檔案,在載入組態檔案之前,最好先測試一下組態檔案語法是否正確,使用以下命令來測試 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sites]#

然後執行重新載入Nginx組態檔案 -

[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#

到此,在Nginx上組態虛擬機器演示範例就完了。