编程技术分享平台

网站首页 > 技术教程 正文

JavaWeb 使用nginx负载均衡(java中nginx)

xnh888 2024-10-17 17:18:46 技术教程 11 ℃ 0 评论

Nginx是什么

Nginx是一个轻量级、高性能、稳定性高、并发性好的HTTP和反向代理服务器。

核心功能

· 负载均衡

负载均衡:多在高并发情况下需要使用。其原理就是将数据流量分摊到多个服务器执行,减轻每台服务器的压力,多台服务器(集群)共同完成工作任务,从而提高了数据的吞吐量。

· 反向代理

代理我们要访问的目标服务器。代理服务器接受请求,然后将请求转发给内部网络的服务器(集群化),此时代理服务器对外就表现为一个服务器。

· 动静分离

Nginx提供的动静分离是指把动态请求和静态请求分离开,合适的服务器处理相应的请求,使整个服务器系统的性能、效率更高。

Centos下搭建Nginx

接下来要实现的是两台服务器上装tomcat,一台服务器装Nginx,由Nginx接受请求并将请求转发到相应的服务器.

· gcc 安装

yum install gcc-c++

· PCRE pcre-devel 安装

yum install -y pcre pcre-devel

· zlib

yum install -y zlib zlib-devel

· OpenSSL

yum install -y openssl openssl-devel

Nginx 下载地址

· 解压 nginx-1.8.1.tar.gz

tar -zxvf nginx-1.8.1.tar.gz

cd nginx-1.8.1

· 编译、安装nginx

./configure

make

make install

· 启动 nginx

cd /usr/local/nginx/

./sbin/nginx

·访问ID端口默认是80,出现如下界面,代表Nignx安装成功

· 配置 nginx configure

cd /usr/local/nginx/conf/

vim nginx.conf

我的配置文件如下

#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 174.137.63.58:8080 weight=1;
 server 94.191.14.127: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 /myweb/ {
 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;
 # }
 #}
 
}

· 改完配置文件之后重启nginx

/usr/local/nginx/sbin/nginx -s reload

接下来我们使用http://自己的IP/myweb/访问,结果如下

第一张图片If you're seeing this英文前面没有IP,后面另一台有IP是另一台服务器,我们访问相同的IP地址,由两台服务器分别接收到请求,至此完成了负载均衡的配置.

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表