Nginx 虛擬主機(jī)配置的三種方式(基于域名)

2018-12-19 22:25:19 來(lái)源:B8613A作者:佚名 人氣: 次閱讀 499 條評(píng)論

Nginx配置虛擬主機(jī)支持3種方式:基于IP的虛擬主機(jī)配置,基于端口的虛擬主機(jī)配置,基于域名的虛擬主機(jī)配置。本文主要介紹了基于域名的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下。3、Nginx...

Nginx配置虛擬主機(jī)支持3種方式:基于IP的虛擬主機(jī)配置,基于端口的虛擬主機(jī)配置,基于域名的虛擬主機(jī)配置。本文主要介紹了基于域名的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下

。

3、Nginx基于域名的虛擬主機(jī)配置

使用基于域名的虛擬主機(jī)配置是比較流行的方式,可以在同一個(gè)IP上配置多個(gè)域名并且都通過(guò)80端口訪問(wèn)。

3.1 假設(shè)服務(wù)器有個(gè)IP地址為192.168.2.155

  1. [root@localhost ~]# ifconfig ens33:5 192.168.2.155/24 up
  2. [root@localhost ~]# ifconfig
  3. ens33:5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  4.   inet 192.168.2.155 netmask 255.255.255.0 broadcast 192.168.2.255
  5.   ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)

3.2 192.168.2.155對(duì)應(yīng)的域名如下,配置主機(jī)的host文件便于測(cè)試

  1. [root@localhost ~]# vim /etc/hosts
  2. [root@localhost ~]# cat /etc/hosts|grep 192.168.2.155
  3. 192.168.2.155 www.oa.com
  4. 192.168.2.155 www.bbs.com
  5. 192.168.2.155 www.test.com

3.3 建立虛擬主機(jī)存放網(wǎng)頁(yè)的根目錄,并創(chuàng)建首頁(yè)文件index.html

  1. [root@localhost ~]# cd /data/www/
  2. [root@localhost www]# mkdir www.oa.com
  3. [root@localhost www]# mkdir www.bbs.com
  4. [root@localhost www]# mkdir www.test.com
  5. [root@localhost www]# echo www.oa.com > www.oa.com/index.html
  6. [root@localhost www]# echo www.bbs.com > www.bbs.com/index.html
  7. [root@localhost www]# echo www.test.com > www.test.com/index.html

3.4 修改nginx.conf,將虛擬主機(jī)配置文件包含進(jìn)主文件

  1. [root@localhost /]# cd /usr/local/nginx/conf/
  2. [root@localhost conf]# ls
  3. fastcgi.conf   fastcgi_params   koi-utf mime.types   nginx.conf   scgi_params   uwsgi_params   win-utf
  4. fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
  5. [root@localhost conf]# vim nginx.conf

