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 相关文章推荐
destoon在各个服务器下设置URL Rewrite(伪静态)的方法
Jun 21 Servers
Nginx中break与last的区别详析
Mar 31 Servers
Nginx的反向代理实例详解
Mar 31 Servers
小程序后台PHP版本部署运行 LNMP+WNMP
Apr 01 Servers
nginx部署多前端项目的几种方法
May 25 Servers
了解Kubernetes中的Service和Endpoint
Apr 01 Servers
Tomcat执行startup.bat出现闪退的原因及解决办法
Apr 20 Servers
Apache SeaTunnel实现 非CDC数据抽取
May 20 Servers
Windows server 2012 NTP时间同步的实现
Jun 25 Servers
kubernetes集群搭建Zabbix监控平台的详细过程
Jul 07 Servers
详解apache编译安装httpd-2.4.54及三种风格的init程序特点和区别
Jul 15 Servers
win7配置本地ftp服务器的图文教程
Aug 05 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
php使用fgetcsv读取csv文件出现乱码的解决方法
2014/11/08 PHP
如何写php守护进程(Daemon)
2015/12/30 PHP
php使用curl通过代理获取数据的实现方法
2016/05/16 PHP
PHP mysqli_free_result()与mysqli_fetch_array()函数详解
2016/09/21 PHP
firefox下frameset取不到值的解决方法
2010/09/06 Javascript
事件绑定之小测试  onclick && addEventListener
2011/07/31 Javascript
javascript中call和apply方法浅谈
2013/09/27 Javascript
javascript去掉前后空格的实例
2013/11/07 Javascript
js弹出确认是否删除对话框
2014/03/27 Javascript
jQuery插件实现控制网页元素动态居中显示
2015/03/24 Javascript
利用JQuery写一个简单的异步分页插件
2016/03/07 Javascript
JS声明式函数与赋值式函数实例分析
2016/12/13 Javascript
Angular2自定义分页组件
2017/04/19 Javascript
JS对象序列化成json数据和json数据转化为JS对象的代码
2017/08/23 Javascript
Vue2.0基于vue-cli+webpack同级组件之间的通信教程(推荐)
2017/09/14 Javascript
微信小程序的日期选择器的实例详解
2017/09/29 Javascript
详解node字体压缩插件font-spider的用法
2018/09/28 Javascript
vue2.0 路由模式mode="history"的作用
2018/10/18 Javascript
JavaScript设计模式之责任链模式实例分析
2019/01/16 Javascript
js实现简单掷骰子效果
2019/10/24 Javascript
[01:27]2014DOTA2展望TI 剑指西雅图IG战队专访
2014/06/30 DOTA
Python中for循环详解
2014/01/17 Python
Python的内存泄漏及gc模块的使用分析
2014/07/16 Python
Python字典,函数,全局变量代码解析
2017/12/18 Python
几行Python代码爬取3000+上市公司的信息
2019/01/24 Python
详解Python3中ceil()函数用法
2019/02/19 Python
python3中利用filter函数输出小于某个数的所有回文数实例
2019/11/24 Python
python PIL/cv2/base64相互转换实例
2020/01/09 Python
python获取linux系统信息的三种方法
2020/10/14 Python
python代码实现图书管理系统
2020/11/30 Python
CSS3制作苹果风格键盘特效
2015/02/26 HTML / CSS
Skyscanner加拿大:全球旅行搜索平台
2018/11/19 全球购物
中专生自我鉴定
2013/12/17 职场文书
国庆节文艺活动方案
2014/02/03 职场文书
简单的大学生自我鉴定
2014/02/18 职场文书
初中英语教师个人工作总结
2015/02/09 职场文书