...
http://blog.sina.com.cn/s/blog_5426e0180100dtyz.html
之前一直用apache來做weblogic的前端,由於nginx對靜態內容的出色性能,不得不轉投nginx。這里就不
再寫weblogic的安裝了。
安裝nginx
nginx需要pcre做支持,一般系統都自帶,當然可以自己下載高版本的源碼包安裝,建議大家使用高版本的pcre,
這樣使用正則的時候會有更好的支持。
首先去http://wiki.nginx.org//NginxChs下載nginx,我用了0.7
# tar zxvf nginx-0.7.59.tar.gz
# cd nginx-0.7.59
# ./configure --with-http_stub_status_module –prefix=/opt/nginx
# make
# make install
--with-http_stub_status_module 增加nginx的監控模塊,用來監控 Nginx 的當前狀態。
下面來配置nginx
配置文件,在/opt/nginx/conf下的nginx.conf,為了省事,直接復制我的配置文件過來:
#user nobody;
worker_processes 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll; //nginx處理連接的方法,epoll - 高效的方法,使用於Linux內核2.6版本及以后的系統。
worker_connections 51200;
}
http {
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 75;
#gzip on;
upstream 99ding {
server 124.42.*.***:7002 weight=10;
server 124.42.*.***:7001 weight=10;
} //因為我的weblogic沒有做集群,所以只能用nginx自帶的負載均衡,不過推薦使用weblogic的集群
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ ^/status/ {
stub_status on;
access_log off;
} //監控nginx的狀態:http://www.test.com/status/
location / {
root /opt/html/app;
index index.html index.htm;
expires 30d; //expires是設定cookie的存活時間,我用了30天
}
location ~ ^/(WEB-INF)/ {
deny all;
}
location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
root /opt/html/app;
expires 24h;
}
location ~ (\.jsp)|(\.do) {
proxy_pass http://test;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_max_temp_file_size 512m;
} //最關鍵的地方,將jsp和do文件轉給weblogic處理,這里使用的是上面創建的test(負載均衡的名字),如果不用
負載均衡,可以把test用 “ip:端口號”來代替,例如http://10.0.0.1:7001
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
接下來啟動服務即可,首先檢查配置文件是否有錯誤:
/opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
啟動服務:
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
------------------------
http://blog.wadecn.com/?p=214
nginx用做weblogic前端七層負載
- 准備安裝包(nginx和upstream_hash)
https://github.com/evanmiller/nginx_upstream_hash |
http://nginx.org/download/ |
- 編譯安裝
tar -zxvf nginx-0.7.65. tar .gz |
unzip cep21-nginx_upstream_hash-99ace64.zip |
patch -p0 < ../cep21-nginx_upstream_hash-99ace64/nginx.patch |
patching file src/http/ngx_http_upstream.h |
./configure --add-module=../cep21-nginx_upstream_hash-99ace64 --prefix=/ngboss/webproxy1 |
make && make install |
- 配置nginx
#user webproxy1 webproxy1; |
worker_processes 1; |
#error_log logs/error.log; |
#error_log logs/error.log notice; |
error_log logs/error.log info; |
pid logs/nginx.pid; |
events { |
use epoll; |
worker_connections 51200; |
} |
http { |
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; |
sendfile on; |
#tcp_nopush on; |
keepalive_timeout 15; |
#gzip on; |
userid on; |
userid_name QHAINGBOSSID; |
userid_domain 192.168.102.249; |
userid_path /; |
userid_expires 2d; |
upstream ngboss_cluster { |
hash $cookie_QHAINGBOSSID; |
server 192.168.102.249:8081; |
} |
upstream saleserv_cluster { |
hash $cookie_QHAINGBOSSID; |
server 192.168.102.249:8083; |
} |
server { |
listen 9090; |
server_name localhost; |
proxy_redirect off; |
location /saleserv { |
if ($request_uri ~* ".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$" ) { |
expires max; |
} |
proxy_pass http://saleserv_cluster; |
} |
location / { |
if ($request_uri ~* ".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$" ) { |
expires max; |
} |
proxy_pass http://ngboss_cluster; |
} |
} |
} |
- 起停控制
# 啟服務 |
sbin/nginx |
# 停服務 |
sbin/nginx -s stop |
# 平滑重啟 |
kill -HUP ` cat logs/nginx.pid ` |
==============================================
http://xiahongyuan.blog.51cto.com/906214/852635
http
{
……..
client_max_body_size 300m ; // 允許客戶端請求的最大單個文件字節數
client_body_buffer_size 128k;
// 緩沖區代理緩沖用戶端請求的最大字節數,可以理解為先保存到本地再傳給用戶
proxy_connect_timeout 600;
// 跟后端服務器連接的超時時間_發起握手等候響應超時時間
proxy_read_timeout 600;
// 連接成功后_等候后端服務器響應時間_其實已經進入后端排隊之中等候處理
proxy_send_timeout 600;
proxy_buffer_size 16k; // 會保存用戶的頭信息,供nginx進行規則處理
proxy_busy_buffers_size 64k;
proxy_max_temp_file_size 64k;
// proxy緩存臨時文件的大小
|
upstream clubsrv {
server 192.168.0.110:80 weight=5;
server 192.168.0.121:80 weight=5;
}
upstream mysrv {
server 192.168.0.32:80 weight=2;
server 127.0.0.1:8000 weight=8;
}
server {
listen 80;
server_name club.xywy.com;
charset gbk;
root /www;
access_log logs/aaa.log combined;
//下面是第一個域名,使用clubsrv的代理
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
// 如果后端服務器返回502、504或執行超時等錯誤,自動將請求轉發到upstream另一台服務器
proxy_pass http://clubsrv;
// 與上面upstream自己命名的名字填寫一致
proxy_redirect off;
proxy_set_header Host club.xywy.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
// nginx在前端做代理,后端的日志會顯示127.0.0.1,上面配置可以顯示用戶真實IP(還需裝第三方軟件,見下面的詳細說明)
index index.htm index.html index.php;
}
//下面是第二個域名,使用mysrv的代理,訪問www.sum.com/message目錄下的
server {
listen 80;
server_name www.sum.com;
location /message {
proxy_pass http://mysrv;
proxy_set_header Host $host;
// 訪問這個域名的,只有mysrv 本機可以訪問
}
//訪問除了/message之外的www.sum.com/ 地址,
location / {
proxy_pass http://mysrv;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
|
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
root html;
}
|
upstream mysrv {
ip_hash;
server 192.168.0.110:80 weight=2;
server 127.0.0.1:8000 down;
server 192.168.0.212:80 weight=8;
}
|
upstream mysrv {
ip_hash;
server www.xywy.com weight=2;
server 127.0.0.1:8000 down;
server 192.168.0.212:80 max_fails=3 fail_timeout=30s;
server unix:/tmp/bakend3;
}
|
=================================================
http://sns.linuxpk.com/blog-46744-15630.html
down到nginx-0.6.35,安裝后,配置很簡單。配置后問題如下:走nginx直接到WebLogic上的應用沒有問題;但是nginx到apache再到WebLogic上的應用就有問題。
日志中是302(nginx中"HTTP/1.1 302 2783",apache上是"HTTP/1.0 302 2771")。對比后,開始以為是HTTP/1.1和HTTP/1.0的問題。后來想到 Nginx 和瀏覽器使用 HTTP/1.1 進行對話,而在后台服務中使用 HTTP/1.0,是對的,然后注意到 2個web服務上得到的 返回給客戶端的不包括響應頭的字節數 是不一樣長的,這個缺的 12 字節缺在哪里還需要追究一下。
同時想起來訪問測試的時候訪問 /web 和訪問 /web/ 是不一樣的,apache上是可以處理掉的,注意到apache處理類似的資源請求時 日志中會先打 302,然后后面自動加 / ,之后日志中就200了。nginx沒有處理,不知道是沒有設置的緣故還是他本身就不能處理類似的問題。不過后者幾率應該不大,還得研究一下nginx配置和apache的相關處理機制。
再有想到用重寫機制來加上對"/"的處理,但是考慮到nginx代理到apache上也會報302,估計這樣也解決不了問題,還得研究一下proxy的配置。
OK,看看以上3條路 那條 是 正解。
====================================
http://www.cnblogs.com/huangjingzhou/articles/2177198.html
用nginx做weblogic的前端負載均衡器
## set uid and gid of process
user webproxy ngboss;
## how many process will be started
worker_processes
10
;
## worker_cpu_affinity define
worker_cpu_affinity
000000000100
000000001000
000000010000
000000100000
000001000000
000010000000
000100000000
001000000000
010000000000
100000000000
;
## how many open files will be allowd of each process
worker_rlimit_nofile
51200
;
## error log defind
error_log logs/error.log crit;
## save master process-id in file
pid logs/nginx.pid;
events {
## powered by epoll, good!
use epoll;
worker_connections
51200
;
}
http {
include mime.types;
default_type text/html;
## access log format defind
log_format main
'$remote_addr [$time_local] $request $status $body_bytes_sent'
;
## access log defind
access_log logs/access.log main;
## fast send file system call, good!
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_body_buffer_size 1024k;
proxy_connect_timeout
600
;
proxy_read_timeout
600
;
proxy_send_timeout
600
;
proxy_buffer_size 8k;
proxy_buffers
4
32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 1024k;
## set connection timeout (by second)
keepalive_timeout
30
;
## gzip setting begin.
gzip on;
gzip_min_length 1k;
gzip_buffers
4
16k;
gzip_http_version
1.1
;
gzip_comp_level
9
;
gzip_vary off;
gzip_types text/plain text/javascript text/css text/xml application/xml;
## gzip setting end.
output_buffers
4
32k;
postpone_output
1460
;
client_header_buffer_size 128k;
large_client_header_buffers
4
256k;
##
default
encoding
# charset GBK;
## app redirect && load-balancer start
# ngboss cluster
upstream ngboss_cluster {
ip_hash;
server
10.238
.
15.65
:
7101
;
server
10.238
.
15.66
:
7201
;
server
10.238
.
15.67
:
7301
;
server
10.238
.
15.68
:
7401
;
}
# saleserv cluster
upstream saleserv_cluster {
ip_hash;
server
10.238
.
15.65
:
8181
;
server
10.238
.
15.66
:
8281
;
server
10.238
.
15.67
:
8381
;
server
10.238
.
15.68
:
8481
;
server
10.238
.
15.65
:
8182
;
server
10.238
.
15.66
:
8282
;
server
10.238
.
15.67
:
8382
;
server
10.238
.
15.68
:
8482
;
}
# acctmanm cluster
upstream acctmanm_cluster {
ip_hash;
server
10.238
.
15.65
:
8191
;
server
10.238
.
15.66
:
8291
;
server
10.238
.
15.67
:
8391
;
server
10.238
.
15.68
:
8491
;
server
10.238
.
15.65
:
8192
;
server
10.238
.
15.66
:
8292
;
server
10.238
.
15.67
:
8392
;
server
10.238
.
15.68
:
8492
;
}
# custmanm cluster
upstream custmanm_cluster {
ip_hash;
server
10.238
.
15.65
:
8111
;
server
10.238
.
15.66
:
8211
;
}
# groupserv cluster
upstream groupserv_cluster {
ip_hash;
server
10.238
.
15.65
:
8183
;
server
10.238
.
15.66
:
8283
;
}
# salemanm cluster
upstream salemanm_cluster {
ip_hash;
server
10.238
.
15.65
:
8121
;
server
10.238
.
15.66
:
8221
;
}
# chnlmanm cluster
upstream chnlmanm_cluster {
ip_hash;
server
10.238
.
15.65
:
8101
;
server
10.238
.
15.66
:
8201
;
}
# resmanm cluster
upstream resmanm_cluster {
ip_hash;
server
10.238
.
15.65
:
8131
;
server
10.238
.
15.66
:
8231
;
}
# prodmcrm prodmbil bilmanm cluster
upstream prodmanm_cluster {
server
10.238
.
15.66
:
8261
;
}
# copmanm cluster
upstream copmanm_cluster {
server
10.238
.
15.66
:
8271
;
}
# sysmanm cluster
upstream sysmanm_cluster {
ip_hash;
server
10.238
.
15.65
:
8141
;
server
10.238
.
15.66
:
8241
;
}
# statmanm cluster
upstream statmanm_cluster {
ip_hash;
server
10.238
.
15.65
:
8151
;
server
10.238
.
15.66
:
8251
;
}
## app redirect && load-balancer end
server {
listen
10.238
.
15.101
:
18080
;
server_name
10.238
.
15.101
;
proxy_set_header X-Forwarded-For $remote_addr;
# charset GBK;
location /download {
root html;
proxy_redirect off;
}
location /saleserv {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//saleserv_cluster;
expires max;
break
;
}
proxy_pass http:
//saleserv_cluster;
proxy_redirect off;
}
location /acctmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//acctmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//acctmanm_cluster;
proxy_redirect off;
}
location /custmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//custmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//custmanm_cluster;
proxy_redirect off;
}
location /groupserv {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//groupserv_cluster;
expires max;
break
;
}
proxy_pass http:
//groupserv_cluster;
proxy_redirect off;
}
location /salemanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//salemanm_cluster;
expires max;
break
;
}
proxy_pass http:
//salemanm_cluster;
proxy_redirect off;
}
location /chnlmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//chnlmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//chnlmanm_cluster;
proxy_redirect off;
}
location /resmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//resmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//resmanm_cluster;
proxy_redirect off;
}
location /prodmcrm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//prodmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//prodmanm_cluster;
proxy_redirect off;
}
location /prodmbil {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//prodmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//prodmanm_cluster;
proxy_redirect off;
}
location /bilmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//prodmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//prodmanm_cluster;
proxy_redirect off;
}
location /copmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//copmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//copmanm_cluster;
proxy_redirect off;
}
location /sysmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//sysmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//sysmanm_cluster;
proxy_redirect off;
}
location /statmanm {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//statmanm_cluster;
expires max;
break
;
}
proxy_pass http:
//statmanm_cluster;
proxy_redirect off;
}
location /nginxstatus {
stub_status on;
access_log off;
allow all;
}
location / {
if
($request_uri ~*
".*\.(js|css|gif|jpg|jpeg|png|bmp|swf)$"
) {
proxy_pass http:
//ngboss_cluster;
expires max;
break
;
}
proxy_pass http:
//ngboss_cluster;
proxy_redirect off;
}
# redirect server error pages to the
static
page /50x.html
error_page
500
502
503
504
/50x.html;
location = /50x.html {
root html;
} <br>
|
====================================================
http://www.oschina.net/question/38648_20224
user nobody; worker_processes 16; error_log logs/error.log warn; pid logs/nginx.pid; worker_rlimit_nofile 65000; events { use epoll; worker_connections 65000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 16k; large_client_header_buffers 4 64k; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 60; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; client_max_body_size 16m; client_body_buffer_size 256k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_temp_path proxy_temp_dir; proxy_cache_path proxy_cache_dir levels=1:2 keys_zone=cache_one:256m inactive=2d max_size=1g; upstream auditsrv { server 10.xx.xx.x9:7011; server 10.xx.xx.x0:7011; server 10.xx.xx.x1:7011; server 10.xx.xx.x2:7011; ip_hash; } server { listen 80; server_name 10.xx.xx.xx; 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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 1d; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path proxy_temp_dir; if ( !-e $request_filename ) { proxy_pass http://auditsrv; } } location ~ .*\.(js|css)?$ { expires 2h; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path proxy_temp_dir; if ( !-e $request_filename ) { proxy_pass http://auditsrv; } } location / { proxy_pass http://auditsrv; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache_key $host$uri$is_args$args; proxy_cache cache_one; expires 1d; } location /nginxstatus { stub_status on; access_log on; allow 10.xx.xx.xx; deny all; } } }
我的后台是四個節點的weblogic服務,前端是使用Nginx做的反向代理,配置已貼出,現在的問題是界面點擊注銷后,會出現IE掛掉,從界面反映看得出IE在做死循環,似乎刷新頻率很高,CPU能漲到80%-90%,注銷使用的鏈接是xxxxx/logout.cmd?method=logout,不知道是出了什么問題。
問題解決了,似乎是因為我對動態頁面也設置了緩存才導致的,后來我把
location / { ... }
中的以下三行注釋掉后就沒有出現注銷失敗的問題了
proxy_cache_key $host$uri$is_args$args; proxy_cache cache_one; expires 1d;
看來動態頁面也做緩存的話問題還真多啊
--------------------------
=================================
http://www.graynight.org/?p=1083
現在網站發展的趨勢對網絡負載均衡的使用是隨着網站規模的提升根據不同的階段來使用不同的技術:
一種是通過硬件來進行,常見的硬件有比較昂貴的NetScaler、F5、Radware和Array等商用的負載均衡器,它的優點就是有專業的維護團隊來對這些服務進行維護、缺點就是花銷太大,所以對於規模較小的網絡服務來說暫時還沒有需要使用;另外一種就是類似於LVS/HAProxy、Nginx的基於Linux的開源免費的負載均衡軟件策略,這些都是通過軟件級別來實現,所以費用非常低廉,所以我個也比較推薦大家采用第二種方案來實施自己網站的負載均衡需求。
近期朋友劉鑫(紫雨荷雪)的項目成功上線了,PV達到了億級/日的訪問量,最前端用的是HAProxy+Keepalived雙機作的負載均衡器/反向代理,整個網站非常穩定;這讓我更堅定了以前跟老男孩前輩聊的關於網站架構比較合理設計的架構方案:即Nginx/HAProxy+Keepalived作Web最前端的負載均衡器,后端的MySQL數據庫架構采用一主多從,讀寫分離的方式,采用LVS+Keepalived的方式。
在這里我也有一點要跟大家申明下:很多朋友擔心軟件級別的負載均衡在高並發流量沖擊下的穩定情況,事實是我們通過成功上線的許多網站發現,它們的穩定性也是非常好的,宕機的可能性微乎其微,所以我現在做的項目,基本上沒考慮服務級別的高可用了。相信大家對這些軟件級別的負載均衡軟件都已經有了很深的的認識,下面我就它們的特點和適用場合分別說明下。
LVS:使用集群技術和Linux操作系統實現一個高性能、高可用的服務器,它具有很好的可伸縮性(Scalability)、可靠性(Reliability)和可管理性(Manageability),感謝章文嵩博士為我們提供如此強大實用的開源軟件。
LVS的特點是:
1、抗負載能力強、是工作在網絡4層之上僅作分發之用,沒有流量的產生,這個特點也決定了它在負載均衡軟件里的性能最強的;
2、配置性比較低,這是一個缺點也是一個優點,因為沒有可太多配置的東西,所以並不需要太多接觸,大大減少了人為出錯的幾率;
3、工作穩定,自身有完整的雙機熱備方案,如LVS+Keepalived和LVS+Heartbeat,不過我們在項目實施中用得最多的還是LVS/DR+Keepalived;
4、無流量,保證了均衡器IO的性能不會收到大流量的影響;
5、應用范圍比較廣,可以對所有應用做負載均衡;
6、軟件本身不支持正則處理,不能做動靜分離,這個就比較遺憾了;其實現在許多網站在這方面都有較強的需求,這個是Nginx/HAProxy+Keepalived的優勢所在。
7、如果是網站應用比較龐大的話,實施LVS/DR+Keepalived起來就比較復雜了,特別后面有Windows Server應用的機器的話,如果實施及配置還有維護過程就比較復雜了,相對而言,Nginx/HAProxy+Keepalived就簡單多了。
Nginx的特點是:
1、工作在網絡的7層之上,可以針對http應用做一些分流的策略,比如針對域名、目錄結構,它的正則規則比HAProxy更為強大和靈活,這也是許多朋友喜歡它的原因之一;
2、Nginx對網絡的依賴非常小,理論上能ping通就就能進行負載功能,這個也是它的優勢所在;
3、Nginx安裝和配置比較簡單,測試起來比較方便;
4、也可以承擔高的負載壓力且穩定,一般能支撐超過幾萬次的並發量;
5、Nginx可以通過端口檢測到服務器內部的故障,比如根據服務器處理網頁返回的狀態碼、超時等等,並且會把返回錯誤的請求重新提交到另一個節點,不過其中缺點就是不支持url來檢測;
6、Nginx僅能支持http和Email,這樣就在適用范圍上面小很多,這個它的弱勢;
7、Nginx不僅僅是一款優秀的負載均衡器/反向代理軟件,它同時也是功能強大的Web應用服務器。LNMP現在也是非常流行的web架構,大有和以前最流行的LAMP架構分庭抗爭之勢,在高流量的環境中也有很好的效果。
8、Nginx現在作為Web反向加速緩存越來越成熟了,很多朋友都已在生產環境下投入生產了,而且反映效果不錯,速度比傳統的Squid服務器更快,有興趣的朋友可以考慮用其作為反向代理加速器。
HAProxy的特點是:
1、HAProxy是支持虛擬主機的,以前有朋友說這個不支持虛擬主機,我這里特此更正一下。
2、能夠補充Nginx的一些缺點比如Session的保持,Cookie的引導等工作
3、支持url檢測后端的服務器出問題的檢測會有很好的幫助。
4、它跟LVS一樣,本身僅僅就只是一款負載均衡軟件;單純從效率上來講HAProxy更會比Nginx有更出色的負載均衡速度,在並發處理上也是優於Nginx的。
5、HAProxy可以對Mysql讀進行負載均衡,對后端的MySQL節點進行檢測和負載均衡,不過在后端的MySQL slaves數量超過10台時性能不如LVS,所以我向大家推薦LVS+Keepalived。
6、HAProxy的算法現在也越來越多了,具體有如下8種:
①roundrobin,表示簡單的輪詢,這個不多說,這個是負載均衡基本都具備的;
②static-rr,表示根據權重,建議關注;
③leastconn,表示最少連接者先處理,建議關注;
④source,表示根據請求源IP,這個跟Nginx的IP_hash機制類似,我們用其作為解決session問題的一種方法,建議關注;
⑤ri,表示根據請求的URI;
⑥rl_param,表示根據請求的URl參數'balance url_param' requires an URL parameter name;
⑦hdr(name),表示根據HTTP請求頭來鎖定每一次HTTP請求;
⑧rdp-cookie(name),表示根據據cookie(name)來鎖定並哈希每一次TCP請求
=======================
http://wb1019.blog.163.com/blog/static/128145892011417102339833/
1、 上傳weblogic安裝文件和nginx的源代碼包(略)
2、 安裝weblogic8.16並破解
#groupadd bea
#useradd -d /bea -g bea weblogic
#passwd weblogic
#chmod +x server816_linux32.bin
#su – weblogic
$./server816_linux32.bin
(1) 回車
(2) 選擇1,1 - Yes, I agree with the terms of the license
(3) 選擇1,1|Create a new BEA Home
(4) 輸入/bea,"BEA Home" = [/bea] (根據實際情況選擇)
(5) 選擇1,1|Yes, Use this BEA home directory [/bea]
(6) 選擇1,->1|Complete
|Install the complete BEA WebLogic Platform.
(7) 回車,"Product Installation Directory" = [/bea/weblogic81]
(8) 選擇1,1|Yes, use this product directory [/bea/weblogic81]
$cp /opt/weblogic7_8_full_license/license.bea /bea/
$cp /opt/weblogic7_8_full_license/weblogic_sp.jar /bea/weblogic81/server/lib/
$chmod +x /bea/weblogic81/server/lib/weblogic_sp.jar
3、 部署weblogic8.16的集群
$cd /bea/weblogic81/common/bin/
$./config.sh
(1) 選擇1,->1|Create a new WebLogic configuration
(2) 選擇4,4|Basic WebLogic Server Domain 8.1.6.0
(3) 選擇2,2|No
(4) 配置管理平台信息
| Name | Value |
|__________________|_____________________|
1| *Name: | AdminServer |
2| Listen address: | All Local Addresses |
3| Listen port: | 6666 |
4| SSL listen port: | N/A |
5| SSL enabled: | false |
(5) 選擇1,1|Yes
| Name* | Listen address | Listen port | SSL listen port | SSL enabled |
|_________|_____________________|_____________|_________________|_____________|
1| lishi01 | All Local Addresses | 8001 | N/A | false |
2| lishi02 | All Local Addresses | 8002 | N/A | false |
(6) 一路回車,直道出現以下界面
| Name | Value |
_|_________________________|_________________________________|
1| *User name: | weblogic |
2| *User password: | |
3| *Confirm user password: | |
4| Description: | The default administration user |
(7) 並且更改為以下
| Name | Value |
_|_________________________|_________________________________|
1| *User name: | weblogic |
2| *User password: | ******** |
3| *Confirm user password: | ******** |
4| Description: | The default administration user |
(8) 模式的選擇,這里我是測試,選擇的是開發模式,根據需要自行選擇
->1|Development Mode
2|Production Mode
(9) Jdk的選擇,這里也是根據工程的開發使用得jdk來選擇,這里我選擇的sun的jdk
1|JRockit SDK 1.4.2_10-8160 @ /bea/jrockit81sp6_142_10
2|Sun SDK 1.4.2_11 @ /bea/jdk142_11
3|Other Java SDK
(10)域名的安裝位置,默認就好"Target Location" = [/bea/user_projects/domains]
(11)輸入你的域名
(12)回車開始安裝
4、 配置集群
$cd /bea/user_projects/domains/lishi/
$vi StartAdmin.sh
nohup ./startWebLogic.sh > console.log &
$vi Start_All_Server.sh
nohup ./startManagedWebLogic.sh lishi01 http://ip:port > lishi01.log &
nohup ./startManagedWebLogic.sh lishi02 http://ip:port > lishi02.log &
$chmod +x StartAdmin.sh
$./StartAdmin.sh
等待管理服務器啟動后
$chmod +x Start_All_Server.sh
$./Start_All_Server.sh
(1) 單擊Cluster
(2) Configure a new Cluster...
(3) 輸入cluster name,點擊create
(4) 配置組播ip以及端口

(5) 添加服務器到Cluster中

(6) 依次點擊Machines,Configure a new Machine
(7) 創建Machine

(8) 配置ip以及端口

(9) 添加服務器到Machine

5、 nginx的負載均衡
#tar xzvf nginx-0.7.66.tar.gz
#cd nginx-0.7.66
#./configure --prefix=/opt/nginx --with-http_stub_status_module
#make
#make install
#vi /opt/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 8;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 51200;
}
http {
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;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
upstream lishi
{
server 156.16.34.44:8001;
server 156.16.34.44:8002;
}
server {
listen 80;
server_name 156.16.34.44;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location / {
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;
proxy_pass http://lishi;
}
location /nginxstatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";}
#auth_basic_user_file conf/htpassword;
}
======================================
Nginx 是一個輕量級高性能的 Web 服務器, 並發處理能力強, 對資源消耗小, 無論是靜態服務器還是小網站, Nginx 表現更加出色, 作為 Apache 的補充和替代使用率越來越高.
我在《Apache 虛擬主機 VirtualHost 配置》介紹了在不同操作系統上使用 Apahce 虛擬主機的方法, 還有那么些朋友想知道 Nginx 虛擬主機配置方法, 本文作為補充也介紹如何 Nginx 上添加虛擬主機.
絕大多數的 Nginx 運行在 Linux 機器上, 雖然有 Windows 移植版, 但我也沒搭建過. 所以本文將以 Linux 為例講解, 而 Mac OS 或其他 Unix like 機器上的操作應該是一樣的.
增加 Nginx 虛擬主機
這里假設大家的 Nginx 服務器已經安裝好, 不懂的請閱讀各 Linux 發行版的官方文檔或者 LNMP 的安裝說明. 配置 Virtual host 步驟如下:
1. 進入 /usr/local/nginx/conf/vhost 目錄, 創建虛擬主機配置文件 demo.neoease.com.conf ({域名}.conf).
2. 打開配置文件, 添加服務如下:
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
3. 打開 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http
范圍引入虛擬主機配置文件如下:
include vhost/*.conf; |
4. 重啟 Nginx 服務, 執行以下語句.
service nginx restart |
讓 Nginx 虛擬主機支持 PHP
在前面第 2 步的虛擬主機服務對應的目錄加入對 PHP 的支持, 這里使用的是 FastCGI, 修改如下.
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
圖片防盜鏈
圖片作為重要的耗流量大的靜態資源, 可能網站主並不希望其他網站直接引用, Nginx 可以通過 referer 來防止外站盜鏈圖片.
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; # 這里為圖片添加為期 1 年的過期時間, 並且禁止 Google, 百度和本站之外的網站引用圖片 location ~ .*\.(ico|jpg|jpeg|png|gif)$ { expires 1y; valid_referers none blocked demo.neoease.com *.google.com *.baidu.com; if ($invalid_referer) { return 404; } } log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
WordPress 偽靜態配置
如果將 WordPress 的鏈接結構設定為 /%postname%/
, /%postname%.html
等格式時, 需要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建議, 但沒告知 Nginx 該如何修改. 我們可以將 WordPress 的虛擬主機配置修改如下:
server { listen 80; server_name demo.neoease.com; index index.html index.htm index.php; root /var/www/demo_neoease_com; location / { if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } rewrite /wp-admin$ $scheme://$host$uri/ permanent; location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
LNMP 套件在提供了 WordPress 為靜態配置文件 /usr/local/nginx/conf/wordpress.conf, 在虛擬主機配置的 server 范圍引用如下即可.
include wordpress.conf; |
如果你使用 LNMP 套件, 進入 WordPress 后台發現會出現 404 頁面, wp-admin 后面缺少了斜桿 /
, 請在 wordpress.conf 最后添加以下語句:
rewrite /wp-admin$ $scheme://$host$uri/ permanent; |
后話
一直以來, 我主要在用 Apache, 自從去年從 MT 搬家到 Linode VPS 之后, 發現服務器壓力很大, 每隔幾天就要宕機一次, 在胡戈戈的協助下轉成了 Nginx, 大半年了一直很穩定.
相對 Apache, Nignx 有更加強大的並發能力, 而因為他對進程管理耗用資源也比較少. 而 Apache 比 Nginx 有更多更成熟的可用模塊, bug 也比較少. 賣主機的 IDC 選擇 Nignx, 因為高並發允許他們創建更多虛擬主機空間更來錢; 淘寶也因此改造 Nignx (Tengine) 作為 CDN 服務器, 可承受更大壓力.