詳解Nginx 虛擬主機配置的三種方式(基于IP)
Nginx配置虛擬主機支持3種方式主要有基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。本篇文章主要介紹了基于端口的實現,感興趣的小伙伴們可以參考一下...
2、Nginx基于端口的虛擬主機配置
如一臺服務器只有一個IP或需要通過不同的端口訪問不同的虛擬主機,可以使用基于端口的虛擬主機配置。
2.1 假設服務器有個IP地址為192.168.2.154
- [root@localhost conf]- # ifconfig ens33:4 192.168.2.154/24 up
- [root@localhost conf]- # ifconfig
- ens33:4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
- inet 192.168.2.154 netmask 255.255.255.0 broadcast 192.168.2.255
- ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)
2.2 需要配置的虛擬主機分別為7081、8081和9081,配置主機的host文件便于測試。
- [root@localhost conf]- # vim /etc/hosts
- [root@localhost conf]- # cat /etc/hosts|grep 192.168.2.154
- 192.168.2.154 www.test154.com
2.3 建立虛擬主機存放網頁的根目錄,并創建首頁文件index.html
- [root@localhost conf]- # cd /data/www/
- [root@localhost www]- # mkdir port
- [root@localhost www]- # cd port/
- [root@localhost port]- # mkdir 7081 8081 9081
- [root@localhost port]- # ls
- 7081 8081 9081
- [root@localhost port]- # echo "port 7081" > 7081/index.html
- [root@localhost port]- # echo "port 8081" > 8081/index.html
- [root@localhost port]- # echo "port 9081" > 9081/index.html
2.4 修改nginx.conf,將虛擬主機配置文件包含進主文件
- [root@localhost /]- # cd /usr/local/nginx/conf/
- [root@localhost conf]- # ls
- fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
- fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
- [root@localhost conf]- # vim nginx.conf
在nginx.conf文件末尾加入以下配置
- # 在http段中找到以下內容并刪除每行前面的“#”
- log_format main- '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"'- ;
- # 配置文件結尾的最后一個“}”之前加入以下語句,如下所示
- include vhost/*.conf
2.5 編輯每個端口的配置文件
- [root@localhost vhost]- # vim www.test154.7081.conf
- [root@localhost vhost]- # cat www.test154.7081.conf
- server {
- listen 192.168.2.154:7081;
- # 配置成實際的域名,每個虛擬主機的配置文件域名都相同
- #server_name www.test.com;
- Access_log- /data/logs/www- .test154.7081.log main;
- error_log- /data/logs/www- .test154.7081.error.log;
- location / {
- root- /data/www/port/7081- ;
- index index.html index.htm;
- }
- }
- [root@localhost vhost]- # vim www.test154.8081.conf
- [root@localhost vhost]- # cat www.test154.8081.conf
- server {
- listen 192.168.2.154:8081;
- # 配置成實際的域名,每個虛擬主機的配置文件域名都相同
- #server_name www.test.com;
- access_log- /data/logs/www- .test154.8081.log main;
- error_log- /data/logs/www- .test154.8081.error.log;
- location / {
- root- /data/www/port/8081- ;
- index index.html index.htm;
- }
- }
- [root@localhost vhost]- # vim www.test154.9081.conf
- [root@localhost vhost]- # cat www.test154.9081.conf
- server {
- listen 192.168.2.154:9081;
- # 配置成實際的域名,每個虛擬主機的配置文件域名都相同
- #server_name www.test.com;
- access_log- /data/logs/www- .test154.9081.log main;
- error_log- /data/logs/www- .test154.9081.error.log;
- location / {
- root- /data/www/port/9081- ;
- index index.html index.htm;
- }
- }
2.6 創建日志文件,否則無法啟動nginx
- [root@localhost /]- # mkdir -p /data/logs
- [root@localhost /]- # touch /data/logs/www.test154.7081.log
- [root@localhost /]- # touch /data/logs/www.test154.7081.error.log
- [root@localhost /]- # touch /data/logs/www.test154.8081.log
- [root@localhost /]- # touch /data/logs/www.test154.8081.error.log
- [root@localhost /]- # touch /data/logs/www.test154.9081.log
- [root@localhost /]- # touch /data/logs/www.test154.9081.error.log
- [root@localhost /]- # ls /data/logs/
- www.test154.7081.error.log www.test154.8081.error.log www.test154.9081.error.log
- www.test154.7081.log www.test154.8081.log www.test154.9081.log
2.7 先測試配置文件然后再啟動nginx
- [root@localhost /]- # cd /usr/local/nginx/sbin/
- [root@localhost 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
- # 啟動nginx
- [root@localhost sbin]- # ./nginx
2.8 測試文件
- [root@localhost ~]- # curl http://www.test154.com:7081
- port 7081
- [root@localhost ~]- # curl http://www.test154.com:8081
- port 8081
- [root@localhost ~]- # curl http://www.test154.com:9081
- port 9081
附:配置過程中的問題
1、最后測試時發生的問題
- [root@localhost sbin]- # curl http://www.test154.com:7081
- curl: (7) Failed connect to www.test154.com:7081; 拒絕連接
- [root@localhost sbin]- # curl 192.168.2.154:7081
- curl: (7) Failed connect to 192.168.2.154:7081; 拒絕連接
解決方法:
1.1 使用以下命令查看Nginx是否在監聽相應的端口
- [root@localhost conf]- # netstat -lnt
- Active Internet connections (only servers)
- Proto Recv-Q Send-Q Local Address Foreign Address State
- tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
- tcp 0 0 192.168.2.153:80 0.0.0.0:* LISTEN
- tcp 0 0 192.168.2.152:80 0.0.0.0:* LISTEN
- tcp 0 0 192.168.2.151:80 0.0.0.0:* LISTEN
- tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
- tcp 0 0 192.168.2.154:8081 0.0.0.0:* LISTEN
- tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
- tcp 0 0 192.168.2.154:9081 0.0.0.0:* LISTEN
- tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
- tcp 0 0 192.168.2.154:7081 0.0.0.0:* LISTEN
- tcp6 0 0 :::111 :::* LISTEN
- tcp6 0 0 :::22 :::* LISTEN
- tcp6 0 0 :::23 :::* LISTEN
- tcp6 0 0 ::1:25 :::* LISTEN
1.2 若Nginx未監聽相應端口則重啟Nginx服務,再不行重啟服務器
以上就是詳解Nginx 虛擬主機配置的三種方式(基于IP)的全部內容,希望對大家的學習有所幫助,也希望大家多多支持IT科技網。
  - 詳解Nginx 虛擬主機配置的三種方式(基于端口)- Nginx配置虛擬主機支持3種方式主要有基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。本篇文章主要介紹了基于端口的實現,感興趣的小伙伴們可以參考一下... 
  - Nginx 虛擬主機配置的三種方式(基于域名)- Nginx配置虛擬主機支持3種方式:基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。本文主要介紹了基于域名的實現,感興趣的小伙伴們可以參考一下。3、Nginx... 
  - Nginx服務器中accept鎖的機制與實現詳解- 文章主要給大家介紹了關于Nginx中accept鎖的機制與實現的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,... 
  - Nginx利用Lua+Redis實現動態封禁IP的方法- 文章主要介紹了Linux系統下Nginx支持ipv6的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧一、查看現有nginx是否支持ipv6需要執行以下命令,... 
  - Linux系統服務器下Nginx支持ipv6配置的方法- 文章主要介紹了Linux系統下Nginx支持ipv6的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧一、查看現有nginx是否支持ipv6需要執行以下命令,... 
  - Linux服務器下 php7安裝redis的方法- 文章主要介紹了Linux下 php7安裝redis的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下安裝redis服務1 下載redis cd /usr/local/ 進入安裝目錄 wget http://dow... 
  - Nginx單IP地址配置多個SSL證書的方法示例- 文章主要介紹了Nginx單IP地址配置多個SSL證書的方法示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧默認情況下,Nginx一個IP地址僅支持一個SSL... 
  - 利用nginx和騰訊云免費證書制作https的方法- 文章主要介紹了利用nginx和騰訊云免費證書制作https的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧之前一直在研究,https怎么弄。最近看到... 
  - Vue.js請求JSON Server服務器數據的實現方法- 文章主要介紹了Vue.js請求JSON Server服務器數據的實現方法,需要的朋友可以參考下。由于這里是在之前《vue.js中的vue-resource示例詳解》的基礎上進行稍加修改完成的,因而其... 
  - NVIDIA GPU服務器升級:16塊450W Tesla V100- NVIDIA今天發布了升級版的GPU計算服務器“DGX-2H”,和上代DGX-2一樣配備多達16顆Tesla V100 GPU,但熱設計功耗從350W開放到450W,性能更上一層樓。Tesla V100是迄今為... 

