nginx编译安装+keepalived双主

nginx常用的模块

upstream 
proxy
stub_status

ssl

rewrite 

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

 

在两台主机(192.168.0.6,192.168.0.12)上分别安装nginx+keepalived

1.安装依赖pcre(192.168.0.6)

wget  https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gz  #下载源码

tar xf pcre-8.42.tar.gz   

cd pcre-8.42  

./configure --prefix=/usr/local/pcre       

make && make install

 

2.安装nginx

 wget http://nginx.org/download/nginx-1.14.0.tar.gz

 tar xf nginx-1.14.0.tar.gz

cd nginx-1.14.0/

./configure --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_ssl_module --with-pcre=../pcre-8.42--with-stream #编译

--prefix=           #指向安装目录
--sbin-path         #指向(执行)程序文件(nginx)
--config-path=      #指向配置文件(nginx.conf)
--error-log-path=   #指向错误日志目录
--pid-path=         #指向pid文件(nginx.pid)
--lock-paht=  #指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人占用,或自己误操作)
--user=             #指向程序运行时的非特权用户
--group=            #指定程序运行时的非特权用户组
--builddir=         #指定编译目录
--with-rtsig_module     #启用rtsig模块支持(实时信号)
--with-select_module    #启用 select 模块支持(一种轮询模式,不推荐在高载环境下使用)
禁用:--without-select_module
--with-poll_module       #启用 poll 模块支持(功能与 select 相同,与 select 特性相同,为一种轮询模式,不推荐在高载环境下使用)
--with-file-aio           #启用 file aio 支持(一种 APL 文件传输格式)
--with-ipv6 启用 ipv6 支持
--with-http_ssl_module    #启用 ngx_http_ssl_module 支持(使支持 https 请求,需已安装openssl)
--with-http_realip_module  #启用 ngx_http_realip_module 支持(这个模块允许从请求标头更改客户端的 IP 地址值,默认为关)
--with-http_addition_module  #启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
--with-http_zslt_module      #启用ngx_http_xslt_module支持(过滤转换xml请求)
--with-http_image_filter_module  #启用ngx_http_image_filter_module支持(传输JPEG/GIF/PNG图片的一个过滤器)(默认不启用gd库要用到)
--with-http_geoip_module      #启用ngx_http_geiop_module(该模块创建基于与MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量)
--with-http_sub_module        #启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应的文本)
--with-http_dav_module        #启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL;创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启
--with-http_flv_module     #启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_gzip_static_module      #启用ngx_http_gzip_static_module 支持(在线实时压缩输出数据流)
--with-http_random_index_module     #启用ngx_http_random_index_module 支持(从目录中随机挑选一个目录索引)
--with-http_secure_link_module      #启用 ngx_http_secure_link_module 支持(计算和检查要求所需的安全链接网址)
--with-http_degradation_module     #启用ngx_http_degradation_module 支持(允许在内存不足的情况下返回 204 或 444 码)
--with-http_stub_status_module     #启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
(示例:
location /ngx_status
 {

stub_status on;
 access_log off;
}
Active connections: 36 
server accepts handled requests
9649029 9649029 32008160
Reading: 0 Writing: 1 Waiting: 35
active connections – #活跃的连接数量
server accepts handled requests — #总共处理了 11989 个连接 , 成功创建 11989 次握手, 总共处理了 11991 个请求
reading — #读取客户端的连接数.
writing — #响应数据到客户端的数量
waiting — #开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.)
--without-http_charset_module   #禁用 ngx_http_charset_module 支持(重新编码 web页面,但只能是一个方向--服务器端到客户端,并且只有一个字节的编码可以被重新编码)
--without-http_gzip_module     # 禁 用 ngx_http_gzip_module 支 持 ( 该 模 块 同-withhttp_gzip_static_module 功能一样)
--without-http_ssi_module    # 禁用 ngx_http_ssi_module 支持(该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持 SSI 命令的列表是不完整的)
--without-http_userid_module      #禁用 ngx_http_userid_module 支持(该模块用来处理用来确定客户端后续请求的 cookies)
--without-http_access_module    #禁用 ngx_http_access_module 支持(该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于 ip 地址)
--without-http_auth_basic_module       #禁用 ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于 http 基本认证方法来保护你的站点或其部分内容)
--without-http_autoindex_module  #禁用 disable ngx_http_autoindex_module 支持(该模块用于自动生成目录列表,只在 ngx_http_index_module 模块未找到索引文件时发出请求。)
--without-http_geo_module   #禁用 ngx_http_geo_module 支持(创建一些变量,其值依赖于客户端的 IP 地址)
--without-http_map_module   #禁用 ngx_http_map_module 支持(使用任意的键/值对设置配置变量)
--without-http_split_clients_module   #禁用 ngx_http_split_clients_module 支持(该模块用来基于某些条件划分用户。条件如:ip 地址、报头、cookies 等等)
--without-http_referer_module        #禁用 disable ngx_http_referer_module 支持(该模块用来过滤请求,拒绝报头中 Referer 值不正确的请求)
--without-http_rewrite_module        #禁用 ngx_http_rewrite_module 支持(该模块允许使用正则表达式改变 URI,并且根据变量来转向以及选择配置。如果在 server 级别设置该选项,那么他们将在 location 之前生效。如果在 location 还有更进一步的重写规则,location部分的规则依然会被执行。如果这个 URI 重写是因为 location 部分的规则造成的,那么location 部分会再次被执行作为新的 URI。 这个循环会执行 10 次,然后 Nginx 会返回一个 500 错误。)
--without-http_proxy_module         #禁用 ngx_http_proxy_module 支持(有关代理服务器)
--without-http_fastcgi_module       #禁用 ngx_http_fastcgi_module 支持(该模块允许Nginx 与 FastCGI 进程交互,并通过传递参数来控制 FastCGI 进程工作。 )FastCGI 一个常驻型的公共网关接口。
--without-http_uwsgi_module         #禁用 ngx_http_uwsgi_module 支持(该模块用来医用uwsgi 协议,uWSGI 服务器相关)
--without-http_scgi_module         #禁用 ngx_http_scgi_module 支持(该模块用来启用 SCGI协议支持,SCGI 协议是 CGI 协议的替代。它是一种应用程序与 HTTP 服务接口标准。它有些像 FastCGI 但他的设计 更容易实现。)
--without-http_memcached_module    #禁用 ngx_http_memcached_module 支持(该模块用来提供简单的缓存,以提高系统效率)
-without-http_limit_zone_module    #禁用 ngx_http_limit_zone_module 支持(该模块可以针对条件,进行会话的并发连接数控制)
--without-http_limit_req_module    #禁用 ngx_http_limit_req_module 支持(该模块允许你对于一个地址进行请求数量的限制用一个给定的 session 或一个特定的事件)
--without-http_empty_gif_module    #禁用 ngx_http_empty_gif_module 支持(该模块在内存中常驻了一个 1*1 的透明 GIF 图像,可以被非常快速的调用)
--without-http_browser_module      #禁用 ngx_http_browser_module 支持(该模块用来创建依赖于请求报头的值。如果浏览器为 modern , 则 $modern_browser 等 于modern_browser_value 指令分配的值;如 果浏览器为 old,则$ancient_browser 等于ancient_browser_value 指令分配的值;如果浏览器为 MSIE 中的任意版本,则 $msie 等于 1)
--without-http_upstream_ip_hash_module   #禁用ngx_http_upstream_ip_hash_module 支持(该模块用于简单的负载均衡)
--with-http_perl_module      #启用 ngx_http_perl_module 支持(该模块使 nginx 可以直接使用 perl 或通过 ssi 调用 perl)
--with-perl_modules_path=    #设定 perl 模块路径
--with-perl=                 #设定 perl 库文件路径
--http-log-path=             #设定 access log 路径
--http-client-body-temp-path=    #设定 http 客户端请求临时文件路径
--http-proxy-temp-path=          #设定 http 代理临时文件路径
--http-fastcgi-temp-path=        #设定 http fastcgi 临时文件路径
--http-uwsgi-temp-path=          #设定 http uwsgi 临时文件路径
--http-scgi-temp-path=           #设定 http scgi 临时文件路径
-without-http                    #禁用 http server 功能
--without-http-cache             #禁用 http cache 功能
--with-mail                      #启用 POP3/IMAP4/SMTP 代理模块支持
--with-mail_ssl_module           #启用 ngx_mail_ssl_module 支持
--without-mail_pop3_module       #禁用 pop3 协议(POP3 即邮局协议的第 3 个版本,它是规定个人计算机如何连接到互联网上的邮件服务器进行收发邮件的协议。是因特网电子邮件的第一个离线协议标 准,POP3 协议允许用户从服务器上把邮件存储到本地主机上,同时根据客户端的操作删除或保存在邮件服务器上的邮件。POP3 协议是 TCP/IP 协议族中的一员,主要用于 支持使用客户端远程管理在服务器上的电子邮件)
--without-mail_imap_module       #禁用 imap 协议(一种邮件获取协议。它的主要作用是邮件客户端可以通过这种协议从邮件服务器上获取邮件的信息,下载邮件等。IMAP 协议运行在 TCP/IP 协议之上, 使用的端口是 143。它与 POP3 协议的主要区别是用户可以不用把所有的邮件全部下载,可以通过客户端直接对服务器上的邮件进行操作。)
--without-mail_smtp_module       #禁用 smtp 协议(SMTP 即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP 协议属于TCP/IP 协议族,它帮助每台计算机在发送或中转信件时找到下一个目的地。)
--with-google_perftools_module   #启用 ngx_google_perftools_module 支持(调试用,剖析程序性能瓶颈)
--with-cpp_test_module           #启用 ngx_cpp_test_module 支持
--add-module=                    #启用外部模块支持
--with-cc=                       #指向 C 编译器路径
--with-cpp=                      #指向 C 预处理路径
--with-cc-opt=                   #设置C 编译器参数( PCRE 库,需要指定 – with-cc-opt= ” -I
/usr/local/include”,如果使用 select()函数则需要同时增加文件描述符数量,可以通过–with-cc- opt=”-D FD_SETSIZE=2048”指定。)
--with-ld-opt=                   #设置连接文件参数 。( PCRE 库 , 需 要 指 定 – with-ld-opt= ” -L /usr/local/lib”。)
--with-cpu-opt=                  #指定编译的 CPU,可用的值为: pentium, pentiumpro, pentium3,pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre                  #禁用 pcre 库
--with-pcre                     #启用 pcre 库
--with-pcre=                    #指向 pcre 库文件目录
--with-pcre-opt=                #在编译时为 pcre 库设置附加参数
--with-md5=                     #指向 md5 库文件目录(消息摘要算法第五版,用以提供消息的完整性保护)
--with-md5-opt=                 #在编译时为 md5 库设置附加参数
--with-md5-asm                  #使用 md5 汇编源
--with-sha1=                    #指向 sha1 库目录(数字签名算法,主要用于数字签名)
--with-sha1-opt=                #在编译时为 sha1 库设置附加参数
--with-sha1-asm                 #使用 sha1 汇编源
--with-zlib=                    #指向 zlib 库目录
--with-zlib-opt=                #在编译时为 zlib 设置附加参数
--with-zlib-asm=                #为指定的 CPU 使用 zlib 汇编源进行优化,CPU 类型为 pentium,pentiumpro
--with-libatomic                #为原子内存的更新操作的实现提供一个架构
--with-libatomic=               #指向 libatomic_ops 安装目录
--with-openssl=                 #指向 openssl 安装目录
--with-openssl-opt              #在编译时为 openssl 设置附加参数
--with-debug                    #启用 debug 日志

