nginx+keepalived配置

一、环境

系统:CentOS 6.4x64位最小化安装

nginx-m:192.168.3.23

nginx-s:192.168.3.24

vip:    192.168.3.25

二、安装nginx

在nginx-m和nginx-s安装nginx,这里使用脚本安装,脚本内容如下

#!/bin/bash
 
cur_dir=$(pwd)
NGINXVERSION=‘nginx-1.6.0‘
export LANG=zh_CN.UTF-8
 
#Source function library.
. /etc/init.d/functions
 
create_nginx_conf(){
cat >>/usr/local/nginx/conf/nginx.conf<<EOF
user  www www;
  
worker_processes auto;
  
error_log  /usr/local/nginx/logs/nginx_error.log  crit;
  
pid        /usr/local/nginx/logs/nginx.pid;
  
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
  
events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
  
http
    {
        include       mime.types;
        default_type  application/octet-stream;
  
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
  
        sendfile on;
        tcp_nopush     on;
  
        keepalive_timeout 60;
  
        tcp_nodelay on;
  
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
  
        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied        expired no-cache no-store private auth;
        gzip_disable        "MSIE [1-6]\.";
  
        #limit_conn_zone \$binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
  
        server_tokens off;
        #log format
        log_format  access  ‘\$remote_addr - \$remote_user [\$time_local] "\$request" ‘
             ‘\$status \$body_bytes_sent "\$http_referer" ‘
             ‘"\$http_user_agent" \$http_x_forwarded_for‘;
  
server
    {
        listen 80 default;
        #listen [::]:80 default ipv6only=on;
        server_name www.myweb.com;
        index index.html index.htm index.php;
        root  /var/www/default;
  
        #error_page   404   /404.html;
        location ~ [^/]\.php(/|$)
            {
                # comment try_files \$uri =404; to enable pathinfo
                try_files \$uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
                #include pathinfo.conf;
            }
  
        location /nginx_status {
            stub_status on;
            access_log   off;
        }
  
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)\$
            {
                expires      30d;
            }
  
        location ~ .*\.(js|css)?\$
            {
                expires      12h;
            }
  
        access_log  /var/www/wwwlogs/access.log  access;
    }
include vhost/*.conf;
}
EOF
}
 
create_nginx_init(){
cat >>/etc/init.d/nginx<<EOF
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run ‘update-rc.d -f nginx defaults‘, or use the appropriate command on your
# distro. For CentOS/Redhat run: ‘chkconfig --add nginx‘
  
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/\$NAME
CONFIGFILE=/usr/local/nginx/conf/\$NAME.conf
PIDFILE=/usr/local/nginx/logs/\$NAME.pid
SCRIPTNAME=/etc/init.d/\$NAME
  
case "\$1" in
    start)
        echo -n "Starting \$NAME... "
  
        if netstat -tnpl | grep -q nginx;then
            echo "\$NAME (pid \`pidof \$NAME\`) already running."
            exit 1
        fi
  
        \$NGINX_BIN -c \$CONFIGFILE
  
        if [ "\$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
  
    stop)
        echo -n "Stoping \$NAME... "
  
        if ! netstat -tnpl | grep -q nginx; then
            echo "\$NAME is not running."
            exit 1
        fi
  
        \$NGINX_BIN -s stop
  
        if [ "\$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;
  
    status)
        if netstat -tnpl | grep -q nginx; then
            PID=\`pidof nginx\`
            echo "\$NAME (pid \$PID) is running..."
        else
            echo "\$NAME is stopped"
            exit 0
        fi
    ;;
  
    force-quit)
        echo -n "Terminating \$NAME... "
  
        if ! netstat -tnpl | grep -q nginx; then
            echo "\$NAME is not running."
            exit 1
        fi
  
        kill \`pidof \$NAME\`
  
        if [ "\$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;
  
    restart)
        \$SCRIPTNAME stop
        sleep 1
        \$SCRIPTNAME start
    ;;
  
    reload)
  
        echo -n "Reload service \$NAME... "
  
        if netstat -tnpl | grep -q nginx; then
            \$NGINX_BIN -s reload
            echo " done"
        else
            echo "\$NAME is not running, can‘t reload."
            exit 1
        fi
    ;;
  
    configtest)
  
        echo -n "Test \$NAME configure files... "
  
        \$NGINX_BIN -t
    ;;
  
    *)
        echo "Usage: \$SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}"
        exit 1
    ;;
  
esac
EOF
}
 
#install_nginx
install_nginx(){
        cd $cur_dir
        yum install make gcc gcc-c++ openssl-devel -y
        #add user www for nginx
        id www &>/dev/null
        if [ $? -ne 0 ];then
                groupadd www
                useradd -s /sbin/nologin -g www www
        fi
        wget http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.tar.gz/download
        if [ $? -ne 0 ];then
                echo "download pcre package is fail"
                exit $?
        fi
        tar xf  pcre-8.30.tar.gz
        cd pcre-8.30
        ./configure
        make && make install
        if [ $? -eq 0 ];then
                echo "install pcre is successful!!!"
        else
                echo "install pcre is fail!!!"
                exit $?
        fi
        echo "/usr/local/lib/" >>/etc/ld.so.conf
        ldconfig
        #download nginx package
        cd $cur_dir
        wget http://mirrors.sohu.com/nginx/$NGINXVERSION.tar.gz
        if [ $? -ne 0 ];then
                echo "download nginx is fail!!!"
                exit $?
        fi
        tar xf $NGINXVERSION.tar.gz
        cd $NGINXVERSION
         ./configure --user=www --group=www --prefix=/usr/local/$NGINXVERSION --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 
         make && make install
        if [ $? -ne 0 ];then
                echo "install nginx fail!!!"
                exit $?
        fi 
        #links
        ln -s /usr/local/$NGINXVERSION /usr/local/nginx
        ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
        mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
        #create file nginx.conf
        create_nginx_conf 
        mkdir -p /var/www/default
        chmod +w /var/www/default
        mkdir -p /var/www/wwwlogs
        chmod 777 /var/www/wwwlogs 
        chown -R www:www /var/www/default
        cp /usr/local/nginx/html/index.html /var/www/default/index.html 
        #create start scripts for nginx
        create_nginx_init
        chmod +x /etc/init.d/nginx
        chkconfig --add nginx
        chkconfig nginx on
        /etc/init.d/nginx start
        if [ $? -eq 0 ];then
                action "start nginx" /bin/true
                echo "+---------------------------------+"
                echo "+------nginx  install done--------+"
                echo "+---------------------------------+"
        fi
}
 
install_nginx

在iptables中对80端口进行放行

[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[[email protected] ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[[email protected] ~]# echo "nginx-m 23" >/var/www/default/index.html 
[[email protected] ~]# curl http://192.168.3.23
nginx-m 23

nginx-s的配置相同,只有主页内容不一样

[[email protected] ~]# echo "nginx-s 24" >/var/www/default/index.html 
[[email protected] ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[[email protected] ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[[email protected] ~]# curl http://192.168.3.24
nginx-s 24
时间: 2024-08-03 11:25:11

nginx+keepalived配置的相关文章

CentOS Linux 负载均衡高可用WEB集群之Nginx+Keepalived配置

Nginx+Keepalived实现负载均衡高可用的WEB服务集群,nginx作为负载均衡器,keepalived作为高可用,当其中的一台负载均衡器(nginx)发生故障时可以迅速切换到备用的负载均衡器(nginx),保持业务的连续性. 1.服务器的环境配置及IP分配 操作系统:CentOS release 6.7 (Final) nginx版本:nginx/1.8.0 keepalived版本:Keepalived v1.2.13 Nginx + keepalived服务器的IP分配表 服务器

nginx keepalived 配置

前言: 对于大型web项目来说,必不可少的就是高可用. 那随着公司业务的发展,虽然用nginx做负载均衡仍可以支撑日均百万PV的访问,且运行两年之久未出现单点故障,但仍要考虑避免单点故障导致的业务中断. keepalived 简介: keepalived 项目主要的目标是为linux系统和linux基础设施提供负责均衡和高可用. 负载均衡架构依赖于IPVS内核模块提供四层负载均衡,keepalived实现了动态检查和基于负载池的管理服务.另一方面,高可用性是通过VRRP协议实现.简单的框架可以单

ubuntu 14.04.3 LTS 版本 通过 nginx + keepalived 配置 高可用 负载均衡集群演示

系统版本:ubuntu 14.04.3 LTS 服务器准备: lb01-> ifconfig 显示结果: 192.168.91.136 作用:安装keepalived 及 nginx lb02-> ifconfig 显示结果: 192.168.91.135 作用:安装keepalived 及 nginx web01-> ifconfig 显示结果: 192.168.91.134 作用:安装nginx 负责展示 index.html页面 web02-> ifconfig 显示结果:

Nginx+Keepalived 主备高可用 安装与配置

环境说明:操作系统:CentOS6.7 x86_64Nginx版本:nginx-1.9.7Keepalived版本:keepalived-1.2.24 主nginx + Keepalived :10.219.24.26备nginx + Keepalived :10.219.24.23虚拟IP:10.219.24.100后端tomcat_1:10.219.24.21:8080后端tomcat_2:10.219.24.21:8081 架构与原理: 前端双 Nginx + Keepalived ,Ng

Nginx+Keepalived 实现反代 负载均衡 高可用(HA)配置

Nginx+Keepalived实现反代负载均衡高可用(HA)配置 Nginx+Keepalived实现反代负载均衡高可用配置 OS IP 子网掩码 路由网关 Centos6.6 nginx Keepalived Eth0:192.168.26.210 255.255.252.0 192.168.25.3 VIP:192.168.27.210 Centos6.6 Nginx Keepalived Eth0:192.168.26.211 255.255.252.0 192.168.25.3 VIP

Keepalived && Nginx 高可用性配置

keepalived是一个用于做双机热备(HA)的软件,常和haproxy联合起来做热备+负载均衡,达到高可用. 运行原理 keepalived通过选举(看服务器设置的权重)挑选出一台热备服务器做MASTER机器,MASTER机器会被分配到一个指定的虚拟ip,外部程序可通过该ip访问这台服务器,如果这台服务器出现故障(断网,重启,或者本机器上的keepalived crash等),keepalived会从其他的备份机器上重选(还是看服务器设置的权重)一台机器做MASTER并分配同样的虚拟IP,充

Nginx+keepalived(部分配置)

Nginx-S上面也要配置跟Nginx-M上面一样的Nginx代理服务配置 Nginx-M和Nginx-S的keepalived服务配置: 配置文件模板: ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1

centos6中三台物理机配置nginx+keepalived+lvs

以下只是简单的安装配置,并没有测试这套负载,各种参数大家可以自己测试 vip:10.0.50.170 lvs server:10.0.50.183 real server:10.0.50.184/185 183/184/185同步时间,并且安装nginx # ntpdate time.nist.gov # yum install nginx # /etc/init.d/nginx start 在184/185上编写测试页面/usr/share/nginx/html/index.html 183上

nginx+keepalived+tomcat配置高可用web集群

基本架构: 角色 ip 安装软件 作用 主机名 nginx主 192.168.247.129 nginx+keepalived 反向代理 nginxmaster.com nginx备 192.168.247.130 nginx+keepalived 反向代理 nginxsalve.com tomcat1 192.168.247.128 tomcat web服务器 tomcat1.com tomcat2 192.168.247.131 tomcat web服务器 tomcat2.com nfs主