1.安装部署nginx
1、在第三方下载nginx-1.14.0.tar.gz
# tar zxf nginx-1.14.0.tar.gz
# ls
# cd nginx-1.14.0
# ls
# cd src/
# ls
# cd core/
# vim nginx.h
第14行的nginx版本删掉,防黑客攻击
# cd ..
# cd auto/
# cd cc/
# vim gcc
注释CFLAGS="$CFLAGS -g"
#yum install gcc -y
# yum install pcre-devel -y
# yum install openssl-devel -y
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
# make
# make install
# ln -s /usr/local/nginx/sbin/nginx /sbin/
# nginx -t
# nginx
在网页访问172.25.25.4、
2.负载均衡
1)负载均衡配置
# 主配置文件:/usr/local/nginx/conf/nginx.conf
# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
# nginx 多核绑定
worker_processes 2;
worker_cpu_affinity 01 10;
events {
worker_connections 65535;
}
# 后端服务器
http {
upstream westos{
server 172.25.25.1:80;
server 172.25.25.3:80;
}
server {
listen 80;
server_name www.redhat.org;
location / {
proxy_pass http://westos;
}
}
# useradd -M -d /usr/local/nginx/ nginx #创建用户
# nginx -s reload #重新加载
# vim /etc/security/limits.conf
nginx - nofile 65536
开启两个后端服务器的httpd
真机测试:
# curl www.redhat.org
(2)hash算法:
# vim /usr/local/nginx/conf/nginx.conf
http {
upstream westos{
ip_hash;
server 172.25.25.1:80;
server 172.25.25.3:80;
}
3)weight权重,默认为1
# vim /usr/local/nginx/conf/nginx.conf
http {
upstream westos{
server 172.25.25.1:80 weight=2;
server 172.25.25.3:80;
}
3.增加外部扩展模块
需要用到nginx-1.10.1.tar.gz版本
# 关闭服务
# nginx -s stop
# 在第三方下载nginx-sticky-module-ng.tar.gz nginx-1.10.1.tar.gz,解压
# tar zxf nginx-sticky-module-ng.tar.gz
# tar zxf nginx-1.10.1.tar.gz
# cd nginx-1.10.1
#ls
# 源码编译三部曲
# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/mnt/nginx-sticky-module-ng
# make
# make install
# cd /opt/nginx/conf/
# cp /usr/local/nginx/conf/nginx.conf .
# 更改主配置文件
# vim nginx.conf
http {
upstream westos{
sticky;
server 172.25.25.1:80;
server 172.25.25.3:80;
}
# /opt/nginx/sbin/nginx -t
# 启动服务
# /opt/nginx/sbin/nginx