10.130.130.84
upstream.ntest.conf
upstream ntest {
# #ip_hash;
server 10.130.130.84:1818;
server 10.130.130.89:1818;
}
server {
#set $host_path "/data/www/ntest";
#access_log /www/mysite/log/access.log main;
listen 80;
server_name ntest.happyelements.net;
#root /data/www/ntest;
#set $yii_bootstrap "index.php";
#error_log /tmp/nginx/error.log debug;
#charset utf-8;
#proxy_pass http://ntest;
location / {
#index $yii_bootstrap;
#try_files $uri $uri/ /$yii_bootstrap?$args;
proxy_pass http://ntest;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
ntest.conf
server {
#set $host_path "/data/www/ntest";
#access_log /www/mysite/log/access.log main;
listen 1818;
server_name ntest.happyelements.net;
root /data/www/ntest;
set $yii_bootstrap "index.php";
error_log /tmp/nginx/error.log debug;
charset utf-8;
#proxy_pass http://ntest;
location / {
index $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args;
#proxy_pass http://ntest;
}
#location ~ ^/(protected|framework|themes/\w+/views) {
# deny all;
#}
#avoid processing of calls to unexisting static files by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $uri =404;
#}
#location ~* \.(txt|doc|docx|zip|rar|xlsx|xls)$ {
# root /data/www/ntest/upload;
# deny all;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;
#let yii catch the calls to unexising PHP files
set $fsn /$yii_bootstrap;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
#fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;
#PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
#fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
}
# prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
10.130.130.89
ntest.conf
server {
#set $host_path "/data/www/ntest";
#access_log /www/mysite/log/access.log main;
listen 1818;
server_name ntest.happyelements.net;
root /data/www/ntest;
set $yii_bootstrap "index.php";
#error_log /tmp/nginx/error.log debug;
charset utf-8;
#proxy_pass http://ntest;
location / {
index $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args;
#proxy_pass http://ntest;
}
#location ~ ^/(protected|framework|themes/\w+/views) {
# deny all;
#}
#avoid processing of calls to unexisting static files by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $uri =404;
#}
#location ~* \.(txt|doc|docx|zip|rar|xlsx|xls)$ {
# root /data/www/ntest/upload;
# deny all;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;
#let yii catch the calls to unexising PHP files
set $fsn /$yii_bootstrap;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
#fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;
#PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
#fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
}
# prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
Nginx来做负载设备,记录下upstream的几种配置方式。
第一种:轮询
upstream test{
server 192.168.0.1:3000;
server 192.168.0.1:3001;
}
第二种:权重
upstream test{
server 192.168.0.1 weight=2;
server 192.168.0.2 weight=3;
}
这种模式可解决服务器性能不等的情况下轮询比率的调配
第三种:ip_hash
upstream test{
ip_hash;
server 192.168.0.1;
server 192.168.0.2;
}
这种模式会根据来源IP和后端配置来做hash分配,确保固定IP只访问一个后端
第四种:fair
需要安装Upstream Fair Balancer Module
upstream test{
server 192.168.0.1;
server 192.168.0.2;
fair;
}
这种模式会根据后端服务的响应时间来分配,响应时间短的后端优先分配
第五种:自定义hash
需要安装Upstream Hash Module
upstream test{
server 192.168.0.1;
server 192.168.0.2;
hash $request_uri;
}
这种模式可以根据给定的字符串进行Hash分配
具体应用:
server{
listen 80;
server_name .test.com;
charset utf-8;
location / {
proxy_pass http://test/;
}
}
此外upstream每个后端的可设置参数为:
1.down: 表示此台server暂时不参与负载。
2.weight: 默认为1,weight越大,负载的权重就越大。
3.max_fails: 允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误。
4.fail_timeout: max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器,应急措施。