ubuntu编译安装nginx反向代理-负载均衡(建议收藏)

 

目录

一、什么是反向代理

二、 反向代理的作用

三、反向代理有以下优点

四、配置文件

none(轮询)

weight 

ip_hash(访问ip)

fair(第三方)

url_hash(第三方)

least_conn 


一、什么是反向代理

反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。同时,用户不需要知道目标服务器的地址,也无须在用户端作任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率。

二、 反向代理的作用


1、解决网站服务器对外可见的问题、私密

性、安全性

2、路由功能:根据用户请求的URI调度到对应功能的节点处理请求

3、负载均衡:将用户的请求,通过调度算法挑选一台合适的节点处理请求

4、动静分离:根据用户请求的URI进行区分,将动态资源调度到应用服务器处理,将静态资源调度到静态资源服务器处理

5、数据缓存:加速网站的访问速度,减轻web服务器的负担。如果用户请求的内容在缓存中,可以直接在代理服务器中获取,加速用户的访问速度。

三、反向代理有以下优点


1.可以起到保护网站安全的作用,因为任何来自Internet的请求都必须先经过代理服务器。
2.通过缓存静态资源,加速Web请求。
3.实现负载均衡。顺便说下,目前市面上,主流的负载均衡方案,硬件设备有F5,软件方案有四层负载均衡的LVS,七层负载均衡的Nginx、Haproxy等。

在生产环境,Tomcat服务器一般不单独使用在项目中,我们一般通过nginx用于反向代理的服务器,并将请求转发给后端多台Tomcat服务器,从而达到负载均衡的目的。

四、配置文件

root@server:~/apt install libgd-dev  #安装依赖
root@server:~/ wget http://nginx.org/download/nginx-1.22.1.tar.gz  #下载nginx
root@server:~/tar -zxvf nginx-1.22.1.tar.gz                       #解压nginx
root@server:~/nginx-1.22.1# ./configure --prefix=/www/env/nginx  #编译并指定安装位置
checking for OS
 + Linux 5.15.0-91-generic x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found   #这里提示要安装gcc解码
root@server:~/nginx-1.22.1# apt-get install gcc
root@server:~/nginx-1.22.1# ./configure --prefix=/www/env/nginx
问题1:

./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
问题2:
./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 执行编译并安装
root@server:~/nginx-1.22.1# apt install libpcre3 libpcre3-dev  
root@server:~/nginx-1.22.1# ./configure --prefix=/www/env/nginx #这里重新编译
 nginx path prefix: "/www/env/nginx"   ----------------------- 提示安装详情
  nginx binary file: "/www/env/nginx/sbin/nginx"
  nginx modules path: "/www/env/nginx/modules"
  nginx configuration prefix: "/www/env/nginx/conf"
  nginx configuration file: "/www/env/nginx/conf/nginx.conf"
  nginx pid file: "/www/env/nginx/logs/nginx.pid"
  nginx error log file: "/www/env/nginx/logs/error.log"
  nginx http access log file: "/www/env/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
root@server:~/nginx-1.22.1# make && make install     执行make进行安装
Command 'make' not found, but can be installed with:  没有make
apt install make        # version 4.3-4.1build1, or
apt install make-guile  # version 4.3-4.1build1
root@server:~/nginx-1.22.1# apt-get install make       下载make
root@server:~/nginx-1.22.1# make && make install      重新安装
root@server:/www/env/nginx# ll                  安装之后进入路径进行查看
total 24                                        就会看到这几个文件夹
drwxr-xr-x 6 root root 4096 Jan 26 04:45 ./
drwxr-xr-x 3 root root 4096 Jan 26 04:45 ../
drwxr-xr-x 2 root root 4096 Jan 26 04:45 conf/
drwxr-xr-x 2 root root 4096 Jan 26 04:45 html/
drwxr-xr-x 2 root root 4096 Jan 26 04:45 logs/
drwxr-xr-x 2 root root 4096 Jan 26 04:45 sbin/
root@server:/www/env/nginx/sbin# ./nginx  启动nginx
root@server:/www/env/nginx/sbin# ps -aux | grep nginx   查看进程nginx
root        7131  0.0  0.0   4620   364 ?        Ss   04:48   0:00 nginx: master process ./nginx
nobody      7132  0.0  0.1   5372  2572 ?        S    04:48   0:00 nginx: worker process
root        7138  0.0  0.1   6480  2280 pts/1    S+   04:49   0:00 grep --color=auto nginx
root@server:/www/env/nginx/conf# vim nginx.conf  进入配置文件进行代理配置

 ../sbin/nginx -t         验证nginx配置文件是否正确
 ../sbin/nginx -s reload  平滑加载

这里启动两台代理服务器搭配主机来进行测试,其中的代理服务器也要安装nginx并配置web内容(这里以前发过,不会可以查看以前的文章),关闭防火墙 

测试

 到这里90%就完成了,再测试一下其他几个反向代理算法。

分配策略


none(轮询)


upstream按照轮询(默认)方式进行负载,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。虽然这种方式简便、成本低廉。但缺点是:可靠性低和负载分配不均衡。

weight(权重)
upstream nginx_boot{
   # 30s内检查心跳发送两次包,未回复就代表该机器宕机,请求分发权重比为1:2
   server 192.168.1.13 weight=100 max_fails=2 fail_timeout=30s; 
   server 192.168.1.14 weight=200 max_fails=2 fail_timeout=30s;
   # 这里的IP请配置成你WEB服务所在的机器IP
}
weight 


   指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 


ip_hash(访问ip)


每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

配置只需要在upstream中加入ip_hash;即可。

upstream tomcats {
      ip_hash;
      server 127.0.0.1:9001;
      server 127.0.0.1:9002;
}

fair(第三方)


可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配,Nginx本身默认是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块。

upstream tomcats {
      server 127.0.0.1:9001;
      server 127.0.0.1:9002;
      fair;
}

url_hash(第三方)


 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率,Nginx本身默认是不支持url_hash的,如果需要这种高度算法,必须安装Nginx的hash软件包。


least_conn 


  根据后端服务器的连接状况进行分配客户请求,连接最少的服务器将被有限分配客户端请求。


upstream tomcats  {
    server ip:8080; 
}

server {
    listen 80;
    server_name  www.lianggzone.com;

    location / {
        proxy_pass  http://tomcats;

        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}

  • 30
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

duoba_an

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值