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 #编译
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;
}
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