Nginx 做JavaWeb负载均衡
来源:互联网 发布:java线程状态 编辑:程序博客网 时间:2024/06/04 00:40
Nginx 做JavaWeb负载均衡
转载地址:https://www.cnblogs.com/eggplantpro/p/7445795.html随着用户量的增大,单台服务器已经满足不了用户的需求。
准备工作:安装 gcc、pcre-devel、zlib、OpenSSL 一下是在线 离线请戳这里
gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
1yum install gcc-c++
PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
1yum install -y pcre pcre-devel
zlib
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
1yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
1yum install -y openssl openssl-devel
Nginx 下载地址
1、解压 nginx-1.8.1.tar.gz
12tar -zxvf nginx-
1.8
.
1
.tar.gz
cd nginx-
1.8
.
1
2、编译、安装nginx
12345./configure
make
make install
3、启动 nginx
1234[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
1[root
@localhost
nginx]# cd /usr/local/nginx/conf/
a、配置后端服务器组
1234
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 之后一定要加 /
1234
location /mysite/ {
proxy_pass http:
//testsite.com/;
proxy_redirect
default
;
}
c、最后的所有代码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126#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
1[root
@localhost
conf]# ./sbin/nginx -s reload
8、成功
第一张访问 IP:10.13.5.12 的服务器
第二张是访问IP:10.13.5.11 的服务器
标签: nginx
随着用户量的增大,单台服务器已经满足不了用户的需求。
准备工作:安装 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负载均衡
- nginx 做均衡负载
- Nginx做负载均衡
- Nginx+tomcat 做负载均衡
- 利用Nginx做负载均衡
- Nginx +Resin做负载均衡
- 利用Nginx做负载均衡
- 利用Nginx做负载均衡
- 利用Nginx做负载均衡
- nginx做负载均衡配置
- 利用Nginx做负载均衡
- 利用Nginx做负载均衡
- nginx 做mysql负载均衡
- 使用nginx做负载均衡
- 【Nginx】使用Nginx做负载均衡
- nginx+keepalived做负载均衡解决方案
- Nginx 做负载均衡的几种轮询策略
- nginx做负载均衡时session共享
- include标签在shtml页面中不起作用
- 记一个诡异的UDP问题
- python opencv批量读入图像并进行文本检测
- 【ML学习笔记】11:Accuracy/FAR/FRR/Precision/Recall/ROC/AUC
- 扫雷
- Nginx 做JavaWeb负载均衡
- 微服务架构中模块划分和服务识别
- 微信小程序一个你可能需要的功能
- Arduino语音天气预报(一)
- Windowing Functions In Hive
- Spark学习笔记1-RDD编程
- 信数金服决策引擎分享(三):如何实现决策的协同维护?
- 移动web(三)touch事件详解
- C语言实现动态顺序表