在nginx.conf文件末尾加入以下配置

  1. # 在http段中找到以下內(nèi)容并刪除每行前面的“#”
  2.  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  3.       '$status $body_bytes_sent "$http_referer" '
  4.       '"$http_user_agent" "$http_x_forwarded_for"';
  5.  
  6. # 配置文件結(jié)尾的最后一個(gè)“}”之前加入以下語(yǔ)句,如下所示
  7. include vhost/*.conf

3.5 編輯每個(gè)域名的配置文件(每個(gè)虛擬主機(jī)的配置文件)

  1. [root@localhost conf]# cd vhost/
  2. [root@localhost vhost]# cat www.oa.com.conf
  3.  server {
  4.   listen  192.168.2.155:80;
  5.   server_name www.oa.com;
  6.  
  7.   Access_log /data/logs/www.oa.com.log main;
  8.   error_log /data/logs/www.oa.com.error.log;
  9.  
  10.   location / {
  11.    root /data/www/www.oa.com;
  12.    index index.html index.htm;
  13.   }
  14.  }
  15.  
  16. [root@localhost vhost]# cat www.bbs.com.conf
  17.  server {
  18.   listen  192.168.2.155:80;
  19.   server_name www.bbs.com;
  20.  
  21.   access_log /data/logs/www.bbs.com.log main;
  22.   error_log /data/logs/www.bbs.com.error.log;
  23.  
  24.   location / {
  25.    root /data/www/www.bbs.com;
  26.    index index.html index.htm;
  27.   }
  28.  }
  29.  
  30. [root@localhost vhost]# cat www.test.com.conf
  31.  server {
  32.   listen  192.168.2.155:80;
  33.   server_name www.test.com;
  34.  
  35.   access_log /data/logs/www.test.com.log main;
  36.   error_log /data/logs/www.test.com.error.log;
  37.  
  38.   location / {
  39.    root /data/www/www.test.com;
  40.    index index.html index.htm;
  41.   }
  42.  }
  43.  
  44. [root@localhost vhost]# cat /data/www/www.oa.com/index.html
  45. www.oa.com
  46. [root@localhost vhost]# cat /data/www/www.bbs.com/index.html
  47. www.bbs.com
  48. [root@localhost vhost]# cat /data/www/www.test.com/index.html
  49. www.test.com
 

3.6 創(chuàng)建日志文件,否則無(wú)法啟動(dòng)nginx

  1. [root@localhost /]# mkdir -p /data/logs
  2. [root@localhost /]# touch /data/logs/www.oa.com.log
  3. [root@localhost /]# touch /data/logs/www.oa.com.error.log
  4. [root@localhost /]# touch /data/logs/www.bbs.com.log
  5. [root@localhost /]# touch /data/logs/www.bbs.com.error.log
  6. [root@localhost /]# touch /data/logs/www.test.com.log
  7. [root@localhost /]# touch /data/logs/www.test.com.error.log
  8. [root@localhost /]# ls /data/logs/
  9. www.oa.com.error.log www.bbs.com.error.log www.test.com.error.log
  10. www.oa.com.log  www.bbs.com.log  www.test.com.log

3.7 先測(cè)試配置文件然后再啟動(dòng)nginx

  1. [root@localhost /]# cd /usr/local/nginx/sbin/
  2. [root@localhost sbin]# ./nginx -t
  3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  5. # 啟動(dòng)nginx
  6. [root@localhost sbin]# ./nginx

3.8 測(cè)試文件

  1. [root@localhost vhost]# curl http://www.oa.com
  2. www.oa.com
  3. [root@localhost vhost]# curl http://www.bbs.com
  4. www.bbs.com
  5. [root@localhost vhost]# curl http://www.test.com
  6. www.test.com

附:配置過(guò)程中的問(wèn)題

1、最后測(cè)試時(shí)發(fā)生的問(wèn)題

  1. [root@localhost ~]# curl http://www.oa.com
  2. curl: (7) Failed connect to www.oa.com:80; 拒絕連接

解決方法: 

查看Nginx是否在監(jiān)聽(tīng)相應(yīng)的端口。

  1. [root@localhost ~]# netstat -lnt
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address   Foreign Address   State
  4. tcp  0  0 0.0.0.0:111    0.0.0.0:*    LISTEN
  5. tcp  0  0 192.168.2.155:80  0.0.0.0:*    LISTEN
  6. tcp  0  0 0.0.0.0:8080   0.0.0.0:*    LISTEN
  7. tcp  0  0 0.0.0.0:22    0.0.0.0:*    LISTEN
  8. tcp  0  0 127.0.0.1:25   0.0.0.0:*    LISTEN
  9. tcp6  0  0 :::111     :::*     LISTEN
  10. tcp6  0  0 :::22     :::*     LISTEN
  11. tcp6  0  0 :::23     :::*     LISTEN
  12. tcp6  0  0 ::1:25     :::*     LISTEN
 

1、配置虛擬主機(jī)文件時(shí)要加上監(jiān)聽(tīng)的IP地址,每個(gè)虛擬主機(jī)配置文件都一樣。

  1. listen  192.168.2.155:80;

2、配置完成后要重啟服務(wù)器

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持IT科技網(wǎng)。