Linux - Nginx的集群与负载均衡
Nginx的集群与负载均衡
集群就是一群人干同样的活,负载均衡就是保证每个人都干得差不多。或者大人干得多一些,小孩干得少一些。
Nginx实现负载均衡很方便。
准备三台服务器,一台是用于访问图片(66)。另外是两台用于提供图片服务的集群(61,62)。
先准备三个logo.png图片。
66上如下:
61上如下:
62上如下:
设置图片组66:
upstream imgserver {
server 192.168.70.61:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.70.62:80 weight=1 max_fails=2 fail_timeout=30s;
}
处理反向代理:
location ~ .*\.(jpg|jpeg|png|gif)$ {
proxy_pass http://imgserver;
proxy_set_header X-Forwarded-For $remote_addr;
}
下面是完整的配置。
#user nobody;
user nginx nginx; # 指定Nginx服务的用户和用户组
worker_processes auto;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
server_tokens off;
sendfile on;
#tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
send_timeout 30;
gzip on;
upstream imgserver {
server 192.168.70.61:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.70.62:80 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
charset UTF-8;
#charset koi8-r;
#access_log logs/host.access.log main;
set $root /var/webroot/tp5admin;
location / {
root $root;
index index.php index.html index.htm;
if ( -f $request_filename) {
break;
}
if ( !-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
location ~ .*\.(jpg|jpeg|png|gif)$ {
proxy_pass http://imgserver;
proxy_set_header X-Forwarded-For $remote_addr;
}
location ~ .*\.(js|css)$ {
proxy_pass http://192.168.70.62:80;
proxy_set_header X-Forwarded-For $remote_addr;
}
#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 /var/webroot;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php(.*)$ {
root $root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
server {
listen 81;
server_name localhost:81;
set $root /var/webroot;
location / {
root $root;
index index.php index.html index.htm;
if ( -f $request_filename) {
break;
}
if ( !-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/webroot;
}
location ~ .+\.php($|/) {
root $root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
include fastcgi_params;
}
}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}