2.1 通用配置选项

--prefix=<path>       #指定Nginx 的安装路径,所有其他的路径都要依赖于该选项
--sbin-path=<path>    #指定 Nginx 二进制文件的路径。如果没有指定,那么这个路径将依赖于--prefix 选项
 --conf-path=<path>   #指定 Nginx 的配置文件的路径,如果在命令行没有指定配置文件,那么将会通过这里指定路径。
--error-log-path=<path>  #指定错误日志文件路径,Nginx 将会往里面写入错误日志文件,除非有其它的配置。
--pid-path=<path>     #指定的 Nginx master 进程的 PID 文件位置,通常在/var/run 下
--lock-path=<path>    #共享存储器互斥锁文件的路径
--user=<user> worker  #进程运行的用户
--group=<group> worker    #进程运行的组
--with-file-aio.          #为 FreeBSD4.3 +和 linux 2.6.22 +系统启用异步 I/O
--with-debug              #这个选项用于启用调试日志,在生产环境的系统中不推荐使用 

2.2 邮件代理的配置选项:

 --with-mail       #该选项用于启用 Mail 模块,该模块默认没有被激活
 --with-mail_ssl_module     #为了代理任何一种类型的使用SSL/TLS的Mail,激活该模块
 --without-mail_pop3_module     #在启用 Mail 模块后,单独地禁用 pop3 模块
 --without-mail_imap_module     #再启用 mail 模块后,单独地禁用 IMAP 模块
 --without-mail_smtp_module      #在启用 mail 模块后,单独地禁用 smtp 模块
 --without--http                 #该选项将完全禁用 http 模块,如果你只想支持

