ES-nginx 长连接和权限配置

2017-11-10  本文已影响0人  YG_9013

长连接配置

events {
    worker_connections  1024;
}

http {
   keepalive_timeout  120s 120s;
   keepalive_requests 10000;

  upstream elasticsearch {
    server 127.0.0.1:9200;

    keepalive 15;
  }

  server {
    listen 8080;

    location / {
      proxy_pass http://elasticsearch;
      proxy_http_version 1.1;
      proxy_set_header Connection "Keep-Alive";
      proxy_set_header Proxy-Connection "Keep-Alive";
    }

  }

}

当你直接向es发送请求,你可以看到连接数是增加的:

$ curl 'localhost:9200/_nodes/stats/http?pretty' | grep total_opened
# "total_opened" : 13
$ curl 'localhost:9200/_nodes/stats/http?pretty' | grep total_opened
# "total_opened" : 14
# ...

通过nginx访问,结果如下:

$ curl 'localhost:8080/_nodes/stats/http?pretty' | grep total_opened
# "total_opened" : 15
$ curl 'localhost:9200/_nodes/stats/http?pretty' | grep total_opened
# "total_opened" : 15
# ...

出现大量TIME_WAIT的情况:
1. keepalive_requests设置比较小,高并发下超过此值后nginx会强制关闭和客户端保持的keepalive长连接;(主动关闭连接后导致nginx出现TIME_WAIT)
2. keepalive设置的比较小(空闲数太小),导致高并发下nginx会频繁出现连接数震荡(超过该值会关闭连接),不停的关闭、开启和后端server保持的keepalive长连接;

授权认证

events {
  worker_connections  1024;
}

http {

  upstream elasticsearch {
    server 127.0.0.1:9200;
  }

  server {
    listen 8080;

    auth_basic "Protected Elasticsearch";
    auth_basic_user_file passwords;

    location / {
      proxy_pass http://elasticsearch;
      proxy_redirect off;
    }
  }

}

通过openssl生成用户名密码:

printf "john:$(openssl passwd -crypt s3cr3t)n" > passwords

通过不通过密码访问:

$ curl -i localhost:8080
# HTTP/1.1 401 Unauthorized
# ...

通过密码访问:

$ curl -i john:s3cr3t@localhost:8080
# HTTP/1.1 200 OK
# ...

限制某些命令:

location / {
  if ($request_filename ~ _shutdown) {
    return 403;
    break;
  }

  proxy_pass http://elasticsearch;
  proxy_redirect off;
}
上一篇下一篇

猜你喜欢

热点阅读