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 相关文章推荐
Filebeat 采集 Nginx 日志的方法
Mar 31 Servers
Nginx如何配置Http、Https、WS、WSS的方法步骤
May 11 Servers
Nginx location 和 proxy_pass路径配置问题小结
Sep 04 Servers
图文详解Nginx版本平滑升级方案
Sep 15 Servers
nginx内存池源码解析
Nov 20 Servers
Nginx+Windows搭建域名访问环境的操作方法
Mar 17 Servers
Nginx中使用Lua脚本与图片的缩略图处理的实现
Mar 18 Servers
Windows Server 2016 配置 IIS 的详细步骤
Apr 28 Servers
docker compose 部署 golang 的 Athens 私有代理问题
Apr 28 Servers
在Windows Server 2012上安装 .NET Framework 3.5 所遇到的问题
Apr 29 Servers
源码安装apache脚本部署过程详解
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
Terran魔法科技
2020/03/14 星际争霸
ftp类(myftp.php)
2006/10/09 PHP
yii2使用GridView实现数据全选及批量删除按钮示例
2017/03/01 PHP
搭建自己的PHP MVC框架详解
2017/08/16 PHP
PHP DB 数据库连接类定义与用法示例
2019/03/11 PHP
PHP中的输出echo、print、printf、sprintf、print_r和var_dump的示例代码
2020/12/01 PHP
FCK调用方法..
2006/12/21 Javascript
TreeView 用法(有代码)(asp.net)
2011/07/15 Javascript
jquery选择器之内容过滤选择器详解
2014/01/27 Javascript
node.js中的fs.fchownSync方法使用说明
2014/12/16 Javascript
jquery中EasyUI使用技巧小结
2015/02/10 Javascript
JS使用parseInt解析数字实现求和的方法
2015/08/05 Javascript
javascript实现3D变换的立体圆圈实例
2015/08/06 Javascript
js实现无缝滚动特效
2015/12/20 Javascript
浅谈javascript中的Function和Arguments
2016/08/30 Javascript
jquery层级选择器(匹配父元素下的子元素实现代码)
2016/09/05 Javascript
JavaScript两个变量交换值的实现方法
2017/03/01 Javascript
详解React开发必不可少的eslint配置
2018/02/05 Javascript
vue.js实现左边导航切换右边内容
2019/10/21 Javascript
JavaScript实现滑动门效果
2020/01/18 Javascript
vue 调用 RESTful风格接口操作
2020/08/11 Javascript
vue实现选中效果
2020/10/07 Javascript
Flask 让jsonify返回的json串支持中文显示的方法
2018/03/26 Python
Python Logging 日志记录入门学习
2018/06/02 Python
[原创]Python入门教程4. 元组基本操作
2018/10/31 Python
python生成多个只含0,1元素的随机数组或列表的实例
2018/11/12 Python
python实现飞行棋游戏
2020/02/05 Python
python suds访问webservice服务实现
2020/06/26 Python
python实现简单的井字棋游戏(gui界面)
2021/01/22 Python
Strathberry苏贝瑞中国官网:西班牙高级工匠手工打造
2020/10/19 全球购物
50岁生日感言
2014/01/23 职场文书
语文教学感言
2014/02/06 职场文书
杜甫草堂导游词
2015/02/03 职场文书
公司周年庆典致辞
2015/07/30 职场文书
2016新教师岗前培训心得体会
2016/01/08 职场文书
详解Vue项目的打包方式(生成dist文件)
2022/01/18 Vue.js