首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

JavaWeb 使用nginx负载均衡

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;

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地址,由两台服务器分别接收到请求,至此完成了负载均衡的配置.

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181226G1IAJJ00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券