在上一節中,咱們學習了Nginx+PHP7+MySQL的安裝配置,在這一篇文章中,咱們來學習如何在一個Nginx服務器中配置多個站點。php
假要你所在的公司因爲預算問題,如今只能提供一臺服務器,可是有如下幾個網站:www.yourmall.com
, m.yourmall.com
,www.yourcrm.com
須要部署,而且你已經根據咱們上一篇文章中安裝配置了,實現如下需求。html
www.yourmall.com
是公司的電子商務平臺。m.yourmall.com
是公司的電子商務平臺子站,用於移動端用戶的訪問。www.yourcrm.com
是公司的用戶關係管理系統。如今爲以上網站在服務器上規劃目錄,建立一個目錄:/var/sites
,爲上面三個站點分別建立一個目錄:nginx
www.yourmall.com
對應建立的目錄爲: /var/sites/yourmall
m.yourmall.com
對應建立的目錄爲: /var/sites/m_yourmall
www.yourcrm.com
對應建立的目錄爲: /var/sites/yourcrm
以下圖所示 -shell
[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; } }
如今,完整的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
用於測試網站的配置狀況,其代碼以下 -session
<!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配置文件,在加載配置文件以前,最好先測試一下配置文件語法是否正確,使用如下命令來測試 -app
[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配置文件 -yii
[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
,在這個文件的最後一行加入如下內容 -tcp
192.168.0.134 www.yourmall.com 192.168.0.134 yourmall.com
注意:在這裏,
192.168.0.134
是配置Nginx服務器的IP,若是你的服務器不是這個IP,請寫上對應的服務器IP。
如今,打開瀏覽器,訪問如下網站域名: www.yourmall.com
或 yourmall.com
,沒有錯誤,應該會看到如下界面 -
就這樣,第一個網站的配置完成了!
如今,咱們來配置第二個網站: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
沒有錯誤,應該會看到如下界面 -
就這樣,第二個網站(子域名)的配置完成了!
接下來,咱們來配置第三個網站: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.com
或 yourcrm.com
,沒有錯誤,應該會看到如下界面 -
就這樣,第三個網站的配置完成了!
通過了上面三個網站的配置,相信你可能以爲 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上配置虛擬機演示實例就完了。