Linux 下 nginx反向代理与负载均衡
前面几篇记录下nginx的基本运功,代理服务器的访问,这里来试验下nginx的反向代理。
在我们的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反向代理与负载均衡的更多相关文章
- Linux系统——Nginx反向代理与负载均衡
集群集群是指一组(若干个)相互独立的计算机,利用高速通信网路组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运用各自服务的独立服务器.这些服务器之间可以彼此通信,协同向用户提供 ...
- Linux 下 Nginx 反向代理 负载均衡配置
转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52036350 上一篇分享了 Nginx + JDK + Tomcat + MySQ ...
- Nginx反向代理,负载均衡,redis session共享,keepalived高可用
相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...
- 【转】Nginx反向代理和负载均衡
原文链接:http://www.cnblogs.com/shuoer/p/7820899.html Nginx反向代理和负载均衡 环境说明 由于我使用的是windows系统,所以我用虚拟机虚拟出来了3 ...
- Nginx反向代理与负载均衡应用实践(一)
Nginx反向代理与负载均衡应用实践(一) 链接:https://pan.baidu.com/s/1xB20bnuanh0Avs4kwRpSXQ 提取码:migq 复制这段内容后打开百度网盘手机App ...
- Nginx反向代理实现负载均衡配置图解
Nginx反向代理实现负载均衡配置图解 [导读] 负载均衡配置是超大型机器需要考虑的一些问题,同时也是数据安全的一种做法,下面我来介绍在nginx中反向代理 负载均衡配置图解,大家可参考本文章来操作. ...
- nginx反向代理与负载均衡
一:nginx反向代理与负载均衡配置思路与用法 1.nginx反向代理:就是你去相亲时,媒婆就是这里的代理,让媒婆带你去见姑娘 2.nginx负载均衡:就是有很多的媒婆经过商量给你选出最适合你的姑娘, ...
- 谁说前端不需要懂-Nginx反向代理与负载均衡
转:https://juejin.im/post/5b01336af265da0b8a67e5c9 学到老活到老 前端圈一直很新,一直要不停的学习,而且在进入大厂的路上,还要求熟悉一门后台语言等等.用 ...
- [转]Nginx反向代理和负载均衡部署指南
Nginx反向代理和负载均衡部署指南 1. 安装 1) 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最 ...
随机推荐
- .net Core2.2 WebApi通过OAuth2.0实现微信登录
前言 微信相关配置请参考 微信公众平台 的这篇文章.注意授权回调域名一定要修改正确. 微信网页授权是通过OAuth2.0机制实现的,所以我们可以使用 https://github.com/china- ...
- SpringMVC中ModelAndView对象与“视图解析器”
摘要: spring MVC这个环境中,Spring MVC会依据controller(或者你叫它handler)中处理方法的返回值,进行解析,解析之后提供一个视图,作为响应. 标注了@Control ...
- Linux查看日志定位问题
1.定位错误关键字所在行数 cat -n test.log |grep "查找的错误关键字" 2.得到错误关键字所在行号(假设为第500行),查询错误关键字前后100行数据 cat ...
- 10.25 正睿停课训练 Day9
目录 2018.10.25 正睿停课训练 Day9 A 数独(思路 DP) B 红绿灯(最短路Dijkstra) C 轰炸(计算几何 圆并) 考试代码 B C 2018.10.25 正睿停课训练 Da ...
- curl解压gzip页面gzcompress内容
$headers = array( //"Content-type:application/json;charset='utf-8'", "Cache-Control:n ...
- C++泛型编程(2)--通过排序和查找元素理解迭代器
许多C++开源库(stl,opencv,ros和blas等)都使用了大量的泛型编程的思想,如果不理解这些思想,将很难看懂代码,而<泛型编程与STL>一书对理解泛型编程思想非常的有帮助,这里 ...
- ArcGIS中国工具2.5正式发布
ArcGIS中国工具2.5正式发布 1. 支持1:500,1:1000,1:2000的梯形接幅表和图框打印(见3.2) 2. 支持接幅表,一定间隔如1秒加节点生成并能自动识别打印(见3. ...
- MX4_ADB
一.Ubuntu环境1. 建立或修改文件 ~/.android/adb_usb.ini,在文件开头或末尾添加一行,内容是0x2a45. 2. 建立或修改文件 /etc/udev/rules.d/51- ...
- Knockout.Js官网学习(enable绑定、disable绑定)
enable绑定 enable绑定使DOM元素只有在参数值为 true的时候才enabled.在form表单元素input,select,和textarea上非常有用. enable简单示例 < ...
- 使用three.js写全景图,使用sprite类canvas,结合射线,点击跳转指定全景图【转】
https://blog.csdn.net/WDCCSDN/article/details/81214804 话不多说上代码: 1.html: <!DOCTYPE html> < ...