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="http://www.yiibai.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 ,沒有錯誤,應該會看到以下界面 -

Nginx配置虛擬機

就這樣,第一個網站的配置完成了!

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="http://www.yiibai.com/">m.yourmall.com</a> ,僅用於Nginx的虛擬機配置測試演示。</p>

<p>
<a href="http://www.yiibai.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 沒有錯誤,應該會看到以下界面 -

Nginx配置虛擬機

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

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="http://www.yiibai.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 ,沒有錯誤,應該會看到以下界面 -

Nginx配置虛擬機

就這樣,第三個網站的配置完成了!

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上配置虛擬機演示實例就完了。