在上一節(jié)中,我們學習了Nginx+PHP7+MySQL的安裝配置,在這一篇文章中,我們來學習如何在一個Nginx服務器中配置多個站點。
假要你所在的公司由于預算問題,現(xiàn)在只能提供一臺服務器,但是有以下幾個網(wǎng)站:www.yourmall.com, m.yourmall.com,www.yourcrm.com需要部署,并且你已經根據(jù)我們上一篇文章中安裝配置了,實現(xiàn)以下需求。
www.yourmall.com是公司的電子商務平臺。m.yourmall.com 是公司的電子商務平臺子站,用于移動端用戶的訪問。www.yourcrm.com 是公司的用戶關系管理系統(tǒng)。現(xiàn)在為以上網(wǎng)站在服務器上規(guī)劃目錄,創(chuàng)建一個目錄:/var/sites,為上面三個站點分別創(chuàng)建一個目錄:
www.yourmall.com對應創(chuàng)建的目錄為: /var/sites/yourmallm.yourmall.com 對應創(chuàng)建的目錄為: /var/sites/m_yourmallwww.yourcrm.com 對應創(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]#
打開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;
}
}
現(xiàn)在,完整的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 下創(chuàng)建一個文件:index.html用于測試網(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>歡迎您訪問: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>
現(xiàn)在,重新加載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]#
然后執(zhí)行重新加載Nginx配置文件 -
[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#
到了這一步,我們來看看網(wǎng)站 www.yourmall.com 是否配置成功,接下來我們要訪問這個域名,看能不能看到我們在上面寫入的 index.html 文件中的內容。在測試訪問www.yourmall.com 域名之前,我們還要對這個域名進行映射(外網(wǎng)稱為解析)。在局域網(wǎng)內的一臺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。
現(xiàn)在,打開瀏覽器,訪問以下網(wǎng)站域名: www.yourmall.com 或 yourmall.com ,沒有錯誤,應該會看到以下界面 -

就這樣,第一個網(wǎng)站的配置完成了!
現(xiàn)在,我們來配置第二個網(wǎng)站:m.yourmall.com, 假設 m.yourmall.com 這個網(wǎng)站它是 www.yourmall.com 的二級域名網(wǎng)站,主要用于服務主站 www.yourmall.com 的移動端訪問用戶。
打開Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 塊下加入以下子塊server,如下配置內容所示 -
# 手機/移動端網(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的內容如下所示 -
#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;
}
}
# 手機/移動端網(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)建一個文件:index.html用于測試網(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>歡迎您訪問: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>
現(xiàn)在,重新加載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]#
然后執(zhí)行重新加載Nginx配置文件 -
[root@localhost sites]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost sites]#
到了這一步,我們來看看網(wǎng)站 m.yourmall.com 是否配置成功,接下來我們要訪問這個域名,看能不能看到我們在上面寫入的 index.html 文件中的內容。在測試訪問m.yourmall.com 域名之前,我們還要對這個域名進行映射(外網(wǎng)稱為解析)。在局域網(wǎng)內的一臺Windows計算機上打開文件 C:\Windows\System32\drivers\hosts,在這個文件的最后一行加入以下內容 -
192.168.0.134 m.yourmall.com
注意:在這里,
192.168.0.134是配置Nginx服務器的IP,如果你的服務器不是這個IP,請寫上對應的服務器IP。
現(xiàn)在,打開瀏覽器,訪問以下網(wǎng)站域名: m.yourmall.com 沒有錯誤,應該會看到以下界面 -

就這樣,第二個網(wǎng)站(子域名)的配置完成了!
接下來,我們來配置第三個網(wǎng)站:www.yourcrm.com, 假設 www.yourcrm.com 這個網(wǎng)站它是一個客戶管理系統(tǒng),主要記錄客戶信息的管理網(wǎng)站。
注意:配置這個網(wǎng)站與第一個網(wǎng)站類似,只是指定/使用文件目錄不太一樣。
打開Nginx的配置文件:/usr/local/nginx/conf/nginx.conf ,并在 http 塊下加入以下子塊server,如下配置內容所示 -
# 客戶管理系統(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的內容如下所示 -
#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;
}
}
# 手機/移動端網(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)建一個文件:index.html用于測試網(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>歡迎您訪問:yourcrm.com </h1>
<p>這是 yourcrm.com 網(wǎng)站的首頁 ,僅用于Nginx的虛擬機配置測試演示。</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配置文件,在加載配置文件之前,最好先測試一下配置文件語法是否正確,使用以下命令來測試 -
[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]#
到了這一步,我們來看看網(wǎng)站 www.yourcrm.com 是否配置成功,接下來我們要訪問這個域名,看能不能看到我們在上面寫入的 index.html 文件中的內容。在測試訪問www.yourcrm.com 域名之前,我們還要對這個域名進行映射(外網(wǎng)稱為解析)。在局域網(wǎng)內的一臺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。
現(xiàn)在,打開瀏覽器,訪問以下網(wǎng)站域名: www.yourcrm.com 或 yourcrm.com ,沒有錯誤,應該會看到以下界面 -

就這樣,第三個網(wǎng)站的配置完成了!
經過了上面三個網(wǎng)站的配置,相信你可能覺得 Nginx 的配置文件:/usr/local/nginx/conf/nginx.conf 內容有點多了,這還只是最簡單的配置(還未包函重寫,日志記錄等規(guī)則),從頭看到尾多少有點不太方便,能不能有更好的辦法解決這個問題? 當然可以。我們可以把每個網(wǎng)站的配置獨立成一個配置文件,然后在主配置文件中使用 include 指令包函進來。
現(xiàn)在來看看怎么修改配置,首先分別創(chuàng)建三個網(wǎng)站的配置文件:
www.yourmall.com -> /usr/local/nginx/conf/vhosts/yourmall.confm.yourmall.com -> /usr/local/nginx/conf/vhosts/m_yourmall.confwww.yourcrm.com -> /usr/local/nginx/conf/vhosts/yourcrm.conf再將上面配置中對應每個網(wǎng)站的文件包括放入主配置文件。例如,對于文件 /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;
}
}
其它兩個網(wǎng)站也同樣放入對應配置。
在主配置文件:/usr/local/nginx/conf/nginx.conf 中,包函上述三個文件即可?,F(xiàn)在文件:/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;
}
}
# 網(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配置文件,在加載配置文件之前,最好先測試一下配置文件語法是否正確,使用以下命令來測試 -
[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上配置虛擬機演示實例就完了。