鍍金池/ 教程/ Linux/ Nginx配置虛擬機(jī)
Nginx安裝
Nginx內(nèi)容緩存
Nginx架構(gòu)
Nginx進(jìn)程和運(yùn)行時(shí)控制
Nginx配置虛擬機(jī)
Nginx從源碼構(gòu)建安裝配置
為什么需要Nginx?
Nginx配置文件
Nginx壓縮和解壓
Nginx+PHP7+MySQL5.7(LNMP)環(huán)境配置
Nginx反向代理
Nginx+PHP7源碼安裝配置
Nginx快速入門(mén)
Nginx的優(yōu)勢(shì)(優(yōu)點(diǎn))
Nginx教程
Nginx特性
Nginx配置Web服務(wù)器
Nginx配置日志
Nginx配置靜態(tài)內(nèi)容服務(wù)器
Nginx主要應(yīng)用場(chǎng)景(必讀)
Nginx安裝(從Linx包安裝)

Nginx配置虛擬機(jī)

在上一節(jié)中,我們學(xué)習(xí)了Nginx+PHP7+MySQL的安裝配置,在這一篇文章中,我們來(lái)學(xué)習(xí)如何在一個(gè)Nginx服務(wù)器中配置多個(gè)站點(diǎn)。

假要你所在的公司由于預(yù)算問(wèn)題,現(xiàn)在只能提供一臺(tái)服務(wù)器,但是有以下幾個(gè)網(wǎng)站:www.yourmall.com, m.yourmall.com,www.yourcrm.com需要部署,并且你已經(jīng)根據(jù)我們上一篇文章中安裝配置了,實(shí)現(xiàn)以下需求。

  • www.yourmall.com是公司的電子商務(wù)平臺(tái)。
  • m.yourmall.com 是公司的電子商務(wù)平臺(tái)子站,用于移動(dòng)端用戶的訪問(wèn)。
  • www.yourcrm.com 是公司的用戶關(guān)系管理系統(tǒng)。

現(xiàn)在為以上網(wǎng)站在服務(wù)器上規(guī)劃目錄,創(chuàng)建一個(gè)目錄:/var/sites,為上面三個(gè)站點(diǎn)分別創(chuàng)建一個(gè)目錄:

  • www.yourmall.com對(duì)應(yīng)創(chuàng)建的目錄為: /var/sites/yourmall
  • m.yourmall.com 對(duì)應(yīng)創(chuàng)建的目錄為: /var/sites/m_yourmall
  • www.yourcrm.com 對(duì)應(yīng)創(chuàng)建的目錄為: /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. 第一個(gè)域名/網(wǎng)站配置

打開(kāi)Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 塊下加入以下子塊server,如下配置內(nèi)容所示 -

    #  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;
        }

    }

現(xiàn)在,完整的Nginx配置文件:/usr/local/nginx/conf/nginx.conf的內(nèi)容如下所示 -


#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 下創(chuàng)建一個(gè)文件:index.html用于測(cè)試網(wǎng)站的配置情況,其代碼如下 -

<!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>歡迎您訪問(wèn):Yourmall.com </h1>
<p>這是 Yourmall.com 的首頁(yè),僅用于Nginx的虛擬機(jī)配置測(cè)試演示。</p>

<p>
<a href="http://www.yiibai.com/">yourmall.com</a>.</p>

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

現(xiàn)在,重新加載Nginx配置文件,在加載配置文件之前,最好先測(cè)試一下配置文件語(yǔ)法是否正確,使用以下命令來(lái)測(cè)試 -

[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]#

然后執(zhí)行重新加載Nginx配置文件 -

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

到了這一步,我們來(lái)看看網(wǎng)站 www.yourmall.com 是否配置成功,接下來(lái)我們要訪問(wèn)這個(gè)域名,看能不能看到我們?cè)谏厦鎸?xiě)入的 index.html 文件中的內(nèi)容。在測(cè)試訪問(wèn)www.yourmall.com 域名之前,我們還要對(duì)這個(gè)域名進(jìn)行映射(外網(wǎng)稱為解析)。在局域網(wǎng)內(nèi)的一臺(tái)Windows計(jì)算機(jī)上打開(kāi)文件 C:\Windows\System32\drivers\hosts,在這個(gè)文件的最后一行加入以下內(nèi)容 -

