Nginx反向代理及负载均衡如何实现(基于linux)


Posted in Servers onMarch 31, 2021

这里来试验下nginx的反向代理。

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

在我们的java项目中的体现就是,通过80端口访问,Nginx接收到,然后进行转发给tomcat服务器,再将服务器的结果给返回。

这里需要修改nginx.conf文件。

upstream backend {
  #代理的IP weight权重大的,接收的访问量就大,反之
  server localhost:8084 weight=50;
  server localhost:8088 weight=50;
}

将接收的请求进行转发:

# / 所有做负载均衡 + 反向代理
    location / {
      root  /data/wwwroot1;
      index index.html index.htm;#索引文件
      proxy_pass  http://backend;
    }

这样,通过请求nginx的请求,就可以被分配转发到tomcat上去。这里我是定义了两台tomcat服务器,同时用来做负载均衡的处理。通过设置weight,可以控制访问量。

具体配置代码如下;

#user nobody;
# worker 工作进程 一般设置 CPU数 * 核数
worker_processes 1;
 
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
 
#pid    logs/nginx.pid;
 
# 设置连接特性
events {
  worker_connections 1024;#1个worker产生多少个连接数
}
 
# 配置HTTP服务器的主要段
http {
  include    mime.types;
  default_type application/octet-stream;
 
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #         '$status $body_bytes_sent "$http_referer" '
  #         '"$http_user_agent" "$http_x_forwarded_for"';
 
  #access_log logs/access.log main;
 
  sendfile    on;
  #tcp_nopush   on;
 
  #keepalive_timeout 0;
  keepalive_timeout 65;
     
  #gzip压缩功能设置
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  gzip_vary on;
   
  #设定负载均衡后台服务器列表
  upstream backend {
    #代理的IP weight权重大的,接收的访问量就大,反之
    server localhost:8084 weight=50;
    server localhost:8088 weight=50;
  }
   
   
   
  server {
    listen    2022;
    server_name localhost;
    charset utf-8;
    access_log logs/wwwroot2.access.log main;
    location / {
      root  /data/wwwroot2;
      index index.html index.htm;#索引文件
    }
  }
  # 虚拟主机段
  server {
    listen    80;
    server_name localhost;
    root /data/wwwroot1;
    charset utf-8;
    #访问日志
    access_log logs/wwwroot1.access.log main;
    # / 所有做负载均衡 + 反向代理
    location / {
      root  /data/wwwroot1;
      index index.html index.htm;#索引文件
      proxy_pass  http://backend;
    }
 
    error_page 404       /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
 
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #  proxy_pass  http://127.0.0.1;
    #}
 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #  root      html;
    #  fastcgi_pass  127.0.0.1:9000;
    #  fastcgi_index index.php;
    #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    #  include    fastcgi_params;
    #}
 
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #  deny all;
    #}
  }
 
 
  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #  listen    8000;
  #  listen    somename:8080;
  #  server_name somename alias another.alias;
 
  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}
 
 
  # HTTPS server
  #
  #server {
  #  listen    443 ssl;
  #  server_name localhost;
 
  #  ssl_certificate   cert.pem;
  #  ssl_certificate_key cert.key;
 
  #  ssl_session_cache  shared:SSL:1m;
  #  ssl_session_timeout 5m;
 
  #  ssl_ciphers HIGH:!aNULL:!MD5;
  #  ssl_prefer_server_ciphers on;
 
  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}
 
}

测试结果发现,通过访问80端口的地址,展现的结果是基本五五开的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Servers 相关文章推荐
Nginx 502 Bad Gateway错误原因及解决方案
Mar 31 Servers
Nginx反爬虫策略,防止UA抓取网站
Mar 31 Servers
Apache Linkis 中间件架构及快速安装步骤
Mar 16 Servers
从零开始在Centos7上部署SpringBoot项目
Apr 07 Servers
阿里云k8s服务升级时502错误 springboot项目应用
Apr 09 Servers
关于windows server 2012 DC 环境 重启后蓝屏代码:0xc00002e2的问题
May 25 Servers
WinServer2012搭建DNS服务器的方法步骤
Jun 10 Servers
nginx之内存池的实现
Jun 28 Servers
centos环境下nginx高可用集群的搭建指南
Jul 23 Servers
Windows Server 2016服务器用户管理及远程授权图文教程
Aug 14 Servers
教你使用RustDesk 搭建一个自己的远程桌面中继服务器
Aug 14 Servers
Flink 侧流输出源码示例解析
Sep 23 Servers
Nginx配置80端口访问8080及项目名地址方法解析
Mar 31 #Servers
Nginx配置https原理及实现过程详解
Mar 31 #Servers
如何在centos上使用yum安装rabbitmq-server
Mar 31 #Servers
Windows下使用Nginx+Tomcat做负载均衡的完整步骤
阿里云Nginx配置https实现域名访问项目(图文教程)
详解Nginx 工作原理
fastdfs+nginx集群搭建的实现
You might like
Thinkphp无限级分类代码
2015/11/11 PHP
php使用PDO执行SQL语句的方法分析
2017/02/16 PHP
基于swoole实现多人聊天室
2018/06/14 PHP
[IE&FireFox兼容]JS对select操作
2007/01/07 Javascript
JavaScript子窗口ModalDialog中操作父窗口对像
2012/12/11 Javascript
Jquery为单选框checkbox绑定单击click事件
2012/12/18 Javascript
javascript一元操作符(递增、递减)使用示例
2013/08/07 Javascript
jquery validation验证身份证号,护照,电话号码,email(实例代码)
2013/11/06 Javascript
jquery选择器之层级过滤选择器详解
2014/01/27 Javascript
jQuery $.extend()用法总结
2014/06/15 Javascript
js事件处理程序跨浏览器解决方案
2016/03/27 Javascript
jQuery插件datatables使用教程
2016/04/21 Javascript
使用jquery给指定的table动态添加一行、删除一行
2016/10/13 Javascript
JavaScript实现简单图片轮播效果
2017/08/21 Javascript
详解PHP后期静态绑定分析与应用
2018/03/21 Javascript
在vue.js中使用JSZip实现在前端解压文件的方法
2018/09/05 Javascript
p5.js实现简单货车运动动画
2019/10/23 Javascript
[35:26]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第三局
2016/02/26 DOTA
python将图片文件转换成base64编码的方法
2015/03/14 Python
解读Python中degrees()方法的使用
2015/05/18 Python
python操作redis的方法
2015/07/07 Python
Django 前后台的数据传递的方法
2017/08/08 Python
利用python为运维人员写一个监控脚本
2018/03/25 Python
Python中pandas模块DataFrame创建方法示例
2018/06/20 Python
用于业余项目的8个优秀Python库
2018/09/21 Python
使用 tf.nn.dynamic_rnn 展开时间维度方式
2020/01/21 Python
Python实现弹球小游戏
2020/08/01 Python
美国知名运动产品零售商:Foot Locker
2016/07/23 全球购物
Wedgwood英国官方网站:英式精致骨瓷餐具、礼品与生活精品,源于1759年
2019/09/02 全球购物
班级学习计划书
2014/04/27 职场文书
第一批党的群众路线教育实践活动总结报告
2014/07/03 职场文书
超市开店计划书
2014/09/15 职场文书
欢迎词范文
2015/01/27 职场文书
幼儿园大班开学寄语(2016秋季)
2015/12/03 职场文书
Go语言基础知识点介绍
2021/07/04 Golang
漫画《催眠麦克风-Dawn Of Divisions》第二卷PV公开
2022/04/05 日漫