一、
1、將tomcat 的server.xml文件中所有端口號都改為不同。
2、Nginx 的nginx.conf文件中
http {}增加如下內容
upstream sp.imichat.com{
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:8088 weight=2;
ip_hash;
}
server {} 修改信息:
listen 80;
server_name sp.imichat.com;
#charset koi8-r;
#access_log logs/host_access_log main;
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://sp.imichat.com;
}
例:
#user nobody;
worker_processes 1;
events {
worker_connections 51024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream localhost {
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:8088 weight=2;
ip_hash;
}
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
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://localhost;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}}
二、
參數描述
檢測nginx配置文件是否正確
/usr/local/nginx/sbin/nginx -t -c nginx.conf
-c 配置文件路徑
-g Set global directives. (version >=0.7.4)
-t 檢測文件是否正確不執行
-v Print version.
-V Print nginx version, compiler version and configure parameters.
編譯時如果使用了–with-debug編譯,還可以使用error_log file [ debug_core| debug_http | debug_event …] 來獲得debug信息
通過信號對 Nginx 進行控制
Nginx 支持下表中的信號:
信號名 作用描述
TERM, INT 快速關閉程序,中止當前正在處理的請求
QUIT 處理完當前請求后,關閉程序
HUP 重新加載配置,并開啟新的工作進程,關閉就的進程,此操作不會中斷請求
USR1 重新打開日志文件,用于切換日志,例如每天生成一個新的日志文件
USR2 平滑升級可執行程序
WINCH 從容關閉工作進程
有兩種方式來通過這些信號去控制 Nginx,第一是通過 logs 目錄下的 nginx.pid 查看當前運行的 Nginx 的進程 ID,通過 kill – XXX <pid> 來控制 Nginx,其中 XXX 就是上表中列出的信號名。如果您的系統中只有一個 Nginx 進程,那您也可以通過 killall 命令來完成,例如運行 killall – s HUP nginx 來讓 Nginx 重新加載配置。
配置:
use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];FreeBSD使用kqueue,Linux選epoll.
worker_connections number 每個worker的最大連接數
Maxclient = work_processes * worker_connections
nginx的upstream目前支持4種方式的分配
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
2、weight
指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的情況。
2、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
3、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
4、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
代理
只需要在nginx的配置文件中增加虛擬主機,然后加入
\proxy_pass http://localhost:8000;
負載均衡:
只需要在http中增加
upstream tgcluster {#定義負載均衡設備的Ip及設備狀態
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用負載均衡的server中增加
proxy_pass http://tgcluster/;
每個設備的狀態設置為:
1.down 表示單前的server暫時不參與負載
2.weight 默認為1.weight越大,負載的權重就越大。
3.max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗后,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
client_body_in_file_only 設置為On 可以講client post過來的數據記錄到文件中用來做debug
client_body_temp_path 設置記錄文件的目錄 可以設置最多3層目錄
location 對URL進行匹配.可以進行重定向或者進行新的代理 負載均衡
FASTCGI配置:
請將以下內容保存為fastcgi_params文件,保存于/usr/local/nginx/conf下(Ubuntu可保存于/etc/nginx下),他為我們的FastCGI模塊設置了基本的環境變量:
#fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;請特別注意加粗的一行,PHP-CGI特別需要此行信息來確定PHP文件的位置。
另外需要在PHP-CGI的配置文件(Ubuntu 上此配置文件位于/etc/php5/cgi/php.ini)中,打開cgi.fix_pathinfo選項:
cgi.fix_pathinfo=1;這樣php-cgi方能正常使用SCRIPT_FILENAME這個變量。
接下來在nginx的配置中針對php文件配置其利用FastCGI進程來執行:
server {
index index.php;
root /usr/local/nginx/html;
location ~ .*.php$ {
include /usr/local/nginx/conf/fastcgi_params; #請根據自己保存的路徑進行設置
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000; #請根據自己的FastCGI綁定的地址和端口進行配置
}
}通知Nginx重新載入配置:
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`Ubuntu用戶可以使用init腳本:sudo /etc/init.d/nginx reload
然后啟動php-cgi -b 127.0.0.1:9000
如果出現No input file specified表示SCRIPT_FILENAME設置的有問題。
使用lighttpd的 spawn-fcgi
get http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #獲取Lighttpd的源碼包
tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure #編譯
make
cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #取出spawn-fcgi的程序下面我們就可以使用 spawn-fcgi 來控制php-cgi的FastCGI進程了
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi參數含義如下
-f <fcgiapp> 指定調用FastCGI的進程的執行程序位置,根據系統上所裝的PHP的情況具體設置
-a <addr> 綁定到地址addr
-p <port> 綁定到端口port
-s <path> 綁定到unix socket的路徑path
-C <childs> 指定產生的FastCGI的進程數,默認為5(僅用于PHP)
-P <path> 指定產生的進程的PID文件路徑
-u和-g FastCGI使用什么身份(-u 用戶 -g 用戶組)運行,Ubuntu下可以使用www-data,其他的根據情況配置,如nobody、apache等
#運行用戶
user nobody nobody;
#啟動進程
worker_processes 2;
#全局錯誤日志及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#工作模式及連接數上限
events {
use epoll;
worker_connections 1024;
}
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型
include conf/mime.types;
default_type application/octet-stream;
#設定日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$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;
#開啟gzip模塊
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#設定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 65;
#設定負載均衡的服務器列表
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的幾率越大
#本機上的Squid開啟3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
#設定虛擬主機
server {
listen 80;
server_name 192.168.8.1
www.yejr.com;
charset gb2312;
#設定本虛擬主機的訪問日志
access_log logs/www.yejr.com.access.log main;
#如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不通過squid
#如果這些文件較多,不推薦這種方式,因為通過squid的緩存效果更好
location ~ ^/(img|js|css)/ {
root /data3/Html;
expires 24h;
}