Nginx 虚拟主机
========================================================
基于IP
基于主机名
基于端口
基于主机名 name-based
基于IP ip-based
一、规划
网站 IP 网站主目录
www.jeson.com 192.168.10.33 /nginx/jeson
www.126.com 192.168.10.33 /nginx/126
www.baidu.com 192.168.10.133 /nginx/baidu
由于使用了两个IP,在接口上绑定IP
二、DNS解析
www.jeson.com jeson.com ==> 192.168.10.33
www.126.com 126.com ==> 192.168.10.33
www.baidu.com baidu.com ==> 192.168.10.133
ping …
三、Nginx虚拟主机
1. 准备工作
[root@station230 ~]# mkdir -p /nginx/{126,jeson,baidu}
[root@station230 ~]# echo “nginx test: www.126.com” > /nginx/126/index.html
[root@station230 ~]# echo “nginx test: www.jeson.com” > /nginx/jeson/index.html
[root@station230 ~]# echo “nginx test: www.baidu.com” > /nginx/baidu/index.html
2.配置Nginx实现虚拟主机
[root@jeson ~]# vim /usr/local/nginx-1.2.0/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.10.33:80;
server_name www.jeson.com jeson.com; 基于主机名
location / {
root /nginx/jeson;
index index.html index.htm;
}
}
server {
listen 192.168.10.33:80;
server_name www.126.com 126.com; 基于主机名
location / {
root /nginx/126;
index index.html index.htm;
}
}
server {
listen 192.168.10.133:80;
server_name www.baidu.com baidu.com; 基于IP
location / {
root /nginx/baidu;
index index.html index.htm;
}
}
}
3. 启动
[root@jeson ~]# /usr/local/nginx-1.2.0/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.2.0/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.2.0/conf/nginx.conf test is successful
[root@jeson ~]# pgrep nginx
10627
10866
10867
[root@jeson ~]# kill -HUP 10627
4. 测试
[root@jeson ~]# links -dump 126.com
nginx test: www.126.com
[root@jeson ~]#
[root@jeson ~]# links -dump jeson.com
nginx test: www.jeson.com
[root@jeson ~]#
[root@jeson ~]# links -dump baidu.com
nginx test: www.baidu.com