vm镜像:centos6.6
域名服务已配好
web01ip:192.168.146.134 www.abc.cn/bbs.abc.cn
web02ip:192.168.146.135 www.abc.cn/bbs.abc.cn
nginx主配ip:192.168.146.132
nginx副配ip:192.168.146.133
1.对web01和web02主机nginx配置:
1.1 编译安装nginx后
cd /application/nginx/
1.2 mkdir html/{www bbs}
echo "web01 www.abc,cn">html/www/index.html
echo "web01 bbs.abc,cn">html/bbs/index.html
同样在web02主机建立
echo "web02 www.abc,cn">html/www/index.html
[root@web01 nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name www.abc.cn;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/www.access.log main;
}
server {
listen 80;
server_name bbs.abc.cn;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/bbs.access.log main;
}
}
/application/nginx/sbin/nginx -t
......is ok
启动:(不启动会报invalid pid ...nginx.pid之类的)
/application/nginx/sbin/nginx
1.2加入web01/web02主机解析
[root@localhost ~]# cat /etc/hosts
192.168.146.134 www.abc.cn
192.168.146.134 bbs.abc.cn
2.对nginx主配和nginx副配nginx.conf编辑相同如下:
[root@localhost nginx]# cat conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools{ #<==定义web服务器池,包含134,135两个web节点
server 192.168.146.134:80 weight=1;
server 192.168.146.135:80 weight=1;
}
server { #<==定义代理的负载均衡域名虚拟主机
listen 80;
server_name www.abc.cn;
location / {
proxy_pass http://www_server_pools; #<==访问www.abc.cn,请求发送给www_server_pools里的节点
}
}
}
2.1对ip192.168.146.132主配nginx编辑:
echo “192.168.146.132 www.abc.cn” >/etc/hosts
curl www.abc.cn
web01 www.abc,cn
curl www.abc.cn
web02 www.abc,cn