作者官方网站:http://www.wxl568.cn
user mwopr;
worker_processes 1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 2048;
}
http {
include /app/nginx/conf/healcheck.conf;
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 60;
#gzip
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;
gzip_proxied any;
client_max_body_size 20M;
upstream temple {
server 10.202.1.1:8080;
server 10.202.1.1:8080;
}
upstream temple1 {
sticky;
server 10.203.244.145:12180;
server 10.203.244.146:12180;
check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
# check_http_send "HEAD /mwmonitor/check.jsp HTTP/1.0\r\n\r\n";
# check_http_expect_alive http_2xx;
}
server {
listen 8080;
server_name localhost;
access_log logs/access.log main;
location / {
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_set_header Cookie $http_cookie;
proxy_pass http://temple1;
#root html;
#index index.html index.htm;
}
location /check {
root html;
index index.html;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp3|mp4)$ {
expires 30d;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}}
但是只包含http健康检查不包含tcp下面安装nginx可以实现http及tcp健康检查
nginx会话保持之nginx-sticky-module模块
在使用负载均衡的时候会遇到会话保持的问题,常用的方法有:
1.ip hash,根据客户端的IP,将请求分配到不同的服务器上;
2.cookie,服务器给客户端下发一个cookie,具有特定cookie的请求会分配给它的发布者,
注意:cookie需要浏览器支持,且有时候会泄露数据
[name=route] 设置用来记录会话的cookie名称
[domain=.foo.bar] 设置cookie作用的域名
[path=/] 设置cookie作用的URL路径,默认根目录
[expires=1h] 设置cookie的生存期,默认不设置,浏览器关闭即失效,需要是大于1秒的值
[hash=index|md5|sha1] 设置cookie中服务器的标识是用明文还是使用md5值,默认使用md5
[no_fallback] 设置该项,当sticky的后端机器挂了以后,nginx返回502 (Bad Gateway or Proxy Error) ,而不转发到其他服务器,不建议设置
[secure] 设置启用安全的cookie,需要HTTPS支持
[httponly] 允许cookie不通过JS泄漏,没用过
作者官方网站:http://www.wxl568.cn