Nginx 做JavaWeb负载均衡
随着用户量的增大,单台服务器已经满足不了用户的需求。
准备工作:安装 gcc、pcre-devel、zlib、OpenSSL 一下是在线 离线请戳这里
gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
zlib
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
Nginx 下载地址
1、解压 nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
2、编译、安装nginx
./configure make make install
3、启动 nginx
[root@localhost nginx-1.8.1]# whereis nginx
nginx: /usr/local/nginx
[root@localhost nginx-1.8.1]# cd /usr/local/nginx/
[root@localhost nginx]# ./sbin/nginx #启动
4、可以去访问ip nginx 端口默认是80 看到一下界面就代表成功了一半 PS:如果访问不到,可以试着把防火墙关掉。建议是 开启80 端口

5、准备两个tomcat 当然 你可以在同一台服务器上,然后改成不痛端口。我这边是在两台服务器上。一台IP:10.13.5.11 另一台IP:10.13.5.12。为了方便区别是两个不同的tomcat 我把index.jsp 修改成以下:
第一台IP:10.13.5.11

第二台IP:10.13.5.12

6、配置 nginx configure
[root@localhost nginx]# cd /usr/local/nginx/conf/
a、配置后端服务器组
upstream testsite.com{
server 10.13.5.11:8080 weight=1;
server 10.13.5.12:8080 weight=2;
}
b、使用服务器组 注意!!!!! 如果 location / { proxy_pass http://testsite.com; } proxy_pass http://testsite.com 末尾可以不用加 / ,但是 location /other_string/ { proxy_pass http://testsite.com/; } proxy_pass http://testsite.com 之后一定要加 /
location /mysite/ {
proxy_pass http://testsite.com/;
proxy_redirect default;
}
c、最后的所有代码
#user nobody;
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;
} 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 on; upstream testsite.com{
server 10.13.5.11:8080 weight=1;
server 10.13.5.12:8080 weight=2;
} server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} location /mysite/ {
proxy_pass http://testsite.com/;
proxy_redirect default;
} #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;
# }
#} }
7、重启nginx
[root@localhost conf]# ./sbin/nginx -s reload
8、成功
第一张访问 IP:10.13.5.12 的服务器

第二张是访问IP:10.13.5.11 的服务器

Nginx 做JavaWeb负载均衡的更多相关文章
- windows下利用nginx 做IIS负载均衡
如果网站流量变大,就想加服务器分担压力,当然就要用到负载均衡,在windows 2003有自带的网络负载均衡,但配置还是挺麻烦的虽然有轮训和iphash的效果,但效果不算好. nginx小巧,下载不到 ...
- 利用nginx做tcp负载均衡
当前nginx-13.1已经支持tcp,ucp,unix域套接字三种负载均衡模式(http肯定支持,这个不用说).最近有需求需要对后端服务做负载均衡,因此考虑使用nginx来做. 1. 下载nginx ...
- nginx做反向负载均衡,后端服务器获取真实客户端ip(转)
首先,在前端nginx上需要做如下配置: location / proxy_set_hearder host $host; proxy_set_header X-forw ...
- nginx做反向负载均衡,后端服务器获取真实客户端ip
首先,在前端nginx上需要做如下配置: location / proxy_set_header host $host; proxy_set_header X-fo ...
- Nginx 简单的负载均衡配置演示样例
近期在做开放查询应用的时候,因为数据两天特别多,两千多万条呢,用户訪问需求也比較大,所以就用nginx做了 负载均衡,以下是改动之后的相关内容. http://www.cnblogs.com/xiao ...
- Nginx 简单的负载均衡配置示例(转载)
原文地址:Nginx 简单的负载均衡配置示例(转载) 作者:水中游于 www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP. 用户访问http://www ...
- 观nginx与lvs负载均衡的较量
在技术工作者中,常用到的就是lvs负载均衡和Nginx负载均衡了.这两者也是比较普及的.那么,根据不同的需求,两者存在着不同的优势.具体选择哪一个,还要看您的要求了.那么我们在此为大家分享一篇文章,对 ...
- Nginx+Tomcat+Memcached负载均衡集群服务搭建
操作系统:CentOS6.5 本文档主要讲解,如何在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群服务器,Nginx负责负载均衡,Tomcat负责实际服务,Memc ...
- nginx 健康检查和负载均衡机制分析
nginx 是优秀的反向代理服务器,这里主要讲它的健康检查和负载均衡机制,以及这种机制带来的问题.所谓健康检查,就是当后端出现问题(具体什么叫出现问题,依赖 于具体实现,各个实现定义不一样),不再往这 ...
随机推荐
- 2019秋季PAT甲级_备考总结
2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...
- python 之 数据库(数据库安装方法、基本sql语句、存储引擎)
第十章 数据库 10.1 数据库介绍 1.数据库相关概念 数据库服务器:本质就是一个台计算机,该计算机之上安装有数据库管理软件的服务端 数据库管理系统RDBMS:本质就是一个C/S架构的套接字软件 库 ...
- 【HC89S003F4开发板】 8c转义成汇编工程
HC89S003F4开发板建立汇编工程 选择编译文件 @选用开发板闪灯例程,将例程删除多余的注释,后面生成的文件会更直观. #define ALLOCATE_EXTERN #include " ...
- 网络地址转换(NAT)
NAT是解决ipv4地址短缺的方案之一 NAT是将位于子网中的主机与外网连通,子网中所有的主机都可以通过路由器的网络地址转换访问外网.对于外网来说该路由器相当于一台完整的主机,子网内所有主机对外网的访 ...
- 【SoloPi】SoloPi使用2-功能使用,录制回放
Soloπ是什么Soloπ是一个无线化.非侵入式的Android自动化工具,公测版拥有录制回放.性能测试.一机多控三项主要功能,能为测试开发人员节省宝贵时间. 录制回放功能在Soloπ的录制模式对应用 ...
- VS.NET(C#)--2.1认识控件
Web控件 四种控件 1.HTML控件 2.HTML服务器控件 在服务器端执行 3.ASP.NET服务器控件 在服务器端执行 4.用户控件和自定义控件 用户自定义控件在服务器端执行 注意: ...
- 什么叫工业4.0,这篇接地气的文章终于讲懂了(ZT)
原地址:https://www.cnblogs.com/namei/p/6110382.html 笔者早年从事过工业自动化行业,后来去了几个城市,讲过<工业互联网与工业文明史>这门课,以至 ...
- 扩展 MongoDB.Driver 支持实体
针对MongoDB的官方C#驱动进行扩展 一.安装 Install-Package Apteryx.MongoDB.Driver.Extend 移步我的项目https://github.com/cod ...
- windows下批处理保留指定日期下的文件
@echo offchcp 65001setlocal enabledelayedexpansion ::设置操作路径set "pic_dir=D:\465"echo 开始清理.. ...
- 【转载】Sqlserver中使用Round函数对计算结果四舍五入
在实际应用的计算中,很多时候我们需要对最后计算结果四舍五入,其实在Sqlserver中也有对应的四舍五入函数,就是Round函数,Round函数的格式为Round(column_name,decima ...