日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx-配置基于ip或域名的虚拟主机

發布時間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx-配置基于ip或域名的虚拟主机 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【0】README:?nginx 可以配置多種類型的虛擬主機:?

類型1) 基于ip的虛擬主機;

類型2)基于域名的虛擬主機;

類型3)基于端口的虛擬主機;


【1】配置基于 ip 的虛擬主機的準備工作

step1) 先用 ifconfig 查看該服務器的ip地址:? ?本服務器有一塊物理網卡設備 eth0 和 本地回環設備lo, eth0 的ip地址為 192.168.186.100 ,本地回環lo的ip地址為 127.0.0.1 ;

eth0 Link encap:Ethernet HWaddr 00:0C:29:71:D7:1A inet addr:192.168.186.100 Bcast:192.168.186.255 Mask:255.255.255.0inet6 addr: fe80::20c:29ff:fe71:d71a/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:1349 errors:0 dropped:0 overruns:0 frame:0TX packets:1155 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:139966 (136.6 KiB) TX bytes:145180 (141.7 KiB)Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING MTU:16436 Metric:1RX packets:4 errors:0 dropped:0 overruns:0 frame:0TX packets:4 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)

step2)??在 eth0 網卡設備上 添加兩個ip別名 192.168.186.101 和 192.168.186.102,可以通過以下的 ifconfig 和 route命令來進行:

[root@localhost nginx]# ifconfig eth0:1 192.168.186.101 broadcast 192.168.186.255 netmask 255.255.255.0 up [root@localhost nginx]# route add -host 192.168.186.101 dev eth0:1 [root@localhost nginx]# [root@localhost nginx]# ifconfig eth0:2 192.168.186.102 broadcast 192.168.186.255 netmask 255.255.255.0 up [root@localhost nginx]# route add -host 192.168.186.102 dev eth0:2

step3)再執行 ifconfig命令,就可以看到 eth0 網卡設備上綁定兩個 ip 別名,如下所示:

[pacoson@localhost ~]$ ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:71:D7:1A inet addr:192.168.186.100 Bcast:192.168.186.255 Mask:255.255.255.0inet6 addr: fe80::20c:29ff:fe71:d71a/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:1349 errors:0 dropped:0 overruns:0 frame:0TX packets:1155 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:139966 (136.6 KiB) TX bytes:145180 (141.7 KiB)Interrupt:19 Base address:0x2000 eth0:1 Link encap:Ethernet HWaddr 00:0C:29:71:D7:1A inet addr:192.168.186.101 Bcast:192.168.186.255 Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1Interrupt:19 Base address:0x2000 eth0:2 Link encap:Ethernet HWaddr 00:0C:29:71:D7:1A inet addr:192.168.186.102 Bcast:192.168.186.255 Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1Interrupt:19 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING MTU:16436 Metric:1RX packets:4 errors:0 dropped:0 overruns:0 frame:0TX packets:4 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)

step4)ping 通, 證明配置無誤

[pacoson@localhost ~]$ ping 192.168.186.101 PING 192.168.186.101 (192.168.186.101) 56(84) bytes of data. 64 bytes from 192.168.186.101: icmp_seq=1 ttl=64 time=4.35 ms 64 bytes from 192.168.186.101: icmp_seq=2 ttl=64 time=0.149 ms 64 bytes from 192.168.186.101: icmp_seq=3 ttl=64 time=0.073 ms 64 bytes from 192.168.186.101: icmp_seq=4 ttl=64 time=0.065 ms 64 bytes from 192.168.186.101: icmp_seq=5 ttl=64 time=0.063 ms 64 bytes from 192.168.186.101: icmp_seq=6 ttl=64 time=0.066 ms 64 bytes from 192.168.186.101: icmp_seq=7 ttl=64 time=0.070 ms 64 bytes from 192.168.186.101: icmp_seq=8 ttl=64 time=0.069 ms ^C --- 192.168.186.101 ping statistics --- 8 packets transmitted, 8 received, 0% packet loss, time 7915ms rtt min/avg/max/mdev = 0.063/0.613/4.352/1.413 ms [pacoson@localhost ~]$ [pacoson@localhost ~]$ [pacoson@localhost ~]$ ping 192.168.186.102 PING 192.168.186.102 (192.168.186.102) 56(84) bytes of data. 64 bytes from 192.168.186.102: icmp_seq=1 ttl=64 time=9.36 ms 64 bytes from 192.168.186.102: icmp_seq=2 ttl=64 time=0.212 ms 64 bytes from 192.168.186.102: icmp_seq=3 ttl=64 time=0.065 ms 64 bytes from 192.168.186.102: icmp_seq=4 ttl=64 time=0.063 ms 64 bytes from 192.168.186.102: icmp_seq=5 ttl=64 time=0.064 ms 64 bytes from 192.168.186.102: icmp_seq=6 ttl=64 time=0.063 ms 64 bytes from 192.168.186.102: icmp_seq=7 ttl=64 time=0.063 ms 64 bytes from 192.168.186.102: icmp_seq=8 ttl=64 time=0.077 ms 64 bytes from 192.168.186.102: icmp_seq=9 ttl=64 time=0.062 ms ^C --- 192.168.186.102 ping statistics --- 9 packets transmitted, 9 received, 0% packet loss, time 8993ms rtt min/avg/max/mdev = 0.062/1.115/9.366/2.917 ms

