1、所需安装包:nginx
2、安装方式:
./configure --prefix=安装路径
make&&make install
3、配置/usr/local/nginx/conf/nginx.conf
ser wenguang281 wenguang281;#工作进程使用的用户
worker_processes 1;#工作进程数 根据硬件调整,通常等于CPU数量或者2倍于CPU。
error_log logs/error.log;#错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;#进程号文件
events {
use epoll;#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
worker_connections 1024;#单个工作进程可连接的最大数
}
http {
include mime.types;#文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型,默认为text/plain
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';#自定义日志格式
access_log logs/access.log main;
sendfile on;#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
#gzip on; #开启gzip压缩输出
server {
keepalive_requests 120; #单连接请求上限次数
listen 80; #监控端口
server_name server-test.com.cn; #服务名称
#charset koi8-r;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
说明:/usr/local/nginx/sbin/nginx -t可验证配置文件的正确性
4、nginx的启停:
nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出
nginxnginx -t #测试配置是否有语法错误
nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
-?,-h : 打开帮助信息
-v : 显示版本信息并退出
-V : 显示版本和配置选项信息,然后退出
-t : 检测配置文件是否有语法错误,然后退出启动 平滑重启时需要加此参数
-q : 在检测配置文件期间屏蔽非错误信息
-s signal : 给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)
-p prefix : 设置前缀路径(默认是:/usr/local/Cellar/nginx/1.2.6/)
-c filename : 设置配置文件(默认是:/usr/local/etc/nginx/nginx.conf)
-g directives : 设置配置文件外的全局指令
nginx配置完后下面进行与lvs的集成配置,lvs+keepalived的配置见http://blog.csdn.net/wngua/article/details/54378448
1、nginx与lvs的dr模式集成时需要在nginx的lo网口配置与lvs共同的vip(10.20.13.101)
ifconfig lo:0 10.20.13.101 netmask 255.255.255.255 broadcast 10.20.13.101 up
必须要配置在lo网口的原因:1、在real server返回信息给客户端的时候必须保持源ip是vip 2、在局域网能要让real server知道自己具有vip,以及不能影响与局域网其他主机通信,配置在eth网口会影响网络。
2、关闭arp报文广播和应答,避免real server与前端lvs抢vip的应答。
修改 /etc/sysctl.conf文件,添加下列内容
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce =2
执行sysctl -p时配置生效。
3、查看lvs信息如下
[root@property-service-01 ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.20.13.101:http rr persistent 50
-> server-test.com.cn:http Route 1 0 0
[root@property-service-01 ~]#
4、验证:
访问http://10.20.13.101/能正常打开,停掉lvs后无法访问。停掉master后自动切到backup。lvs上activeConn或InActConn有数字变化。
[root@property-service-01 ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.20.13.101:http rr persistent 50
-> server-test.com.cn:http Route 1 1 0
[root@property-service-01 ~]#