Nginx 是一个高性能的 HTTP 和 反向代理 服务器, 也是一个 IMAP/POP3/SMTP 代理服务器 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的, 第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日其将源代码以类 BSD 许可证的形式发布, 因它的稳定性丰富的功能集示例配置文件和低系统资源的消耗而闻名
下面小编就为大家分享一篇 nginx+tomcat 实现 Windows 系统下的负载均衡搭建教程, 具有很好的参考价值, 希望对大家有所帮助
刚入行没多久就听过负载均衡'的大名, 到现在因为工作接触的少, 所以没什么太多的认识但自己又对其非常的好奇, 所以前两天通过查资料, 在自己的笔记本上就搭建了一个超简单的案例(工作中没有时间, 晚上到家了条件又不够, 只能用自己的笔记本将就一下了, 重在理解思想)
通俗点将, 负载均衡就是因为访问流量太大, 导致项目访问不流畅甚至宕掉, 所以通过一种分流的方式来缓解这种情况
首先, 安装两个 tomcat, 可以是同一个复制成两个, 也可以下载两个不同版本的 tomcat, 我就是下载了两个不同版本的下载地址: https://tomcat.apache.org/download-80.cgi
(这是 8.0 版本的, 随便找两个不是特别老的版本的就行)
然后启动两个 tomcat, 在启动前, 先更改其中一个的端口号, 使得两个 tomcat 启动时不会端口冲突, 一个是本身的 8080 端口, 一个是改成了 9080 端口配好以后, 打开 cmd 命令窗口, 我的 tomcat 一个放在 D:\software\apache-tomcat-8.5.24 目录下, 按照如下命令即可启动, 启动成功会弹出另一个窗口, 显示如下:
打开浏览器, 输入 http://localhost:9080/, 出现如下界面即 tomcat 启动成功另一个采用同样的步骤即可
图 1:tomcat8 图 2:tomcat7
再之后, 安装一个 nginx, 我装的是稳定版的 nginx, 下载地址: http://nginx.org/download/nginx-1.12.2.zip, 解压即可使用
在启动前, 必须要对 nginx 进行一下配置才可实现负载均衡的功能, 打开 conf 文件夹, 下面有一个 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;
- # 以下四行是新添加的, 两个 IP 是两个 tomcat 的访问地址, weight 表示分给该服务器的请求比重, 两个都是 1, 则按照 1:1 来分配,
- upstream netitcast.com{
- server 127.0.0.1:8080 weight=1;
- server 127.0.0.1:9080 weight=2;
- }
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- # 一下两行是进行修改的, http://netitcast.com 和上面添加的要保持一致
- location / {
- proxy_pass http://netitcast.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;
- # }
- #}
- }
还是打开 cmd 窗口, 进入到以上目录, 执行命令: start nginx 即启动成功, 然后输入网址: http://localhost/index.jsp, 不断的进行访问, 就会发现, 上方显示的图 1 和图 2 在交互的显示因为以上的配置 weight 是以 1:2 的比重来分配的, 所以 9080 端口的比重就大一些, 访问到图一 (9080 端口) 的几率就比较大, 访问到图二 (8080 端口) 的几率比较小, 这个概率一个是三分之二, 一个是三分之一
当然, 这只是一个简单的示例, 在生产环境中开发, 肯定还要解决许多问题, 比如会话保持'等, 这个以后明白了再来补充写这个的目的就是让自己对负载均衡更加熟悉, 希望大家以后一起努力, 加油!!!
来源: http://www.phperz.com/article/18/0317/362601.html