詳解Nginx 虛擬主機配置的三種方式(基于IP)
Nginx配置虛擬主機支持3種方式:基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。
詳解Nginx 虛擬主機配置的三種方式(基于端口) https://www.jb51.net/article/14977.htm
詳解Nginx 虛擬主機配置的三種方式(基于域名) https://www.jb51.net/article/14978.htm
1、基于IP的虛擬主機配置
如果同一臺服務器有多個IP,可以使用基于IP的虛機主機配置,將不同的服務綁定在不同的IP上。
1.1 假設(shè)服務器有個IP地址為192.168.2.150,首先使用ifconfig在同一個網(wǎng)絡接口上綁定其他3個IP。
[root@localhost ~]# ifconfig ens33:1 192.168.2.151/24 up [root@localhost ~]# ifconfig ens33:2 192.168.2.152/24 up [root@localhost ~]# ifconfig ens33:3 192.168.2.153/24 up [root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.106 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::2a8d:be6:a4a8:ea0 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) RX packets 1220 bytes 87955 (85.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 206 bytes 23755 (23.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.151 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.152 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) ens33:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.153 netmask 255.255.255.0 broadcast 192.168.2.255 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 72 bytes 6252 (6.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 72 bytes 6252 (6.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.2 3個IP對應的域名如下,配置主機的host文件便于測試
[root@localhost ~]# vim /etc/hosts [root@localhost ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.151 www.test151.com 192.168.2.152 www.test152.com 192.168.2.153 www.test153.com
可以模擬實現(xiàn)DNS輪詢的情況。
附:設(shè)置完hosts文件后一定要記得執(zhí)行以下命令使其生效
1、windows下cmd進入命令行
C:\Users\1234>ipconfig /flushdns Windows IP 配置 已成功刷新 DNS 解析緩存。
1.3 建立虛擬主機存放網(wǎng)頁的根目錄,并創(chuàng)建首頁文件index.html
[root@localhost /]# mkdir -p /data/www [root@localhost /]# cd /data/www [root@localhost www]# mkdir 151 [root@localhost www]# mkdir 152 [root@localhost www]# mkdir 153 [root@localhost www]# echo "192.168.2.151" > 151/index.html [root@localhost www]# echo "192.168.2.152" > 152/index.html [root@localhost www]# echo "192.168.2.153" > 153/index.html [root@localhost www]# ls 151 152 153
1.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段中找到以下內(nèi)容并刪除每行前面的“#” log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_age
1.5 編輯每個IP的配置文件(每個虛擬主機的配置文件)
[root@localhost conf]# mkdir -p vhost [root@localhost conf]# cd vhost/ [root@localhost vhost]# cat www.test151.conf server { listen 192.168.2.151:80; # 配置成實際的域名,每個虛擬主機的配置文件域名都相同 #server_name www.test.com; access_log /data/logs/www.test151.com.log main; error_log /data/logs/www.test151.com.error.log; location / { root /data/www/151; index index.html index.htm; } } [root@localhost vhost]# cat www.test152.conf server { listen 192.168.2.152:80; # 配置成實際的域名,每個虛擬主機的配置文件域名都相同 #server_name www.test.com; access_log /data/logs/www.test152.com.log main; error_log /data/logs/www.test152.com.error.log; location / { root /data/www/152; index index.html index.htm; } } [root@localhost vhost]# cat www.test153.conf server { listen 192.168.2.153:80; # 配置成實際的域名,每個虛擬主機的配置文件域名都相同 #server_name www.test.com; access_log /data/logs/www.test153.com.log main; error_log /data/logs/www.test153.com.error.log; location / { root /data/www/153; index index.html index.htm; } }
1.6 創(chuàng)建日志文件,否則無法啟動nginx
[root@localhost /]# mkdir -p /data/logs [root@localhost /]# touch /data/logs/www.test151.com.log [root@localhost /]# touch /data/logs/www.test151.com.error.log [root@localhost /]# touch /data/logs/www.test152.com.log [root@localhost /]# touch /data/logs/www.test152.com.error.log [root@localhost /]# touch /data/logs/www.test153.com.log [root@localhost /]# touch /data/logs/www.test153.com.error.log [root@localhost /]# ls /data/logs/ www.test151.com.error.log www.test152.com.error.log www.test153.com.error.log www.test151.com.log www.test152.com.log www.test153.com.log
1.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
1.8 測試文件
[root@localhost sbin]# curl www.test151.com 192.168.2.151 [root@localhost sbin]# curl www.test152.com 192.168.2.152 [root@localhost sbin]# curl www.test153.com 192.168.2.153
附:配置過程中出現(xiàn)的問題
1、測試配置文件時出現(xiàn)的問題
[root@localhost sbin]# ./nginx -t nginx: [emerg] unexpected "}" in /usr/local/nginx/conf/nginx.conf:122 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
解決方法:下面語句忘記加分號
include vhost/*.conf;
2、使用 curl www.test*.com 測試時總是訪問到相同的結(jié)果
解決方法:不要將IP地址寫在 server_name 后面, server_name 后面只能添加域名。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。