2.3 指定路径的配置选项

 --without-http_perl_module       #Nginx 配置能够使用扩展使用 Perl 代码,这个选项启用这个模块(此模块会降低性能)
 --without-perl_module_path=<path>     #对于额外嵌入的 Perl 模块,使用该选项指定该 Perl 解析器的路径,也可以通过配置选项来指定 Perl 模块解析器的位置
 --without—perl=<path>                #如果在默认路径中没有找到 Perl,那么指定 Perl 的路径
 --http-log-path=<path>               #Http 访问日志的默认路径
 --http-client-body-temp-path=<path>     #从客户端收到请求后,该选项设置的目录用于作为请求体临时存放的目录。如果 WebDAV 模块启用,那么推荐设置该路径为同一文件系统上的目录作为最终的目的地
 --http-proxy-temp-path=<path>       #在使用代理后,通过该选项设置存放临时文件路径
 --http-fastcgi-temp-path=<path>     #设置 FastCGI 临时文件的目录
 --http-uwsgi-temp-path=<path>       #设置 uWSGI 临时文件的目录
 --http-scgi-temp-path=<path>        #设置 SCGI 临时文件的目录

2.4 各种模块配置选项:

 --with-http_ssl_module      #如果需要对流量进行加密,可以使用该选项,在 URLs中开始部分将会是 https(需要 OpenSSL 库)
 --with-http_realip_module    #如果你的 Nginx 在七层负载均衡器或者是其他设备之后,它们将 http 头中的客户端 IP 地址传递,那么你将会需要启用这个模块。在多个客户处于一个 IP 地址的情况下使用
