環境:javascript
nginx1:192.168.68.41css
tomcat1:192.168.68.43java
tomcat2:192.168.68.45nginx
nginx安裝網上不少教程,我是用yum安裝的。vim
配置nginx:後端
vim /etc/nginx/conf.d/default.conf 緩存
內容:tomcat
#負責壓縮數據流 gzip on; gzip_min_length 1000; gzip_types text/plain text/css application/x-javascript; #設定負載均衡的服務器列表 #weigth參數表示權值,權值越高被分配到的概率越大 upstream hello{ server 192.168.68.43:8080 weight=1; server 192.168.68.45:8080 weight=1; } server { #偵聽的80端口 listen 80; server_name localhost; #設定查看Nginx狀態的地址 location /nginxstatus{ stub_status on; access_log on; auth_basic "nginxstatus"; auth_basic_user_file htpasswd; } #匹配以jsp結尾的,tomcat的網頁文件是以jsp結尾 location / { index index.jsp; proxy_pass http://hello; #在這裏設置一個代理,和upstream的名字同樣 #如下是一些反向代理的配置可刪除 proxy_redirect off; #後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #容許客戶端請求的最大單文件字節數 client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數 proxy_connect_timeout 300; #nginx跟後端服務器鏈接超時時間(代理鏈接超時) proxy_send_timeout 300; #後端服務器數據回傳時間(代理髮送超時) proxy_read_timeout 300; #鏈接成功後,後端服務器響應時間(代理接收超時) proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小 proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k如下的話,這樣設置 proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳 } }
啓動:服務器
nginxapp
退出:
nginx -s quit
啓動nginx後,訪問http://192.168.68.41/test/
會被導航到http://192.168.68.43:8080/test/ 和http://192.168.68.45:8080/test/
從而實現了負載均衡和避免單點故障。