詳解Nginx 虛擬主機配置的三種方式(基於埠)

佚名發表於2018-12-19
Nginx配置虛擬主機支援3種方式主要有基於IP的虛擬主機配置,基於埠的虛擬主機配置,基於域名的虛擬主機配置。本篇文章主要介紹了基於埠的實現,感興趣的小夥伴們可以參考一下。

2、Nginx基於埠的虛擬主機配置

如一臺伺服器只有一個IP或需要透過不同的埠訪問不同的虛擬主機,可以使用基於埠的虛擬主機配置。

2.1 假設伺服器有個IP地址為192.168.2.154

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

2.2 需要配置的虛擬主機分別為7081、8081和9081,配置主機的host檔案便於測試。

  1. [root@localhost conf]# vim /etc/hosts
  2. [root@localhost conf]# cat /etc/hosts|grep 192.168.2.154
  3. 192.168.2.154 www.test154.com

2.3 建立虛擬主機存放網頁的根目錄,並建立首頁檔案index.html

  1. [root@localhost conf]# cd /data/www/
  2. [root@localhost www]# mkdir port
  3. [root@localhost www]# cd port/
  4. [root@localhost port]# mkdir 7081 8081 9081
  5. [root@localhost port]# ls
  6. 7081 8081 9081
  7. [root@localhost port]# echo "port 7081" > 7081/index.html
  8. [root@localhost port]# echo "port 8081" > 8081/index.html
  9. [root@localhost port]# echo "port 9081" > 9081/index.html

2.4 修改nginx.conf,將虛擬主機配置檔案包含進主檔案

  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段中找到以下內容並刪除每行前面的“#”
  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. # 配置檔案結尾的最後一個“}”之前加入以下語句,如下所示
  7. include vhost/*.conf

2.5 編輯每個埠的配置檔案

  1. [root@localhost vhost]# vim www.test154.7081.conf
  2. [root@localhost vhost]# cat www.test154.7081.conf
  3.  server {
  4.  listen 192.168.2.154:7081;
  5.  # 配置成實際的域名,每個虛擬主機的配置檔案域名都相同
  6.  #server_name www.test.com;
  7.  
  8.  Access_log /data/logs/www.test154.7081.log main;
  9.  error_log /data/logs/www.test154.7081.error.log;
  10.  
  11.  location / {
  12.   root /data/www/port/7081;
  13.   index index.html index.htm;
  14.  }
  15.  }
  16.  
  17. [root@localhost vhost]# vim www.test154.8081.conf
  18. [root@localhost vhost]# cat www.test154.8081.conf
  19.  server {
  20.  listen 192.168.2.154:8081;
  21.  # 配置成實際的域名,每個虛擬主機的配置檔案域名都相同
  22.  #server_name www.test.com;
  23.  
  24.  access_log /data/logs/www.test154.8081.log main;
  25.  error_log /data/logs/www.test154.8081.error.log;
  26.  
  27.  location / {
  28.   root /data/www/port/8081;
  29.   index index.html index.htm;
  30.  }
  31.  }
  32.  
  33. [root@localhost vhost]# vim www.test154.9081.conf
  34. [root@localhost vhost]# cat www.test154.9081.conf
  35.  server {
  36.  listen 192.168.2.154:9081;
  37.  # 配置成實際的域名,每個虛擬主機的配置檔案域名都相同
  38.  #server_name www.test.com;
  39.  
  40.  access_log /data/logs/www.test154.9081.log main;
  41.  error_log /data/logs/www.test154.9081.error.log;
  42.  
  43.  location / {
  44.   root /data/www/port/9081;
  45.   index index.html index.htm;
  46.  }
  47.  }

2.6 建立日誌檔案,否則無法啟動nginx

  1. [root@localhost /]# mkdir -p /data/logs
  2. [root@localhost /]# touch /data/logs/www.test154.7081.log
  3. [root@localhost /]# touch /data/logs/www.test154.7081.error.log
  4. [root@localhost /]# touch /data/logs/www.test154.8081.log
  5. [root@localhost /]# touch /data/logs/www.test154.8081.error.log
  6. [root@localhost /]# touch /data/logs/www.test154.9081.log
  7. [root@localhost /]# touch /data/logs/www.test154.9081.error.log
  8. [root@localhost /]# ls /data/logs/
  9. www.test154.7081.error.log www.test154.8081.error.log www.test154.9081.error.log
  10. www.test154.7081.log www.test154.8081.log www.test154.9081.log

2.7 先測試配置檔案然後再啟動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. # 啟動nginx
  6. [root@localhost sbin]# ./nginx

2.8 測試檔案

  1. [root@localhost ~]# curl http://www.test154.com:7081
  2. port 7081
  3. [root@localhost ~]# curl http://www.test154.com:8081
  4. port 8081
  5. [root@localhost ~]# curl http://www.test154.com:9081
  6. port 9081

附:配置過程中的問題

1、最後測試時發生的問題

  1. [root@localhost sbin]# curl http://www.test154.com:7081
  2. curl: (7) Failed connect to www.test154.com:7081; 拒絕連線
  3. [root@localhost sbin]# curl 192.168.2.154:7081
  4. curl: (7) Failed connect to 192.168.2.154:7081; 拒絕連線

解決方法:

1.1 使用以下命令檢視Nginx是否在監聽相應的埠

  1. [root@localhost conf]# 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.153:80 0.0.0.0:*  LISTEN
  6. tcp 0 0 192.168.2.152:80 0.0.0.0:*  LISTEN
  7. tcp 0 0 192.168.2.151:80 0.0.0.0:*  LISTEN
  8. tcp 0 0 0.0.0.0:8080  0.0.0.0:*  LISTEN
  9. tcp 0 0 192.168.2.154:8081 0.0.0.0:*  LISTEN
  10. tcp 0 0 0.0.0.0:22  0.0.0.0:*  LISTEN
  11. tcp 0 0 192.168.2.154:9081 0.0.0.0:*  LISTEN
  12. tcp 0 0 127.0.0.1:25  0.0.0.0:*  LISTEN
  13. tcp 0 0 192.168.2.154:7081 0.0.0.0:*  LISTEN
  14. tcp6 0 0 :::111   :::*   LISTEN
  15. tcp6 0 0 :::22   :::*   LISTEN
  16. tcp6 0 0 :::23   :::*   LISTEN
  17. tcp6 0 0 ::1:25   :::*   LISTEN

1.2 若Nginx未監聽相應埠則重啟Nginx服務,再不行重啟伺服器

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援IT科技網。

相關文章