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 相关文章推荐
Linux中Nginx的防盗链和优化的实现代码
Jun 20 Servers
Nginx部署vue项目和配置代理的问题解析
Aug 04 Servers
Apache Linkis 中间件架构及快速安装步骤
Mar 16 Servers
Nginx中使用Lua脚本与图片的缩略图处理的实现
Mar 18 Servers
Minikube搭建Kubernetes集群
Mar 31 Servers
了解Kubernetes中的Service和Endpoint
Apr 01 Servers
Docker 镜像介绍以及commit相关操作
Apr 13 Servers
Tomcat配置访问日志和线程数
May 06 Servers
Linux中文件的基本属性介绍
Jun 01 Servers
Windows10安装Apache2.4的方法步骤
Jun 25 Servers
windows系统安装配置nginx环境
Jun 28 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
浅谈电磁辐射对健康的影响
2021/03/01 无线电
一个简洁的PHP可逆加密函数(分享)
2013/06/06 PHP
PHP Cookie学习笔记
2016/08/23 PHP
关于ThinkPHP中的异常处理详解
2018/05/11 PHP
javascript转换字符串为dom对象(字符串动态创建dom)
2010/05/10 Javascript
基于jquery的跨域调用文件
2010/11/19 Javascript
javascript 三种方法实现获得和设置以及移除元素属性
2013/03/20 Javascript
js的flv视频播放器插件使用方法
2015/06/23 Javascript
基于Echarts 3.19 制作常用的图形(非静态)
2016/05/19 Javascript
js图片上传前预览功能(兼容所有浏览器)
2016/08/24 Javascript
js 概率计算(简单版)
2017/09/12 Javascript
JavaScript通过mouseover()实现图片变大效果的示例
2017/12/20 Javascript
解决nodejs的npm命令无反应的问题
2018/05/17 NodeJs
如何利用nodejs自动定时发送邮件提醒(超实用)
2020/12/01 NodeJs
[02:29]DOTA2英雄基础教程 陈
2013/12/17 DOTA
[01:12:35]Spirit vs Navi Supermajor小组赛 A组败者组第一轮 BO3 第二场 6.2
2018/06/03 DOTA
Python输出汉字字库及将文字转换为图片的方法
2016/06/04 Python
python如何删除文件中重复的字段
2019/07/16 Python
Python坐标线性插值应用实现
2019/11/13 Python
python实现银行实战系统
2020/02/26 Python
python保留格式汇总各部门excel内容的实现思路
2020/06/01 Python
用html5的canvas和JavaScript创建一个绘图程序的简单实例
2016/07/06 HTML / CSS
韩国邮政旗下生鲜食品网上超市:epost
2016/08/27 全球购物
获取邓白氏信用报告:Dun & Bradstreet
2019/01/22 全球购物
Java如何格式化日期
2012/08/07 面试题
3.12植树节活动总结2014
2014/03/13 职场文书
《灰椋鸟》教学反思
2014/04/27 职场文书
服务承诺书范文
2014/05/19 职场文书
社团活动总结怎么写
2014/06/30 职场文书
婚礼证婚人演讲稿
2014/09/13 职场文书
2014年实验室工作总结
2014/12/03 职场文书
2015年青年志愿者工作总结
2015/05/20 职场文书
导游词之贵州百里杜鹃
2019/10/29 职场文书
vscode中使用npm安装babel的方法
2021/08/02 Javascript
使用redis生成唯一编号及原理示例详解
2021/09/15 Redis
MySQL新手入门进阶语句汇总
2022/09/23 MySQL