【注意】ifconfig 和 route 配置的ip別名在服務器重啟后會消失;可以將 ifconfig 和 route 命令添加到 /etc/rc.local 文件中, 讓系統開機時自動運行, 以下是相關命令:

vim /etc/rc.local

在末尾增加以下內容,?

ifconfig eth0:1 192.168.186.101 broadcast 192.168.186.255 netmask 255.255.255.0 up route add -host 192.168.186.101 dev eth0:1 ifconfig eth0:2 192.168.186.102 broadcast 192.168.186.255 netmask 255.255.255.0 up route add -host 192.168.186.102 dev eth0:2


【2】配置基于ip的虛擬主機

1)配置文件 nginx.conf 如下:

# this config is from 3-6 worker_processes 3; events {worker_connections 1024; } http {# first virtual host 第一個虛擬主機server{# ip and port monitored 監聽的ip和端口listen 192.168.186.100:80;# host name 主機名稱server_name 192.168.186.100;# the dir access_log saved 訪問日志文件存放路徑access_log logs/server1.access.log combined;location /{# default index file, priority reduction from left to right# 默認首頁文件,順序從左到右,index index.html index.htm;# the dir html file saved, html網頁文件存放目錄root /data0/htdocs/server1;}}# second virtual hostserver{listen 192.168.186.101:80;server_name 192.168.186.101;access_log logs/server2.access.log combined;location /{index index.html index.htm;root /data0/htdocs/server2;}}# third virtual hostserver {listen 192.168.186.102:80;server_name 192.168.186.102;access_log logs/server3.access.log combined;location /{index index.html index.htm;root /data0/htdocs/server3.com;}} }

【3】配置基于域名的虛擬主機
1)nginx配置文件如下:

http {# 第一個虛擬主機server{# 監聽的端口listen 80;# 主機名稱server_name aaa.domain.com;# 訪問日志文件存放路徑access_log logs/aaa.domain.com.access.log combined;location /{# 默認首頁文件,順序從左到右,如果找不到index.html文件,則查找index.htm文件作為首頁文件index index.html index.htm;# HTML網頁文件存放的目錄root /data0/htdocs/aaa.domain.com;}}# 第二個虛擬主機server{# 監聽的IP和端口listen 80;# 主機名稱server_name bbb.otherdomain.com;# 訪問日志文件存放路徑access_log logs/bbb.otherdomain.com.access.log combined;location /{# 默認首頁文件,順序從左到右,如果找不到index.html文件,則查找index.htm文件作為首頁文件index index.html index.htm;# HTML網頁文件存放的目錄root /data0/htdocs/bbb.otherdomain.com;}}# 第三個虛擬主機server{# 監聽的IP和端口listen 80;# 主機名稱server_name www.domain.com domain.com *.domain.com;# 訪問日志文件存放路徑access_log logs/bbb.domain.com.access.log combined;location /{# 默認首頁文件,順序從左到右,如果找不到index.html文件,則查找index.htm文件作為首頁文件index index.html index.htm;# HTML網頁文件存放的目錄root /data0/htdocs/domain.com;}} }

總結

以上是生活随笔為你收集整理的nginx-配置基于ip或域名的虚拟主机的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。