1. 需要的工具
nginx-1.8.1.zip 點擊下載
tomcat 8 點擊下載
2. 實現的步驟
(1)解壓 nginx 至相應的目錄(本人目錄: F:\jd\tomcat_nginx\nginx-1.8.1);
(2)打開文件夾 nginx-1.8.1 找到 nginx.exe 運行文件雙擊,如果界面一閃而過,輸入地址: http://localhost 則會出現以下頁面,nginx 的默認端口為 80;
(3) 解壓 tomcat 至相應的目錄(本人目錄:F:\jd\tomcat_nginx\apache-tomcat-8.0.47-18080 和 F:\jd\tomcat_nginx\apache-tomcat-8.0.47-28080);
(4)修改 tomcat 端口為 18080 的 server.xml 文件(目錄: F:\jd\tomcat_nginx\apache-tomcat-8.0.47-18080\conf)為以下內容,為避免啟動程序出現錯誤,共修改了三處位置:
(5)為了區別兩個 tomcat 的差別,刪除所有 apache-tomcat-8.0.47-18080\webapps\ROOT 目錄下的所有文件,並且新建一個 index.jsp ,添加內容為 <h1>Tomcat 18080</h1>,啟動 tomcat 服務輸入 http://localhost:18080,如果成功則出現以下頁面:
(6)修改 tomcat 端口為 28080 的 server.xml 文件,修改步驟重復第(4)(5)步;
(7)配置 nginx 來實現負載均衡,打開目錄 F:\jd\tomcat_nginx\nginx-1.8.1\conf 找到 nginx.conf 文件並進行如下修改:
以上為主要的修改位置,下面是該文件的全部文件信息,添加了注解:
1 #user nobody; 2 worker_processes 1; # 工作進程的個數,一般與計算機的cpu核數一致 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 11 events { 12 worker_connections 1024; # 單個進程最大連接數(最大連接數 = 連接數 * 進程數) 13 } 14 15 16 http { 17 include mime.types; # 文件擴展名與文件類型映射表 18 default_type application/octet-stream; # 默認文件類型 19 20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 # '$status $body_bytes_sent "$http_referer" ' 22 # '"$http_user_agent" "$http_x_forwarded_for"'; 23 24 #access_log logs/access.log main; 25 26 sendfile on; # 開啟高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設為on,如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。 27 #tcp_nopush on; 28 29 #keepalive_timeout 0; 30 keepalive_timeout 65; # 長連接超時時間,單位是秒 31 32 #gzip on; # 啟用Gizp壓縮 33 34 #服務器的集群 35 upstream yjq { # 服務器集群名字 36 server 127.0.0.1:18080 weight=1; # 服務器配置 weight是權重的意思,權重越大,分配的概率越大。 37 server 127.0.0.1:28080 weight=2; 38 } 39 40 # 當前的Nginx的配置 41 server { 42 listen 80; # 監聽80端口,可以改成其他端口 43 server_name localhost; # 當前服務的域名 44 45 #charset koi8-r; 46 47 #access_log logs/host.access.log main; 48 49 location / { 50 # root html; 51 # index index.html index.htm; 52 proxy_pass http://yjq; 53 proxy_redirect default; 54 } 55 56 #error_page 404 /404.html; 57 58 # redirect server error pages to the static page /50x.html 59 # 60 error_page 500 502 503 504 /50x.html; 61 location = /50x.html { 62 root html; 63 } 64 65 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 66 # 67 #location ~ \.php$ { 68 # proxy_pass http://127.0.0.1; 69 #} 70 71 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 72 # 73 #location ~ \.php$ { 74 # root html; 75 # fastcgi_pass 127.0.0.1:9000; 76 # fastcgi_index index.php; 77 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 78 # include fastcgi_params; 79 #} 80 81 # deny access to .htaccess files, if Apache's document root 82 # concurs with nginx's one 83 # 84 #location ~ /\.ht { 85 # deny all; 86 #} 87 } 88 89 90 # another virtual host using mix of IP-, name-, and port-based configuration 91 # 92 #server { 93 # listen 8000; 94 # listen somename:8080; 95 # server_name somename alias another.alias; 96 97 # location / { 98 # root html; 99 # index index.html index.htm; 100 # } 101 #} 102 103 104 # HTTPS server 105 # 106 #server { 107 # listen 443 ssl; 108 # server_name localhost; 109 110 # ssl_certificate cert.pem; 111 # ssl_certificate_key cert.key; 112 113 # ssl_session_cache shared:SSL:1m; 114 # ssl_session_timeout 5m; 115 116 # ssl_ciphers HIGH:!aNULL:!MD5; 117 # ssl_prefer_server_ciphers on; 118 119 # location / { 120 # root html; 121 # index index.html index.htm; 122 # } 123 #} 124 125 }
補充說明:
在http節點里添加:
# 定義負載均衡設備的 Ip及設備狀態
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用負載的Server節點下添加
proxy_pass http://myServer;
upstream 每個設備的狀態;
down 表示當前的 server 暫時不參與負載 ;
weight 默認為 1,weight 越大,負載的權重就越大;
max_fails 允許請求失敗的次數默認為 1,當超過最大次數時,返回 proxy_next_upstream 模塊定義的錯誤 ;
fail_timeout:max_fails 次失敗后,暫停的時間;
backup 其它所有的非 backup 機器 down 或者忙的時候,請求 backup 機器,所以這台機器壓力會最輕;
(8)最后輸入網址 http://localhost 檢測(三個服務都必須打開,tomcat18080、tomcat28080、nginx.exe)
第一次打開的頁面:
第二次打開的頁面:
第三次打開的頁面:
第四次打開的頁面:
所以,現在基本上完成了Nginx + Tomcat 搭建負載均衡;
附錄:
如果系統占用了 80 端口,導致 nginx 不能啟動,可以通過 netstat -aon | findstr :80 命令查看80端口被誰占用,如果是系統占用,通過以下步驟解決:
Nginx 常用命令(切換到 nginx 安裝目錄來執行):
start nginx.exe: 啟動 nginx;
nginx.exe -s stop: 停止 nginx;
nginx.exe -s reload: 配置文件修改,重新加載配置文件;
nginx -t: 查看nginx是否啟動成功;
nginx -v: 查看nginx版本;