05_C74部署_Nginx 负载均衡

nginx01:192.168.189.79
nginx02:192.168.189.80

一、安装Nginx 1.15.9 (nginx01 02 都要操作)

1.1、安装依赖包:
yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel -y
useradd -s /sbin/nologin nginx

1.2、下载Nginx 解压、安装
cd /opt
wget http://nginx.org/download/nginx-1.15.9.tar.gz

tar zxf nginx-1.15.9.tar.gz
cd nginx-1.15.9/
./configure --with-http_ssl_module --with-http_stub_status_module --prefix=/usr/local/nginx/
make
make install
1.3、配置nginx服务
cd /usr/lib/systemd/system
touch nginx.service
chmod 754 nginx.service
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.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 stop
PrivateTmp=True
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload

1.4、修改Nginx 配置文件,整合PHP
cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.$(date +%F%H%M%S)
cat >/usr/local/nginx/conf/nginx.conf << EOF
worker_processes 4;
worker_rlimit_nofile 10000; #更改worker进程的最大打开文件数限制.
events {
worker_connections 40960;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nodelay on;
tcp_nopush on;
server_tokens off; #隐藏版本号。
keepalive_timeout 60;
gzip on;
gzip_disable "msie6"; #设置成IE6或者更低版本禁用gzip功能。
gzip_proxied any;
gzip_min_length 1000; #设置对数据启用压缩的最少字节数。
gzip_comp_level 6;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
EOF
1.5、启动
systemctl enable nginx
systemctl start nginx

++++++++++++++++++++++++++++++++++++++++

二、配置负载均衡
vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
worker_rlimit_nofile 10000; #更改worker进程的最大打开文件数限制.
events {
worker_connections 40960;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nodelay on;
tcp_nopush on;
server_tokens off; #隐藏版本号。
keepalive_timeout 60;
gzip on;
gzip_disable "msie6"; #设置成IE6或者更低版本禁用gzip功能。
gzip_proxied any;
gzip_min_length 1000; #设置对数据启用压缩的最少字节数。

gzip_comp_level 6;
upstream web {
server 192.168.189.73:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.189.74:80 weight=1 max_fails=2 fail_timeout=30s;
}

server {
listen 80;
server_name localhost;

location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://web;
root html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

保存退出,重启nginx .
systemctl restart nginx

三、测试