之前有过Docker安装经验,本教程演示宿主机直接安装方法
1.Nginx下载官网
2.因为在Centos虚拟机安装,所以下载Linux版本
- ## 1.解压安装包
- tar zxvf nginx-1.22.0.tar.gz
- ## 2.将解压后的包移动到
- mv nginx-1.22.0 /usr/local/nginx-1.22.0
- ## 3.安装gcc环境:提示输入【y】并回车
- yum install gcc-c++
- ## 4.安装pcre
- yum install -y pcre pcre-devel
- ## 5.安装zlib
- yum install -y zlib zlib-devel
- ## 6.安装openssl
- yum install -y openssl openssl-devel
- ## 7.进入Nginx目录
- cd /usr/local/nginx-1.22.0
- ## 8.构建makefile
- ./configure \
- --prefix=/usr/local/nginx \
- --pid-path=/var/run/nginx/nginx.pid \
- --lock-path=/var/lock/nginx.lock \
- --error-log-path=/var/log/nginx/error.log \
- --http-log-path=/var/log/nginx/access.log \
- --with-http_gzip_static_module \
- --http-client-body-temp-path=/var/temp/nginx/client \
- --http-proxy-temp-path=/var/temp/nginx/proxy \
- --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
- --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
- --http-scgi-temp-path=/var/temp/nginx/scgi \
- --with-http_stub_status_module \
- --with-http_ssl_module \
- --with-file-aio \
- --with-http_realip_module
- ## 9.编译Nginx
- make
- ## 10.创建配置的临时文件目录
- mkdir -p /var/temp/nginx
- ## 11.安装
- make install
- ## 12.启动服务
- /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- ## 13.查看服务进程
- ps aux | grep nginx
- ## 14.停止服务
- /usr/local/nginx/sbin/nginx -s stop
- ## 15.关闭防火墙
- systemctl stop firewalld && systemctl disable firewalld
-
3.打开页面查看(服务可正常访问)
轮循:round-robin 默认策略,请求均匀打到每台服务器上,如果服务器宕机则自动剔除
权重:weight 按权重进行请求分流,每台服务器默认权重为 1
哈希:ip_hash 请求按访问IP的哈希结果分配,这样每个客户端多次访问都会请求同一服务器,可以解决Session问题
最少:last_conn 请求被打倒连接数最少的服务器
参数 | 说明 |
---|---|
weight | 各个节点分流权重 |
max_fail | 最大失败次数 |
fail_time | 失败超时,当请求失败次数超过最大次数,则超时时间内,新的请求不会再分配给该服务器 |
backup | 备用机,其他节点均不可用时,服务会发到备用机上 |
down | 永久停机(请求不会发到此地址) |
max_conns | 最大连接数,默认为 0 即不限制 |
- ## 1.进入编辑状态
- vi /usr/local/nginx/conf/nginx.conf
- ## 2.增加负载均衡配置
- upstream proxymoonhello{
- server 192.168.184.4:8801 ;
- server 192.168.184.5:8801 backup;
- server 192.168.184.6:8801 down;
- }
- ## 3.增加转发配置
- location /hello {
- proxy_pass http://proxymoonhello;
- proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
- proxy_connect_timeout 20s;
- proxy_read_timeout 20s;
- proxy_send_timeout 20s;
- }
- ## 3.重载配置文件
- /usr/local/nginx/sbin/nginx -s reload
-
编写一个Nginx开机自启脚本
- ## 1.创建文件
- vim /usr/lib/systemd/system/nginx.service
- ## 2.写入指定内容
- ## 3.激活脚本
- systemctl enable nginx.service
- ## 4.刷新
- systemctl daemon-reload
- ## 5.启动测试
- systemctl start nginx.service
- ## 6.查看状态
- systemctl status nginx.service
-
注意启动要先关掉手动启动的 Nginx 进程
脚本内容
- [Unit]
- Description=Nginx
- After=network.target remote-fs.target nss-lookup.target
-
- [Service]
- Type=forking
- Environment=JAVA_HOME=/usr/local/java
- User=root
- TimeoutSec=0
- PIDFile=/var/run/nginx/nginx.pid
- ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- ExecReload=/bin/kill -s HUP $MAINPID
- ExecStop=/bin/kill -s QUIT $MAINPID
- PrivateTmp=true
-
- [Install]
- WantedBy=multi-user.target
-
- package com.demo.controller;
-
- import org.springframework.web.bind.annotation.*;
-
- import javax.servlet.http.HttpServletResponse;
-
- /**
- *
- * Description: 你好
- *
- * @Author: zhx & moon hongxu_1234@163.com
- * @Date: 2022-03-30 20:21
- * @version: V1.0.0
- */
- @RestController
- @RequestMapping("/hello")
- public class Hello {
-
- @GetMapping("/{type}")
- public String hello(@PathVariable("type") int type, HttpServletResponse responseBody){
- System.out.println("Accept HTTP .........................");
- if (type > 0){
- return "S";
- }
- responseBody.setStatus(500);
- return "F";
- }
- }
-
-
打包并部署:java -jar zhx_moon_consumer.jar &
服务均可直接响应
Nginx转发调用
配置
效果
配置
效果:比重并不是按序123 45 6去落到三台机器上的
一台失败不可用,一台备用,一台宕机
效果