Nginx
主要两个点: 反向代理与负载均衡
正向代理
代理服务器代理了客户端,去和目标服务器进行交互 应用场景: 黄牛买票
优点: 突破IP访问限制,;通过缓存加速访问资源;隐藏客户端真实IP;客户端访问授权
反向代理
代理服务器代理了目标服务器,去和客户端交互,应用场景: 客服热线
优点: 隐藏服务器真实IP;负载均衡;通过缓存加速访问资源;提高安全保障
配置
安装
#下载
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#解压
tar zxvf nginx-1.18.0.tar.gz
#打开目录
cd nginx-1.18.0/
#安装nginx依赖
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl-devel
#配置nginx安装目录
./configure --prefix=/home/luochen/nginx/bin
#编译并安装ls
make && make install
#到只能装目录下启动
#启动 默认端口80
安装目录/sbin/nginx
sbin/nginx -s reload #重启
#设置用户
yum -y install httpd-tools #安装 htpasswd 工具
htpasswd -c /home/luochen/nginx/bin/.passwd-www 用户名 #生成用户登录的认证文件
常用命令
-?,-h : this help #帮助
-v : show version and exit #查看版本信息
-V : show version and configure options then exit #查看版本和配置信息
-t : test configuration and exit #测试配置有没有问题
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /home/luochen/nginx//) #设置路径
-c filename : set configuration file (default: conf/nginx.conf) #按照配置文件启动
-g directives : set global directives out of configuration file
负载均衡
轮询与权重
/home/luochen/nginx/bin/conf/nginx.conf
#编辑配置文件
vim nginx.conf
#配置要代理的服务器集群
# weight: 权重,默认为1
upstream tomcat{
server 192.168.163.129:8080 weight=1;
server 192.168.163.130:8080 weight=1;
server 192.168.163.131:8080 weight=1;
}
#监听服务端口
server {
listen 80;
server_name localhost;
#开启用户认证
auth_basic "User Authentication";
#配置认证文件
auth_basic_user_file /home/luochen/nginx/bin/.passwd-www;
#charset koi8-r;
#access_log logs/host.access.log main;
#tomcat: 引用代理的服务器
location / {
proxy_pass http://tomcat;
#root html;
#index index.html index.htm;
}