server 192.168.1.100:10024;
server 192.168.1.99:10024;
}

2. 將server節(jié)點下的location節(jié)點中的proxy_pass配置為:http:// upstream名稱,即“
http://testBlance”.

location / {
root html;
index index.html index.htm;
proxy_pass http://testBlance;
}

 

==============================location配置示例==================

#根據(jù)后綴名分發(fā)請求

location ~.(gif|jpg|png|jpeg|css|js|html) {
root resources;
}

對應(yīng)轉(zhuǎn)發(fā)的地址為:

http://192.168.1.119/images/head.png

http://127.0.0.1/css/index.css

http://127.0.0.1/css/index.css

http://127.0.0.1/js/consult_add.js

http://127.0.0.1/html/common_problem.html

nginx根目錄下新建目錄resources,resources中新建目錄css,js,html,images對應(yīng)存放不同類型的文件.

===================================================================

nginx配置文件nginx.conf詳細說明:

nginx.conf

========================start=================================

#定義Nginx運行的用戶和用戶組

user www www;

 

#nginx進程數(shù),建議設(shè)置為等于CPU總核心數(shù)。

worker_processes 8;

 

#全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]

error_log logs/error.log info;

 

#進程文件

pid logs/nginx.pid;

 

#一個nginx進程打開的最多文件描述符數(shù)目,理論值應(yīng)該是最多打開文件數(shù)(系統(tǒng)的值ulimit -n)與nginx進程數(shù)相除,但是nginx分配請求并不均勻,所以建議與ulimit -n的值保持一致。

worker_rlimit_nofile 65535;

 

#工作模式與連接數(shù)上限

events

{

#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內(nèi)核中的高性能網(wǎng)絡(luò)I/O模型,如果跑在FreeBSD上面,就用kqueue模型。

use epoll;

#單個進程最大連接數(shù)(最大連接數(shù)=連接數(shù)*進程數(shù))

worker_connections 65535;

}

 

#設(shè)定http服務(wù)器

http

{

include mime.types; #文件擴展名與文件類型映射表

default_type application/octet-stream; #默認文件類型

#charset utf-8; #默認編碼

server_names_hash_bucket_size 128; #服務(wù)器名字的hash表大小

client_header_buffer_size 32k; #上傳文件大小限制

large_client_header_buffers 4 64k; #設(shè)定請求緩

client_max_body_size 8m; #設(shè)定請求緩

sendfile on; #開啟高效文件傳輸模式,sendfile指令指定nginx是否調(diào)用sendfile函數(shù)來輸出文件,對于普通應(yīng)用設(shè)為 on,如果用來進行下載等應(yīng)用磁盤IO重負載應(yīng)用,可設(shè)置為off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的負載。注意:如果圖片顯示不正常把這個改成off。

autoindex on; #開啟目錄列表訪問,合適下載服務(wù)器,默認關(guān)閉。

tcp_nopush on; #防止網(wǎng)絡(luò)阻塞

tcp_nodelay on; #防止網(wǎng)絡(luò)阻塞

keepalive_timeout 120; #長連接超時時間,單位是秒

 

#FastCGI相關(guān)參數(shù)是為了改善網(wǎng)站的性能:減少資源占用,提高訪問速度。下面參數(shù)看字面意思都能理解。

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模塊設(shè)置

gzip on; #開啟gzip壓縮輸出

gzip_min_length 1k; #最小壓縮文件大小

gzip_buffers 4 16k; #壓縮緩沖區(qū)

gzip_http_version 1.0; #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)

gzip_comp_level 2; #壓縮等級

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

#壓縮類型,默認就已經(jīng)包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。

gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m; #開啟限制IP連接數(shù)的時候需要使用

 

upstream youtuyouliao {

#upstream的負載均衡,weight是權(quán)重,可以根據(jù)機器配置定義權(quán)重。weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大。

server 192.168.80.121:80 weight=3;

server 192.168.80.122:80 weight=2;

server 192.168.80.123:80 weight=3;

}

 

#虛擬主機的配置

server

{

#監(jiān)聽端口

listen 80 default_server;

#域名可以有多個,用空格隔開

server_name www.youtuyouliao.com youtuyouliao.com;

index index.html index.htm;

 

#日志格式設(shè)定

log_format access \\\’$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 access;

 

#對 "/" 啟用反向代理

location / {

proxy_pass http://youtuyouliao;

proxy_redirect off;

proxy_set_header X-Real-IP $remote_addr;

#后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#以下是一些反向代理的配置,可選。

proxy_set_header Host $host;

client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù)

client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù),

proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時時間(代理連接超時)

proxy_send_timeout 90; #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時)

proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時)

proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小

proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的設(shè)置

proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2)

proxy_temp_file_write_size 64k;

#設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳

}

 

#禁止訪問

location ~ ^/(WEB-INF)/ {

deny all;

}

 

#設(shè)定查看Nginx狀態(tài)的地址

location /NginxStatus {

stub_status on;

access_log on;

auth_basic "NginxStatus";

auth_basic_user_file conf/htpasswd;

#htpasswd文件的內(nèi)容可以用apache提供的htpasswd工具來產(chǎn)生。

}

 

#本地動靜分離反向代理配置

#所有jsp的頁面均交由tomcat或resin處理

location ~ .(jsp|jspx|do)?$ {

proxy_pass http://youtuyouliao;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

#所有靜態(tài)文件由nginx直接讀取不經(jīng)過tomcat或resin

location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {

expires 15d;

}

 

#JS和CSS緩存時間設(shè)置

location ~ .*.(js|css)?$ {

expires 1h;

}

}

}

========================end=================================

參考:http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html

 

更多關(guān)于云服務(wù)器域名注冊,虛擬主機的問題,請訪問三五互聯(lián)官網(wǎng):www.shinetop.cn

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。郵箱:3140448839@qq.com。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:三五互聯(lián)知識庫 » Nginx配置upstream實現(xiàn)負載均衡 nginx配置文件nginx.conf詳細說明

登錄

找回密碼

注冊