前面几篇记录下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服务器下的测试文件。

 

Linux 下 nginx反向代理与负载均衡的更多相关文章

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

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

  2. Linux 下 Nginx 反向代理 负载均衡配置

    转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52036350 上一篇分享了 Nginx + JDK + Tomcat + MySQ ...

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

    相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...

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

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

  5. Nginx反向代理与负载均衡应用实践(一)

    Nginx反向代理与负载均衡应用实践(一) 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App ...

  6. Nginx反向代理实现负载均衡配置图解

    Nginx反向代理实现负载均衡配置图解 [导读] 负载均衡配置是超大型机器需要考虑的一些问题,同时也是数据安全的一种做法,下面我来介绍在nginx中反向代理 负载均衡配置图解,大家可参考本文章来操作. ...

  7. nginx反向代理与负载均衡

    一:nginx反向代理与负载均衡配置思路与用法 1.nginx反向代理:就是你去相亲时,媒婆就是这里的代理,让媒婆带你去见姑娘 2.nginx负载均衡:就是有很多的媒婆经过商量给你选出最适合你的姑娘, ...

  8. 谁说前端不需要懂-Nginx反向代理与负载均衡

    转:https://juejin.im/post/5b01336af265da0b8a67e5c9 学到老活到老 前端圈一直很新,一直要不停的学习,而且在进入大厂的路上,还要求熟悉一门后台语言等等.用 ...

  9. [转]Nginx反向代理和负载均衡部署指南

    Nginx反向代理和负载均衡部署指南   1.        安装 1)         从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最 ...

随机推荐

  1. grant 权限 on 数据库对象 to 用户

    grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant select on testdb.* to common_ ...

  2. 4889: [Tjoi2017]不勤劳的图书管理员 树套树

    国际惯例的题面(Bzoj没有,洛谷找的):动态加权逆序对,一眼树套树.256MB内存,5e4范围,不虚不虚.首先把交换改成两个插入和两个删除.考虑插入和删除的贡献,就是统计前面比这个值大的数的数值和, ...

  3. [Agc005D]K Perm Counting

    [Agc005D] K Perm Counting Description 糟糕爷特别喜爱排列.他正在构造一个长度为N的排列.但是他特别讨厌正整数K.因此他认为一个排列很糟糕,当且仅当存在至少一个i( ...

  4. dataGridView使用指南系列一、回车换行或换列完美解决方案

    在使用datagridview控件时,默认按回车是跳转到下一行的当前列的,要想让按回车跳转到同一行的下一列该怎么做呢? 百度搜索了一下,大都是使用该控件的key_down事件和CellEndEdit进 ...

  5. 自定义Directive使用ngModel

    我们知道ngModel是AngularJS中默认的一个Directive,用于数据的双向绑定.通常是这样使用的: <input type="text" ng-model=&q ...

  6. extern字符串常量,宏定义字符串常量,怎么选

    在使用常量的时候,我看到主要有两种写法: #define RKLICURegexEnumerationOptionsErrorKey @"RKLICURegexEnumerationOpti ...

  7. caffe solver 配置详解

    caffe solver通过协调网络前向推理和反向梯度传播来进行模型优化,并通过权重参数更新来改善网络损失求解最优算法,而solver学习的任务被划分为:监督优化和参数更新,生成损失并计算梯度.caf ...

  8. java 集合框架 Java集合&List的实现

    原文: https://blog.csdn.net/u012658346/article/details/50917068 一.java集合类图如下所示: 上述类图中,实线边框的是实现类,比如Arra ...

  9. iOS:用Block写一个链式编程

    一.介绍 链式编程是一个比较新颖的编程方式,简单直观,用起来也比较舒服.目前比较有名的Mansory和BabyBlueTooth就是使用链式编程写的第三方框架. 二.写法 链式编程写法不同于传统方式, ...

  10. aglio报错解决

    Cannot write or read cache for themes (ENOENT on cache folder) aglio -i ./api.md -o api.html >> ...