win7+nginx均衡負載配置?

Tags: 伺服器, 負載,

win7+nginx均衡負載配置,可以大大減低伺服器的負載,增強伺服器的穩定性;

工具/原料

(nginx-1.5.8.zip)

方法/步驟

一、 下載Nginx(nginx-1.5.8.zip)

解壓到D:/serve目錄下

win7+nginx均衡負載配置

雙擊執行nginx.exe,螢幕一閃而過或者在DOS裡面敲打命令,如圖所示:

win7+nginx均衡負載配置

可以看到螢幕一閃而過,接下來測試一下是否安裝啟動成功;

win7+nginx均衡負載配置

在工作管理員的程序中檢視nginx.exe是否啟動

win7+nginx均衡負載配置

若看到該映像名稱,證明nginx程式已經啟動成功!

這時我們就可以在瀏覽器中訪問:

win7+nginx均衡負載配置

這樣我們就成功的搭建了一個nginx服務!!

Nginx+Tomcat負載均衡配置這裡只需要修改Nginx的配置檔案nginx.conf,讓它通過tomcat來轉發。

Nginx配置檔案完成如下:

#使用的使用者和組,window下不指定

#user nobody;

#指定工作衍生程序數(一般等於CPU總和數或總和數的兩倍,例如兩個四核CPU,則總和數為8)

worker_processes 1;

#指定錯誤日誌檔案存放路徑,錯誤日誌級別可選項為【debug info notice warn error crit】

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#指定pid存放路徑

#pid logs/nginx.pid;

#工作模式及連線數上限

events {

#使用網路I/O模型,Linux系統推薦使用epoll模型,FreeBSD系統推薦使用kqueue;window下不指定

#允許的連線數

#user epoll;

worker_connections 100;

}

#設定http伺服器,利用他的反向代理功能提供負載均衡支援

http {

#設定mime型別

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"';

client_header_buffer_size 1k;

large_client_header_buffers 4 4k;

access_log logs/access.log main;

#設定access log

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

#keepalive_timeout 0;

keepalive_timeout 65;

#開啟gzip模組

gzip on;

gzip_min_length 1100;

gzip_buffers 4 8k;

gzip_types text/plain application/x-javascript text/css application/xml;

output_buffers 1 32k;

postpone_output 1460;

server_names_hash_bucket_size 128;

client_max_body_size 8m;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

gzip_http_version 1.1;

gzip_comp_level 2;

gzip_vary on;

#設定負載均衡的伺服器列表

upstream localhost {

#設定負載均衡的伺服器列表

#ip_hash;

#同一機器在多網情況下,路由切換,ip可能不同 #weigth引數表示權值,權值越高被分配到的機率越大

server 192.168.101.222:8088 max_fails=2 fail_timeout=600s;

server 192.168.101.5:8081 max_fails=2 fail_timeout=600s;

}

#設定虛擬主機

server {

listen 80;

server_name localhost;

charset UTF-8;

#設定本虛擬主機的訪問日誌

access_log logs/host.access.log main;

#對 "/" 啟用負載均衡

location / {

root \\192.168.101.222\D:\web\apache-tomcat-6.0.37\webapps;

index index.html index.htm index.aspx;

proxy_redirect off;

#保留使用者真實資訊

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 12;

#連線成功後 等待後端伺服器響應時間 其實已進入後端的排隊之中等候處理

proxy_read_timeout 90;

#代理請求快取區 這個快取區間會儲存使用者的頭資訊一共Nginx進行規則處理 一般只要能儲存下頭資訊即可

proxy_send_timeout 90;

#同上 告訴Nginx儲存單個用的幾個Buffer最大用多大空間

proxy_buffer_size 4k;

proxy_buffers 4 32k;

#如果系統很忙的時候可以申請國內各大的proxy_buffers 官方推薦 *2

proxy_busy_buffers_size 64k;

#proxy 快取臨時檔案的大小

proxy_temp_file_write_size 64k;

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

proxy_max_temp_file_size 128m;

proxy_pass ;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

四、測試

前提是分別啟動兩臺機器的tomcat,其次啟動nginx服務;

在192.168.101.23上訪問nginx伺服器(通過訪問(專案路徑)來實現對192.168.101.222和192.168.101.5這兩個機器上應用程式的訪問,最終實現tomcat的均衡負載)

[1]關閉192.168.101.222上的tomcat服務,訪問192.168.101.23上的nginx服務,觀察192.168.101.5上的tomcat是否執行;

[2]關閉192.168.101.5上的tomcat服務,訪問192.168.101.23上的nginx服務,觀察192.168.101.222上的tomcat是否執行;

[3]兩個tomcat都啟動,訪問nginx服務,模擬併發使用者n個,觀察tomcat的執行情況;

如果[1]和[2]的tomcat都執行就說明搭建nginx代理服務成功,tomcat實現了均衡負載;

相關問題答案