1.Nginx基本概念
Nginx是什么?做什么事情?
正向代理:代理客户端,如fan墙
反向代理:代理服务器,隐藏真实服务器IP
负载均衡:将请求分发到不同服务器
动静分离:把动态页面和静态页面由不同的服务器来解析
2.Nginx安装、常用命令、配置文件
一:安装Nginx相关依赖
安装nginx相关依赖
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
二:安装Nginx
安装nginx
tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure
make && make install
cd /usr/local/nginx/sbin 启动nginx: ./nginx
浏览器访问:localhost (nginx.conf默认配置nginx监听80端口,代理访问的是nginx首页)
卸载nginx:
./nginx -s stop
find / -name nginx
rm -rf /usr/local/nginx
三:Nginx常用命令
nginx命令:/usr/local/nginx/sbin/nginx
1.查看nginx版本 ./nginx -v
2.启动nginx ./nginx
3.关闭nginx ./nginx -s stop
4.重新加载nginx配置 ./nginx -s reload
5.查询nginx进程 ps -ef | grep nginx
3.Nginx配置实例
一:反向代理
配置文件:/usr/local/nginx/conf/nginx.conf
准备:
192.168.196.128 是nginx服务器地址
准备tomcat,并新建测试页面/edu/index.html 放到webapps下
index.html内容:
<h1> hello nginx <h1>
效果:根据不同的请求转发到不同的服务器上
http://192.168.196.128:8888/edu/index.html
http://192.168.196.128:8888/4399
vi /usr/local/nginx/conf/nginx.conf 添加:
server {
listen 8888;
server_name localhost;
location ~ /edu {
proxy_pass http://127.0.0.1:8080;
}
location = /4399 {
proxy_pass http://www.4399.com;
}
}
直接关闭防火墙(适合学习nginx时使用)
systemctl stop firewalld.service
systemctl status firewalld.service
(正式环境一般不会关闭防火墙,设置对外开放端口)
启动防火墙: systemctl start firewalld.service
查看开放的接口: firewall-cmd --list-all
设置开放端口:
firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --add-port=8888/tcp --permanent
重启防火墙: systemctl restart firewalld.service
Google Chrome 默认非安全端口列表:避免使用以下端口
***
4045, // lockd
6000, // X11
6665, // Alternate IRC [Apple addition]
6666, // Alternate IRC [Apple addition]
***等等
二:负载均衡
使用upstream实现负载均衡,将访问的请求被分到不同服务器上:
一:
//在http节点下,添加upstream节点
upstream blance{
server 192.168.196.128:8080;
server 192.168.196.128:8070;
}
// proxy_pass 使用定义的负载均衡
server {
listen 80;
server_name localhost;
location / {
root html;
proxy_pass http://blance;
index index.html index.htm;
}
}
二:几种负载策略:
1.轮询(默认)可靠性低和负载分配不均衡,默认weight=1
修改weight(权重)权重越高被分配的客户端越多
upstream blance{
server 192.168.196.128:8080 weight=5;
server 192.168.196.128:8070 weight=10;
}
2. ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
upstream blance{
ip_hash;
server 192.168.196.128:8080;
server 192.168.196.128:8070;
}
3.url_hash(三方) url的hash结果来分配请求,使每个url定向到同一个后端服务器
4.fair(三方) 此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配
upstream blance{
fair;
server 192.168.196.128:8080;
server 192.168.196.128:8070;
}
Nginx本身不提供fair,需github下载并加入到nginx。
https://github.com/gnosek/nginx-upstream-fair
You'll need to re-compile Nginx from source to include this module.
./configure --with-http_ssl_module --add-module=/opt/nginx-upstream-fair
make
&&make install
nginx 安装fair模块error:
/opt/nginx-upstream-fair-master/ngx_http_upstream_fair_module.c:553:51: error: ‘ngx_http_upstream_srv_conf_t’ {aka ‘struct ngx_http_upstream_srv_conf_s’} has no member named ‘default_port’
sed -i 's/default_port/no_port/g' /opt/nginx-upstream-fair/ngx_http_upstream_fair_module.c
三:动静分离
目的:将静态请求和动态请求分开
1.准备
mkdir -p /data/www
mkdir -p /data/image
mv index.html /data/www
mv a.jpg b.jpg /data/image
2.mginx.conf配置
location /www {
root /data/;
index index.html index.htm;
}
location /image {
root /data/;
autoindex on;
}
3.测试
http://192.168.196.128/image/
http://192.168.196.128/www/
4.区别alias(别名) 和 root
location /img/ {
alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件
四:Nginx配置高可用集群
1.主/备nginx两台服务器,并在主/备上安装keepalived
192.168.196.128(主)
192.168.196.129(从)
yum install keepalived
rpm -q -a keepalived #查看是否已经安装上
默认安装路径: /etc/keepalived
2.主从配置
(1)修改/etc/keepalived/keepalived.conf
#调用脚本
vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh" # 心跳执行的脚本,检测nginx是否启动
interval 2 # (检测脚本执行的间隔)2s
weight 2 #权重
}
#vrrp 实例定义
vrrp_instance VI_1 {
state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备
interface ens33 // vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看网卡
virtual_router_id 51 # (虚拟路由编号)主、备机的virtual_router_id必须相同
priority 100 # 主优先级,数值越大,获取处理请求的优先级越高,MASTER值较大,BACKUP值较小
advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
#授权访问
authentication { # 设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
auth type PASS
auth pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.196.50 # 定义虚拟ip
}
}
(2)在路径/usr/local/src/ 下新建检测脚本 nginx_check.sh
#! /bin/bash
A=`ps -C nginx -no-header | wc - 1`
if [ $A -eq 0];then #如果nginx没有启动就启动nginx
/usr/local/nginx/sbin/nginx #重启nginx
sleep 2
if [`ps -C nginx --no-header| wc -1` -eq 0 ];then #nginx重启失败,则停掉keepalived服务
killall keepalived
fi
fi
(3)启动两台机器上的nginx和keepalived
systemctl start keepalived.service
3.最终测试
(1) 在浏览器地址栏输入虚拟ip地址http://192.168.196.50/,显示主服务器提供的服务。
(2) 把主服务器(192.168.196.128) nginx停掉, 再访问192.168.196.50
还是显示主服务器提供的服务,这是因为keepalived里面配置的脚本,每2秒执行一次,发现nginx挂掉,会重启nginx
systemctl stop nginx
(3)把主服务器(192.168.196.128) nginx和keealived都停掉,再访问192.168.196.50
发现页面显示192.168.196.129提供的服务,这个时候keepalived就自动故障转移了
systemctl stop nginx
systemctl stop keepalived.service