1.下载nginx
[root@nginx ~]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@nginx ~]# tar -zxvf nginx-1.16.1.tar.gz
2. 下载nginx_upstream_check_module
[root@nginx ~]#cd /usr/local/src/
[root@nginx src]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@nginx src]# yum -y install unzip
[root@nginx src]# unzip master.zip
3.安装依赖包
[root@nginx ~]# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
4.打补丁,加载模块
[root@nginx ~]# yum -y install patch
[root@nginx ~]# cd nginx-1.16.1
[root@nginx nginx-1.16.1]# patch -p1 < /usr/local/src/nginx_upstream_check_module-master/check_1.16.1+.patch
5. 编译安装
[root@nginx ~]# cd nginx-1.16.1
[root@nginx nginx-1.16.1]# ./configure --prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-pcre \
--add-module=/usr/local/src/nginx_upstream_check_module-master/
[root@nginx nginx-1.16.1]# make -j8 && make install
6.配置nginx的环境变量
[root@nginx nginx-1.16.1]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
[root@nginx nginx-1.16.1]# nginx -v
nginx version: nginx/1.16.1
6.1配置nginx服务
[root@nginx ~]# cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
6.2 加载nginx服务到system
[root@nginx ~]# systemctl daemon-reload
[root@nginx ~]# systemctl start nginx
7.nginx 的配置upstream站点
[root@mysql-130 conf]# cat nginx.conf
user nginx;
worker_processes auto;
error_log logs/error.log;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
#stream的配置tcp文件在这里,用include那个onetcp.conf这个文件
#include /usr/local/nginx/streamconf/*.conf;
http {
log_format main '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log logs/access.log main;
sendfile on; #开启文件的高效传输模式。
tcp_nopush on; #数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞
#长连接超时配置
keepalive_timeout 65;
client_header_timeout 15s;
client_body_timeout 15s;
send_timeout 60s;
client_max_body_size 1024m; #上传文件大小设置
client_body_buffer_size 512k; #客户端请求主体的缓冲区大小
large_client_header_buffers 4 256k;
#设置缓存空间,存储缓存文件
proxy_cache_path /usr/local/nginx/nginx-cache levels=1:2 keys_zone=nginx-cache:200m max_size=50g inactive=600m;
open_file_cache max=65535 inactive=60s;
#文件描述符一直是在缓存中打开的,如果有一个文件在inactive时间内一次没被使用,它将被移除。
open_file_cache_min_uses 1;
#指定多长时间检查一次缓存的有效信息。
open_file_cache_valid 80s;
#代理设置
proxy_connect_timeout 30s; #与后端服务器建立连接的超时时间
proxy_send_timeout 30s;
proxy_read_timeout 60s; #从后端服务器读取响应的超时
proxy_buffer_size 512k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 128m;
gzip on; #开启gzip,减少我们发送的数据量
gzip_min_length 1k; #允许压缩的最小字节数
gzip_buffers 4 16k; #4个单位为16k的内存作为压缩结果流缓存
gzip_http_version 1.1; #设置识别HTTP协议版本,默认是1.1
gzip_comp_level 4; #gzip压缩比,可在1~9中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU
#压缩的类型
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on; #所以根据客户端的HTTP头来判断,是否需要压缩
upstream web-test {
server 192.128.232.129:80 weight=1;
server 192.128.232.131:80 weight=1;
check interval=3000 rise=2 fall=5 timeout=1000 type=http; #后端服务健康检查
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name 192.128.232.130;
location / {
proxy_pass http://web-test;
}
location /status {
check_status;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
8. 192.128.232.129与192.128.232.131两台主机都安装了nginx服务。进行测试
[root@mysql-130 conf]# curl http://192.128.232.130
129 test
[root@mysql-130 conf]# curl http://192.128.232.130
131 server
[root@mysql-130 conf]# curl http://192.128.232.130
129 test
[root@mysql-130 conf]# curl http://192.128.232.130
131 server
9.关闭192.128.232.129这一台nginx服务,是不会转发到宕机的服务,如果转发到宕机服务,会提示502报错
[root@mysql-129 ~]# nginx -s stop
10.测试,请求没转发到129服务器上。
[root@mysql-130 conf]# curl http://192.128.232.130
131 server
[root@mysql-130 conf]# curl http://192.128.232.130
131 server
[root@mysql-130 conf]# curl http://192.128.232.130
131 server
[root@mysql-130 conf]# curl http://192.128.232.130
131 server
11.可以查看nginx服务的状态,在浏览器上
http://192.128.232.130/status