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+Tomcat实现负载均衡、动静分离的原理解析
Mar 31 Servers
Nginx服务器添加Systemd自定义服务过程解析
Mar 31 Servers
Nginx代理同域名前后端分离项目的完整步骤
Mar 31 Servers
Nginx本地目录映射实现代码实例
Mar 31 Servers
扩展多台相同的Web服务器
Apr 01 Servers
制作能在nginx和IIS中使用的ssl证书
Jun 21 Servers
nginx反向代理配置去除前缀案例教程
Jul 26 Servers
Nginx配置之实现多台服务器负载均衡
Aug 02 Servers
NGINX 权限控制文件预览和下载的实现原理
Jan 18 Servers
解决IIS7下无法绑定https主机的问题
Apr 29 Servers
Nginx本地配置SSL访问的实例教程
May 30 Servers
nginx静态资源的服务器配置方法
Jul 07 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
从Web查询数据库之PHP与MySQL篇
2009/09/25 PHP
PHP zlib扩展实现页面GZIP压缩输出
2010/06/17 PHP
php iconv() : Detected an illegal character in input string
2010/12/05 PHP
PHP实例分享判断客户端是否使用代理服务器及其匿名级别
2014/06/04 PHP
PHP根据传来的16进制颜色代码自动改变背景颜色
2014/06/13 PHP
ThinkPHP中自定义目录结构的设置方法
2014/08/15 PHP
PHP中使用asort进行中文排序失效的问题处理
2014/08/18 PHP
php实现文件下载代码分享
2014/08/19 PHP
Yii2.0中的COOKIE和SESSION用法
2016/08/12 PHP
Jquery中给animation加更多的运作效果实例
2013/09/05 Javascript
js获得参数的getParameter使用示例
2014/02/26 Javascript
jQuery实现点击后标记当前菜单位置(背景高亮菜单)效果
2015/08/22 Javascript
js+css实现超简洁的二级下拉菜单效果代码
2015/09/07 Javascript
jquery实现表单获取短信验证码代码
2017/03/13 Javascript
深入理解Angular4订阅(Subscribe)与取消
2017/11/22 Javascript
Angular 5.x 学习笔记之Router(路由)应用
2018/04/08 Javascript
node实现基于token的身份验证
2018/04/09 Javascript
微信小程序分包加载代码实现方法详解
2019/09/23 Javascript
解决vue elementUI中table里数字、字母、中文混合排序问题
2020/01/07 Javascript
nodejs实现的http、https 请求封装操作示例
2020/02/06 NodeJs
JS操作JSON常用方法(10w阅读)
2020/12/06 Javascript
[01:56]林书豪DOTA2上海特级锦标赛励志短片
2016/03/05 DOTA
Python时区设置方法与pytz查询时区教程
2013/11/27 Python
Django+Ajax+jQuery实现网页动态更新的实例
2018/05/28 Python
python 字典中文key处理,读取,比较方法
2018/07/06 Python
python 使用poster模块进行http方式的文件传输到服务器的方法
2019/01/15 Python
瑞士首家网上药店折扣店:McDrogerie
2020/12/22 全球购物
Android interview questions
2016/12/25 面试题
查询优化的一般准则有哪些
2015/03/08 面试题
销售演讲稿范文
2014/01/08 职场文书
初三政治教学反思
2014/01/30 职场文书
春节联欢会主持词
2014/03/24 职场文书
教师岗位聘任书范文
2014/03/29 职场文书
入股协议书范本
2014/04/14 职场文书
安全环保标语
2014/06/09 职场文书
七年级作文之环保作文
2019/10/17 职场文书