国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

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

2024-08-30 12:30:01
字體:
來源:轉載
供稿:網友

Nginx配置虛擬主機支持3種方式:基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。

1、基于IP的虛擬主機配置

如果同一臺服務器有多個IP,可以使用基于IP的虛機主機配置,將不同的服務綁定在不同的IP上。

1.1 假設服務器有個IP地址為192.168.2.150,首先使用ifconfig在同一個網絡接口上綁定其他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 ~]# ifconfigens33: 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 0ens33: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/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1  localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.2.151 www.test151.com192.168.2.152 www.test152.com192.168.2.153 www.test153.com

可以模擬實現DNS輪詢的情況。

附:設置完hosts文件后一定要記得執行以下命令使其生效

1、windows下cmd進入命令行

C:/Users/1234>ipconfig /flushdnsWindows IP 配置已成功刷新 DNS 解析緩存。

1.3 建立虛擬主機存放網頁的根目錄,并創建首頁文件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]# ls151 152 153

1.4 修改nginx.conf,將虛擬主機配置文件包含進主文件

[root@localhost /]# cd /usr/local/nginx/conf/[root@localhost conf]# lsfastcgi.conf  fastcgi_params  koi-utf mime.types  nginx.conf  scgi_params  uwsgi_params  win-utffastcgi.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;}

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 創建日志文件,否則無法啟動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.logwww.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 -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful# 啟動nginx[root@localhost sbin]# ./nginx

1.8 測試文件

[root@localhost sbin]# curl www.test151.com192.168.2.151[root@localhost sbin]# curl www.test152.com192.168.2.152[root@localhost sbin]# curl www.test153.com192.168.2.153

附:配置過程中出現的問題

1、測試配置文件時出現的問題

[root@localhost sbin]# ./nginx -tnginx: [emerg] unexpected "}" in /usr/local/nginx/conf/nginx.conf:122nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

解決方法:下面語句忘記加分號

include vhost/*.conf;

2、使用 curl www.test*.com 測試時總是訪問到相同的結果

解決方法:不要將IP地址寫在 server_name 后面, server_name 后面只能添加域名。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到服務器教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永嘉县| 安吉县| 南开区| 马关县| 东辽县| 佛山市| 玉林市| 嘉禾县| 大关县| 阿克陶县| 合肥市| 嫩江县| 深州市| 罗江县| 高阳县| 云林县| 漳平市| 甘肃省| 稷山县| 夏津县| 福鼎市| 孝昌县| 石河子市| 榆林市| 玛纳斯县| 宁德市| 谷城县| 屯昌县| 安达市| 固阳县| 区。| 灵台县| 资阳市| 朝阳市| 西贡区| 万盛区| 报价| 牙克石市| 广水市| 常熟市| 来安县|