nginx 集群负载均衡
1.去http://www.nginx.org去下载最新的服务端;
2.查找conf/nginx.conf配置文件此处用的版本为1.5.4
#user nobody;
worker_processes 1; #这里是配置的进程数据可以配一个或多个
#error_log logs/error.log;#全局错误日志及PID文件
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式及连接数上限
events {
worker_connections 1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型 这个可以在同一目录中找到相关的文件 下面是默认类型是以stream流行式传输,
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压缩,这块的东西也可以做再tomcat或者是servlet 。filter中
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;
#代理流转向负载均衡所需要用到的ts是自己启用的名称,weight权重负载分配比
upstream ts{
server 172.18.188.64:8080 weight=10;
server 172.18.188.76:8080 weight=20;
server 172.18.188.78:8080 weight=30;
}
server {
listen 80;#监听80端口
server_name localhost;#服务器域名如 www.abc.com
#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;
}
#ts为代理服务
location /
{
proxy_pass http://ts;
}
#图片和静态网页存为30天
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
}
#js,css存一小时
location ~ .*\.(js|css)?$
{
expires 1h;
}
# 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 html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# 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 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# 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;
# }
#}
}