--with-http_addition_module     #这个模块作为输出过滤器,使你能够在请求经过一个location 前或者后时在该 location 本身添加内容
 --with-http_xslt_module   #该模块用于处理 XML 响应转换,基于一个或者多个 XSLT格式(需要 libxml2 和 libxslt 库)
--with-http_image_filter_module   #该模块被作为图像过滤器使用,在将图形投递到客户之前进行处理(需要 libgd 库)
 --with-http_geoip_module         #使用该模块,能够设置各种变量以便在配置文件中区段使用,基于地理位置查找客户端 IP 地址(需要 MaxMfind GeoIP 库和相应的预编译数据库文件)
--with-http_sub_module         #该模块实现了替代过滤,在响应中用一个字符串替代另一个字符串
 --with-http_dav_module        #启用这个模块将激活使用 WebDAV 的配置指令。注意:这个模块也只在有需要使用的基础上启用,如果配置不正确,它将带来安全问题。
 --with-http_flv_module        #如果需要提供 Flash 流媒体视频文件,那么该模块将会提供伪流媒体
--with-http_mp4_module         #这个模块支持 H.264/AAC 文件伪流媒体
 --with-http_gzip_module       #当被调用的资源没有.gz 结尾格式的文件时,如果想支持发送预压缩版本的静态文件,那么使用该模块
 --with-http_gunzio_module     #对应不支持 gzip 编码的客户,该模块用于为客户解压缩预压缩内容
 --with-http_random_index_module      #如果你想提供从一个目录中随机选择文件的索引文件,那么这个模块需要被激活
