Nginx+Windows負載均衡配置方法
2024-08-30 12:27:12
供稿:網友
一、下載Nginx
http://nginx.org/download/nginx-1.2.5.zip
解壓到C:/nginx目錄下
二、在兩臺服務器上分別建一個網站:
S1:192.168.16.35:8054
S2:192.168.16.16:8089
二、找到目錄
C:/nginx/conf/nginx.conf
打開nginx.conf
配置如下:
代碼如下:
#使用的用戶和組,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下不指定
#use epoll;
#允許的連接數
worker_connections 1024;
}
#設定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"';
#access_log logs/access.log main;
log_format main '$remote_addr - $remote_user [$time_local]'
'"$request" $status $bytes_sent'
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"'
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local]'
'"$request" $status $bytes_sent'
'"$http_referer" "$http_user_agent"'
'"$http_range" "$sent_http_content_range"';
#設定請求緩沖
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#設定access log
access_log logs/access.log main;
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計算將請求分配各那個后端tomcat,可以解決session問題
ip_hash;
#同一機器在多網情況下,路由切換,ip可能不同
#weigth參數表示權值,權值越高被分配到的幾率越大