濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 詳解Nginx 虛擬主機(jī)配置的三種方式(基于IP)

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

熱門標(biāo)簽:燃?xì)夤芫€地圖標(biāo)注顏色 話術(shù)外呼系統(tǒng) 哈爾濱400電話去哪辦理 西柏坡地圖標(biāo)注 i智能電話機(jī)器人yeta 廣東如何申請(qǐng)400電話 400電話申請(qǐng)要什么條件 北京人工外呼系統(tǒng)廠家 寧波400電話辦理對(duì)企業(yè)的意義

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

詳解Nginx 虛擬主機(jī)配置的三種方式(基于端口) https://www.jb51.net/article/14977.htm

詳解Nginx 虛擬主機(jī)配置的三種方式(基于域名) https://www.jb51.net/article/14978.htm

1、基于IP的虛擬主機(jī)配置

如果同一臺(tái)服務(wù)器有多個(gè)IP,可以使用基于IP的虛機(jī)主機(jī)配置,將不同的服務(wù)綁定在不同的IP上。

1.1 假設(shè)服務(wù)器有個(gè)IP地址為192.168.2.150,首先使用ifconfig在同一個(gè)網(wǎng)絡(luò)接口上綁定其他3個(gè)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個(gè)IP對(duì)應(yīng)的域名如下,配置主機(jī)的host文件便于測(cè)試

[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

可以模擬實(shí)現(xiàn)DNS輪詢的情況。

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

1、windows下cmd進(jìn)入命令行

C:\Users\1234>ipconfig /flushdns

Windows IP 配置

已成功刷新 DNS 解析緩存。

1.3 建立虛擬主機(jī)存放網(wǎng)頁(yè)的根目錄,并創(chuàng)建首頁(yè)文件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,將虛擬主機(jī)配置文件包含進(jìn)主文件

[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_agent" "$http_x_forwarded_for"';

# 配置文件結(jié)尾的最后一個(gè)“}”之前加入以下語(yǔ)句,如下所示
include vhost/*.conf;
}

1.5 編輯每個(gè)IP的配置文件(每個(gè)虛擬主機(jī)的配置文件)

[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;
 # 配置成實(shí)際的域名,每個(gè)虛擬主機(jī)的配置文件域名都相同
 #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;
 # 配置成實(shí)際的域名,每個(gè)虛擬主機(jī)的配置文件域名都相同
 #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;
 # 配置成實(shí)際的域名,每個(gè)虛擬主機(jī)的配置文件域名都相同
 #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)建日志文件,否則無(wú)法啟動(dò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 先測(cè)試配置文件然后再啟動(dòng)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
# 啟動(dòng)nginx
[root@localhost sbin]# ./nginx

1.8 測(cè)試文件

[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

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

1、測(cè)試配置文件時(shí)出現(xiàn)的問(wè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

解決方法:下面語(yǔ)句忘記加分號(hào)

include vhost/*.conf;

2、使用 curl www.test*.com 測(cè)試時(shí)總是訪問(wèn)到相同的結(jié)果

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

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

標(biāo)簽:西藏 襄陽(yáng) 開(kāi)封 湘潭 巴中 阜陽(yáng) 珠海 張家口

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解Nginx 虛擬主機(jī)配置的三種方式(基于IP)》,本文關(guān)鍵詞  詳解,Nginx,虛擬主機(jī),配置,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《詳解Nginx 虛擬主機(jī)配置的三種方式(基于IP)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于詳解Nginx 虛擬主機(jī)配置的三種方式(基于IP)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    河南省| 刚察县| 壶关县| 南雄市| 梅州市| 雅江县| 柘荣县| 浦城县| 尼玛县| 什邡市| 宝清县| 宜宾县| 维西| 宁津县| 双峰县| 湛江市| 怀柔区| 利津县| 萨迦县| 都兰县| 宜君县| 崇文区| 灌阳县| 乌恰县| 红原县| 益阳市| 正安县| 平果县| 太湖县| 那坡县| 罗城| 黄龙县| 潜山县| 昂仁县| 抚远县| 宝坻区| 罗田县| 察隅县| 墨脱县| 乐都县| 中宁县|