192.168.0.134    www.yourmall.com
192.168.0.134    yourmall.com

注意:在這里,192.168.0.134 是配置Nginx服務(wù)器的IP,如果你的服務(wù)器不是這個(gè)IP,請(qǐng)寫(xiě)上對(duì)應(yīng)的服務(wù)器IP。

現(xiàn)在,打開(kāi)瀏覽器,訪問(wèn)以下網(wǎng)站域名: www.yourmall.comyourmall.com ,沒(méi)有錯(cuò)誤,應(yīng)該會(huì)看到以下界面 -

就這樣,第一個(gè)網(wǎng)站的配置完成了!

2. 第二個(gè)域名/網(wǎng)站配置

現(xiàn)在,我們來(lái)配置第二個(gè)網(wǎng)站:m.yourmall.com, 假設(shè) m.yourmall.com 這個(gè)網(wǎng)站它是 www.yourmall.com 的二級(jí)域名網(wǎng)站,主要用于服務(wù)主站 www.yourmall.com 的移動(dòng)端訪問(wèn)用戶。

打開(kāi)Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 塊下加入以下子塊server,如下配置內(nèi)容所示 -

    #  手機(jī)/移動(dòng)端網(wǎng)站 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;
        }

    }

現(xiàn)在,完整的Nginx配置文件:/usr/local/nginx/conf/nginx.conf的內(nèi)容如下所示 -


#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;
        }

    }


    #  手機(jī)/移動(dòng)端網(wǎng)站 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 下創(chuàng)建一個(gè)文件:index.html用于測(cè)試網(wǎng)站的配置情況,其代碼如下 -

<!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>歡迎您訪問(wèn):m.yourmall.com </h1>
<p>這是 Yourmall.com 主站的子域名:<a href="http://www.yiibai.com/">m.yourmall.com</a> ,僅用于Nginx的虛擬機(jī)配置測(cè)試演示。</p>

<p>
<a href="http://www.yiibai.com/">m.yourmall.com</a>.</p>

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

現(xiàn)在,重新加載Nginx配置文件,在加載配置文件之前,最好先測(cè)試一下配置文件語(yǔ)法是否正確,使用以下命令來(lái)測(cè)試 -

[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]#

然后執(zhí)行重新加載Nginx配置文件 -

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

到了這一步,我們來(lái)看看網(wǎng)站 m.yourmall.com 是否配置成功,接下來(lái)我們要訪問(wèn)這個(gè)域名,看能不能看到我們?cè)谏厦鎸?xiě)入的 index.html 文件中的內(nèi)容。在測(cè)試訪問(wèn)m.yourmall.com 域名之前,我們還要對(duì)這個(gè)域名進(jìn)行映射(外網(wǎng)稱為解析)。在局域網(wǎng)內(nèi)的一臺(tái)Windows計(jì)算機(jī)上打開(kāi)文件 C:\Windows\System32\drivers\hosts,在這個(gè)文件的最后一行加入以下內(nèi)容 -

192.168.0.134    m.yourmall.com

注意:在這里,192.168.0.134 是配置Nginx服務(wù)器的IP,如果你的服務(wù)器不是這個(gè)IP,請(qǐng)寫(xiě)上對(duì)應(yīng)的服務(wù)器IP。

現(xiàn)在,打開(kāi)瀏覽器,訪問(wèn)以下網(wǎng)站域名: m.yourmall.com 沒(méi)有錯(cuò)誤,應(yīng)該會(huì)看到以下界面 -

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

3. 第三個(gè)域名/網(wǎng)站配置

接下來(lái),我們來(lái)配置第三個(gè)網(wǎng)站:www.yourcrm.com, 假設(shè) www.yourcrm.com 這個(gè)網(wǎng)站它是一個(gè)客戶管理系統(tǒng),主要記錄客戶信息的管理網(wǎng)站。

注意:配置這個(gè)網(wǎng)站與第一個(gè)網(wǎng)站類似,只是指定/使用文件目錄不太一樣。

打開(kāi)Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 塊下加入以下子塊server,如下配置內(nèi)容所示 -

    # 客戶管理系統(tǒng) 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;
        }

    }

