Nginx负载均衡

Ubuntu 22.04.1 LTS 编译安装 nginx-1.22.1

1.安装依赖
sudo apt install libgd-dev

Nginx负载均衡_第1张图片

2.下载nginx
wget http://nginx.org/download/nginx-1.22.1.tar.gz

Nginx负载均衡_第2张图片

3.解压nginx
tar -zvxf nginx-1.22.1.tar.gz

Nginx负载均衡_第3张图片

4.编译安装
cd nginx-1.22.1

编译并指定安装位置,执行安装之后会创建指定文件夹/www/env/nginx
./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 

成功

Nginx负载均衡_第4张图片

如果出现报错

./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option. PCRE库

PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。另外,pcre-devel是使用PCRE做二次开发时所需要的开发库,包括头文件等,这也是编译Nginx所必须使用的。可以这样安装:

sudo apt update

sudo apt install libpcre3 libpcre3-dev

出现这个错误

./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option OpenSSL库

如果服务器不只是要支持HTTP,还需要在更安全的SSL协议上传输HTTP,那么需要拥有OpenSSL。另外,如果我们想使用MD5、SHA1等散列函数,那么也需要安装它。可以这样安装:

sudo apt-get install openssl libssl-dev 执行编译并安装

5.安装

make && make install

Nginx负载均衡_第5张图片

如果出现错误

sed -e "s|%%PREFIX%%|/www/env/nginx|" \
    -e "s|%%PID_PATH%%|/www/env/nginx/logs/nginx.pid|" \
    -e "s|%%CONF_PATH%%|/www/env/nginx/conf/nginx.conf|" \
    -e "s|%%ERROR_LOG_PATH%%|/www/env/nginx/logs/error.log|" \
    < man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/home/server/nginx-1.22.1”
make -f objs/Makefile install
make[1]: 进入目录“/home/server/nginx-1.22.1”
test -d '/www/env/nginx' || mkdir -p '/www/env/nginx'
mkdir: 无法创建目录 "/www": 权限不够
make[1]: *** [objs/Makefile:1830:install] 错误 1
make[1]: 离开目录“/home/server/nginx-1.22.1”
make: *** [Makefile:13:install] 错误 2
表明在执行 make install 命令时,尝试创建目录 /www 时遇到了权限问题。系统提示权限不足,因此无法创建该目录。

Nginx负载均衡_第6张图片

尝试使用 sudo 命令来运行 make install,以便以超级用户权限执行该命令。这样应该能够创建目录并完成安装。

sudo make install

安装完成可以在/www/env/nginx 看到以下文件

Nginx负载均衡_第7张图片

6.测试启动Nginx

sbin/nginx  

Nginx负载均衡_第8张图片

Nginx反向代理-负载均衡

在129.168.124.141服务器上配置负载均衡,在/etc/nginx/conf.d/目录下新建文件,命名为nginx.conf。

sudo /etc/nginx/conf.d/nginx.conf

填入以下内容

upstream load_banance {
     #负载均衡方法,可选:least_conn,ip_hash等,不填写则为轮询方式;
     # 服务器的访问地址,最好使用服务器的私有IP以获得更好的性能和安全性。
     server 192.168.229.128:80 weight=1;
     server 192.168.229.129:80 weight=2;
}
server {
        # 负载均衡的监听端口
        listen 80 default_server;
        listen [::]:80 default_server;
        # 负载均衡服务器的服务名称,没有时填写 _
        server_name _;

        location / {
                # 代理转发,注意这个load_banance要与 upstream 后的字符串相同
                proxy_pass http://load_banance;
        }
}

Nginx负载均衡_第9张图片

保存后重启nginx服务

sudo systemctl restart nginx

修改nginx的默认页面便于区分

sudo vim /var/www/html/index.nginx-debian.html

新添加这一行

Nginx负载均衡_第10张图片

另一个服务器同样新添加这一行

浏览器中输入192.168.229.128,刷新页面

Nginx负载均衡_第11张图片

Nginx负载均衡_第12张图片

你可能感兴趣的:(nginx,负载均衡,运维)