nginx - ip 또는 도 메 인 이름 기반 가상 호스트 설정

8436 단어 nginx
【0】README: nginx 는 다양한 종류의 가상 호스트 를 설정 할 수 있 습 니 다. 
형식 1) ip 기반 가상 호스트;
형식 2) 도 메 인 이름 기반 가상 호스트;
형식 3) 포트 기반 가상 호스트;
【 1 】 ip 기반 가상 호스트 설정 준비 작업
step 1) 이 서버 의 ip 주 소 를 ifconfig 로 먼저 봅 니 다.   이 서버 는 물리 적 네트워크 카드 장치 eth0 과 로 컬 리 턴 장치 lo 가 있 습 니 다. eth0 의 ip 주 소 는 192.168.186.100 이 고 로 컬 리 턴 lo 의 ip 주 소 는 127.0.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.0
          inet6 addr: fe80::20c:29ff:fe71:d71a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1349 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1155 errors:0 dropped:0 overruns:0 carrier:0
          collisions: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.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b) 

step2)  eth 0 네트워크 카드 장치 에 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

step 3) ifconfig 명령 을 실행 하면 eth 0 네트워크 카드 장치 에 두 개의 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.0
          inet6 addr: fe80::20c:29ff:fe71:d71a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1349 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1155 errors:0 dropped:0 overruns:0 carrier:0
          collisions: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.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt: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.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:19 Base address:0x2000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)

step 4) 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 host
    server
    {
        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 host
    server 
    {
        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;
        }
    }
}

좋은 웹페이지 즐겨찾기