現(xiàn)在,完整的Nginx配置文件:/usr/local/nginx/conf/nginx.conf的內(nèi)容如下所示 -


#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;
        }

    }


    #  手機(jī)/移動(dòng)端網(wǎng)站 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;
        }

    }


    # 客戶管理系統(tǒng) 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 下創(chuàng)建一個(gè)文件:index.html用于測(cè)試網(wǎng)站的配置情況,其代碼如下 -

<!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>歡迎您訪問(wèn):yourcrm.com </h1>
<p>這是 yourcrm.com 網(wǎng)站的首頁(yè) ,僅用于Nginx的虛擬機(jī)配置測(cè)試演示。</p>

<p>
<a href="http://www.yiibai.com/">yourcrm.com</a>.</p>

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

現(xiàn)在,重新加載Nginx配置文件,在加載配置文件之前,最好先測(cè)試一下配置文件語(yǔ)法是否正確,使用以下命令來(lái)測(cè)試 -

[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]#

然后執(zhí)行重新加載Nginx配置文件 -

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

到了這一步,我們來(lái)看看網(wǎng)站 www.yourcrm.com 是否配置成功,接下來(lái)我們要訪問(wèn)這個(gè)域名,看能不能看到我們?cè)谏厦鎸?xiě)入的 index.html 文件中的內(nèi)容。在測(cè)試訪問(wèn)www.yourcrm.com 域名之前,我們還要對(duì)這個(gè)域名進(jìn)行映射(外網(wǎng)稱為解析)。在局域網(wǎng)內(nèi)的一臺(tái)Windows計(jì)算機(jī)上打開(kāi)文件 C:\Windows\System32\drivers\hosts,在這個(gè)文件的最后一行加入以下內(nèi)容 -

192.168.0.134    www.yourcrm.com
192.168.0.134    yourcrm.com

注意:在這里,192.168.0.134 是配置Nginx服務(wù)器的IP,如果你的服務(wù)器不是這個(gè)IP,請(qǐng)寫(xiě)上對(duì)應(yīng)的服務(wù)器IP。

現(xiàn)在,打開(kāi)瀏覽器,訪問(wèn)以下網(wǎng)站域名: www.yourcrm.comyourcrm.com ,沒(méi)有錯(cuò)誤,應(yīng)該會(huì)看到以下界面 -

就這樣,第三個(gè)網(wǎng)站的配置完成了!

4. 優(yōu)化配置

經(jīng)過(guò)了上面三個(gè)網(wǎng)站的配置,相信你可能覺(jué)得 Nginx 的配置文件:/usr/local/nginx/conf/nginx.conf 內(nèi)容有點(diǎn)多了,這還只是最簡(jiǎn)單的配置(還未包函重寫(xiě),日志記錄等規(guī)則),從頭看到尾多少有點(diǎn)不太方便,能不能有更好的辦法解決這個(gè)問(wèn)題? 當(dāng)然可以。我們可以把每個(gè)網(wǎng)站的配置獨(dú)立成一個(gè)配置文件,然后在主配置文件中使用 include 指令包函進(jìn)來(lái)。

現(xiàn)在來(lái)看看怎么修改配置,首先分別創(chuàng)建三個(gè)網(wǎng)站的配置文件:

  • 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

再將上面配置中對(duì)應(yīng)每個(gè)網(wǎng)站的文件包括放入主配置文件。例如,對(duì)于文件 /usr/local/nginx/conf/vhosts/yourmall.conf 放置的文件內(nèi)容如下:

#  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;
    }

}

其它兩個(gè)網(wǎng)站也同樣放入對(duì)應(yīng)配置。

在主配置文件:/usr/local/nginx/conf/nginx.conf 中,包函上述三個(gè)文件即可。現(xiàn)在文件:/usr/local/nginx/conf/nginx.conf的內(nèi)容如下所示 -


#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;
        }

    }

    # 網(wǎng)站配置
    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配置文件,在加載配置文件之前,最好先測(cè)試一下配置文件語(yǔ)法是否正確,使用以下命令來(lái)測(cè)試 -

[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]#

然后執(zhí)行重新加載Nginx配置文件 -

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

到此,在Nginx上配置虛擬機(jī)演示實(shí)例就完了。


上一篇:Nginx配置日志下一篇:Nginx教程