--with-http_secure_link_module       #该模块提供了一个机制,它会将一个哈希值链接到一个 URL 中,因此,只有那些使用正确的密码能够计算链接
 --with-http_stub_status_module      #启用这个模块后会收集 Nginx 自身的状态信息。输出的状态信息可以使用 RRDtool 或类似的东西来绘制成图

make && make install

/usr/local/nginx/sbin/nginx #启动

配置文件

cat /usr/local/nginx/conf/nginx.conf   192.168.0.6

#user  nginx;
worker_processes  2;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
server_tokens off; #关闭版本显示
    client_max_body_size 100m;
    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;
    tcp_nopush     on;
 
    keepalive_timeout  65; 
    gzip  on;
    gzip_comp_level 7;
    gzip_min_length 1024;
    gzip_buffers 4 8k;
    gzip_types text/plain application/javascript text/css ;
    output_buffers 1 32k;
    postpone_output 1460;
upstream lp {
#server 192.168.0.6:8080;
server 192.168.0.12:8080;
}
 
    limit_req_zone $binary_remote_addr zone=allips:10m rate=100r/m;


include server/*.conf;

}

[root@4haocentos conf]# cat nginx.conf 192.168.0.12
#user  nginx;
worker_processes  2;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
server_tokens off; #关闭版本显示
    client_max_body_size 100m;
    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;
    tcp_nopush     on;
 
    keepalive_timeout  65; 
    gzip  on;
    gzip_comp_level 7;
    gzip_min_length 1024;
    gzip_buffers 4 8k;
    gzip_types text/plain application/javascript text/css ;
    output_buffers 1 32k;
    postpone_output 1460;
upstream lp{
        server 192.168.0.6:8080;
        server 192.168.0.12:8080;
    }
 
    limit_req_zone $binary_remote_addr zone=allips:10m rate=100r/m;


include server/*.conf;

}

 

[root@4haocentos conf]# cat server/bgy.conf 
server {
    listen       80;
    server_name  test.apicloud.com;
    charset utf8;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
    location / {
        proxy_pass http://lp/;
        proxy_redirect  off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Cookie $http_cookie;
        chunked_transfer_encoding  off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
   }
}

 

安装keepalived

 

yum install -y keepalived

配置文件192.168.0.6

cat /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs {
   router_id LVS_01
}


vrrp_script chk_http_port {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
    weight 2
}


vrrp_instance VI_1 {
    state MASTER            
    interface em1
    virtual_router_id 51          
    priority 200
    advert_int 1
    mcast_src_ip 192.168.0.06
    authentication {
        auth_type PASS
        auth_pass 1111
    }
      track_script {
        chk_http_port
    }
    virtual_ipaddress {
     192.168.0.3
    }
}
vrrp_instance VI_2 {
    state BACKUP
    interface em1
    virtual_router_id 52
    priority 150     
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        192.168.0.4
    }

}

配置文件192.168.0.12

cat /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs {
   router_id LVS_01
}


vrrp_script chk_http_port {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
    weight 2
}


vrrp_instance VI_1 {
    state BACKUP           
    interface em1
    virtual_router_id 51          
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
      track_script {
        chk_http_port
    }
    virtual_ipaddress {
     192.168.0.3
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface em1
    virtual_router_id 52
    priority 200     
    mcast_src_ip 192.168.0.12
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        192.168.0.4
    }
}

查看nginx状态脚本

cat /etc/keepalived/chk_nginx.sh 
#!/bin/bash
status=$(ps -C nginx --no-heading|wc -l)
if [ "${status}" = "0" ]; then
        /usr/local/nginx/sbin/nginx
        status2=$(ps -C nginx --no-heading|wc -l)
        if [ "${status2}" = "0"  ]; then
                /etc/init.d/keepalived stop
        fi
fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值