ubuntuNginx反向代理和负载均衡

配置两台服务器确保nginx启动并且可以访问

配置nginx代理服务器

可以选择直接安装或者编译安装nginx

root@ubuntu:~# apt install libgd-dev #安装依赖

root@ubuntu:~# apt update

root@ubuntu:~# apt install libpcre3 libpcre3-dev

root@ubuntu:~# apt-get install openssl libssl-dev

root@ubuntu:~# apt intsall make

root@ubuntu:~# wget http://nginx.org/download/nginx-1.22.1.tar.gz #安装压缩包

root@ubuntu:~# tar -zvxf nginx-1.22.1.tar.gz #解压

root@ubuntu:~# cd nginx-1.22.1 #进入nginx目录

bash 复制代码
./configure --prefix=/www/env/nginx \   编译并指定自己的目录
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module \

预编译完成后

使用make编译

bash 复制代码
nginx常用命令 常用命令 
nginx -s reload 修改配置后重新加载生效 
nginx -s stop 快速停止nginx 
nginx -s start 启动nginx 
nginx -s quit 完整有序的停止nginx 
nginx -v 查看nginx的版本 
nginx -V 查看版本和nginx的配置选项 
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确 
nginx -s reopen 重新打开日志文件

进入配置文件进行配置编写

bash 复制代码
 upstream nginx_boot{
                              #weight权重:权重值越大被分配到的次数就会增多
        server 192.168.182.131 weight=100 max_fails=2 fail_timeout=30s;
        server 192.168.182.134 weight=100 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        #    root   html;
       #     index  index.html index.htm;
                 root   html;
        # 配置一下index的地址,最后加上index.ftl。
                 index  index.html index.htm index.jsp index.ftl;
                 proxy_set_header Host $host;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 请求交给名为nginx_boot的upstream上
                 proxy_pass http://nginx_boot;

        }

测试


ip_hash 固定转发 每个访问客户端会固定访问一个后端服务器

bash 复制代码
upstream nginx_boot { 
		ip_hash; 
		server 192.168.182.131;
		server 192.168.182.134; 
}

最少连接 web请求会被转发到连接数最少的服务器上

bash 复制代码
upstream nginx_boot { 
		least_conn; 
		server 192.168.182.131;
		server 192.168.182.134; 
}
相关推荐
liu****37 分钟前
2.深入浅出理解虚拟化与容器化(含Docker实操全解析)
运维·c++·docker·容器·虚拟化技术
池央1 小时前
CANN 诊断工具链深度解析:oam-tools 的自动化故障信息收集、软硬件状态快照与 AI Core 错误溯源机制
运维·人工智能·自动化
云边有个稻草人1 小时前
打工人摸鱼新姿势!轻量斗地主服务器,内网穿透让同事远程联机不翻车
运维·服务器·cpolar
乾元1 小时前
终端安全(EDR):用深度学习识别未知勒索软件
运维·人工智能·网络协议·安全·网络安全·自动化·安全架构
wbs_scy2 小时前
Linux 进阶指令实操指南:文件查看、时间管理、搜索压缩全场景覆盖(附高频案例)
linux·运维·服务器
安科瑞刘鸿鹏172 小时前
高速路灯故障难定位?用 ASL600 实现精确单灯监测与维护预警
运维·网络·物联网·安全
馨谙2 小时前
Ansible 事实(Facts)全面指南:自动化运维中的主机信息管理
运维·ansible
Lethehong2 小时前
实测可用|一文搞定OpenClaw部署,免费kimi-k2.5+飞书远程,新手也能秒上手
linux·运维·服务器·玩转openclaw·云端创意实践
馨谙2 小时前
Ansible处理程序完全指南:实现智能的任务触发机制
运维·ansible
承渊政道2 小时前
Linux系统学习【Linux基础开发工具】
linux·运维·笔记·学习·centos·编辑器