前面几篇记录下nginx的基本运功,代理服务器的访问,这里来试验下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端口的地址,展现的结果是基本五五开的。

还会随机访问到

以上两个截图,分别对应了我的两个tomcat服务器下的测试文件。

转载于:https://www.cnblogs.com/sunxun/p/9965181.html

Linux 下 nginx反向代理与负载均衡相关推荐

  1. 介绍下Nginx 反向代理与负载均衡

    Nginx的代理功能与负载均衡功能是最常被用到的,这篇就开门见山,先描述一些关于代理功能的配置,再说明负载均衡详细. Nginx 代理服务的配置说明 1.设置 404 页面导向地址 error_pag ...

  2. Linux系统——Nginx反向代理与负载均衡

    集群 集群是指一组(若干个)相互独立的计算机,利用高速通信网路组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运用各自服务的独立服务器.这些服务器之间可以彼此通信,协同向用户提 ...

  3. 从0开始,在Linux中配置Nginx反向代理、负载均衡、session共享、动静分离

    写这篇文章花费了我近一周的时间,参考网上许多优秀的博客文章,我不敢说写的很好,至少很全很详细.本文先介绍原理部分,然后再进行实战操作,我认为这样才会有更深的理解,不过这也导致了文章篇幅很长.但是,如果 ...

  4. Nginx反向代理,负载均衡,redis session共享,keepalived高可用

    本站点停止更新,请访问:blog.coocap.com 相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tom ...

  5. 【转】Nginx反向代理和负载均衡

    原文链接:http://www.cnblogs.com/shuoer/p/7820899.html Nginx反向代理和负载均衡 环境说明 由于我使用的是windows系统,所以我用虚拟机虚拟出来了3 ...

  6. 懂点 Nginx 反向代理与负载均衡,是面试加分项没有之一

    点击上方"方志朋",选择"置顶公众号" 技术文章第一时间送达! 学到老活到老 前端圈一直很新,一直要不停的学习,而且在进入大厂的路上,还要求熟悉一门后台语言等等 ...

  7. nginx ------反向代理和负载均衡

    nginx ------反向代理和负载均衡 最近由于公司的业务增长 服务器承受不住压力经常出现崩溃现象 为了解决 使用nginx的负载均衡解决,以下是操作步骤: 1.nginx 的负载均衡:将压力分散 ...

  8. Nginx反向代理与负载均衡等配置文件示例

    Nginx反向代理于负载均衡等配置文件示例 Nginx.conf配置文件 worker_processes 8;events {worker_connections 1024; }http {incl ...

  9. 基于Nginx反向代理及负载均衡

    基于Nginx反向代理及负载均衡 参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 只要没有被启用,默认就是 ...

最新文章

  1. 项目管理中如何进行多方合作
  2. java不能修改表_java中不可修改列表的类型是什么
  3. C++实现connected component连通分量(附完整源码)
  4. canvas 实现图片局部模糊_JavaScript中的图片处理与合成(四)
  5. Weblogic服务端请求伪造漏洞(SSRF)和反射型跨站请求伪造漏洞(CSS)修复教程...
  6. Linux 环境变量 $PATH
  7. CSS3(animation, trasfrom)总结
  8. Kroneker Tensor:克罗内克张量
  9. TCMalloc:线程缓存Malloc以及tcmalloc与ptmalloc性能对比
  10. ​深度学习基础 | Seq2seq+Attention
  11. 1.Head First Java --- 进入Java的世界
  12. VBA新手教程:抽卡模拟实战(3)
  13. 百度AI开放平台情感分类
  14. 华为2019校招笔试
  15. 制作sim卡 服务器,diao丝绅士日记-自己撸系列 篇一:从此不再做diao丝-自制SIM卡套篇...
  16. 核密度估计_拔剑-浆糊的传说_新浪博客
  17. 安全宝冯景辉:每周都有超过100G大型DDoS攻击
  18. 微软 游戏服务器,微软正式公布游戏串流服务「Project xCloud」
  19. Zbrush 导出置换 然后导入vray 在 3ds max 和 maya 设置
  20. 云原生相关介绍,25 个点很全面

热门文章

  1. JAVA-初步认识-第十三章-多线程(验证同步函数的锁)
  2. Algorithm I assignment Collinear
  3. UVA 11825 状态压缩DP+子集思想
  4. AWS安装CDH5.3-CentOS6.4中关键操作步骤
  5. placeholder=请输入用户名(六位数字加英文字母)
  6. Unity3D 与 objective-c 之间数据交互。iOS SDK接口封装Unity3D接口
  7. RUNOOB python练习题31 根据已输入的字符判断星期几
  8. jQuery禁止Ajax请求缓存
  9. 软键盘弹起,导致底部被顶上去
  10. 漏洞发布平台-安百科技