@gy-ban
2017-12-15T07:33:37.000000Z
字数 4921
阅读 527
nginx+elasticsearch
Nginx(发音同engine x)是一个 Web服务器,也可以用作反向代理,负载平衡器和HTTP缓存,该软件由俄罗斯的Igor Sysoev创建,并于2004首次公开发布。
Nginx是一款免费的开源软件,根据类BSD许可证的条款发布。它也有企业版(收费版)
Nginx在启动后,会有一个master进程和多个worker进程。
master进程
主要用来管理worker进程,包含:接收来自外界的信号,向各worker进程发送信号,监控worker进程的运行状态,当worker进程退出后(异常情况下),会自动重新启动新的worker进程。
master进程充当整个进程组与用户的交互接口,同时对进程进行监护。它不需要处理网络事件,不负责业务的执行,只会通过管理worker进程来实现重启服务、平滑升级、更换日志文件、配置文件实时生效等功能。
我们要控制nginx,只需要通过kill向master进程发送信号就行了。
比如kill -HUP pid,则是告诉nginx,从容地重启nginx,我们一般用这个信号来重启nginx,或重新加载配置,因为是从容地重启,因此服务是不中断的。master进程在接收到HUP信号后是怎么做的呢?
首先master进程在接到信号后,会先重新加载配置文件,然后再启动新的worker进程,并向所有老的worker进程发送信号,告诉他们可以光荣退休了。
新的worker在启动后,就开始接收新的请求,而老的worker在收到来自master的信号后,就不再接收新的请求,并且在当前进程中的所有未处理完的请求处理完成后,再退出。
worker进程
而基本的网络事件,则是放在worker进程中来处理了。多个worker进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。一个请求,只可能在一个worker进程中处理,一个worker进程,不可能处理其它进程的请求。worker进程的个数是可以设置的,一般我们会设置与机器cpu核数一致
worker进程之间是平等的,每个进程,处理请求的机会也是一样的。当我们提供80端口的http服务时,一个连接请求过来,每个进程都有可能处理这个连接,怎么做到的呢?
首先,每个worker进程都是从master进程fork过来,在master进程里面,先建立好需要listen的socket(listenfd)之后,然后再fork出多个worker进程。所有worker进程的listenfd会在新连接到来时变得可读,为保证只有一个进程处理该连接,所有worker进程在注册listenfd读事件前抢accept_mutex,抢到互斥锁的那个进程注册listenfd读事件,在读事件里调用accept接受该连接。
当一个worker进程在accept这个连接之后,就开始读取请求,解析请求,处理请求,产生数据后,再返回给客户端,最后才断开连接,这样一个完整的请求就是这样的了。
我们可以看到,一个请求,完全由worker进程来处理,而且只在一个worker进程中处理。
user nobody;worker_processes 1;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"';sendfile on;keepalive_timeout 65;server {listen 80;server_name gy.es.com;location / {proxy_pass http://127.0.0.1:9200;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
user nobody;worker_processes 1;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"';sendfile on;keepalive_timeout 65;upstream es {server 192.168.2.129:9200;server 192.168.2.128:9200;keepalive 15;}server {listen 80;server_name gy.es.com;location / {proxy_pass http://es;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 Connection "Keep-Alive";proxy_set_header Proxy-Connection "Keep-Alive";}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
使用‘ngx_http_auth_basic_module’模块
user nobody;worker_processes 1;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"';sendfile on;keepalive_timeout 65;upstream es {server 192.168.2.129:9200;server 192.168.2.128:9200;keepalive 15;}server {listen 80;server_name gy.es.com;auth_basic "please log in first";auth_basic_user_file password;location / {proxy_pass http://es;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 Connection "Keep-Alive";proxy_set_header Proxy-Connection "Keep-Alive";}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
加密类型:使用crypt()函数加密,比如htpasswd、openssl passwd
对于某些关键API我们可以关闭访问,比如shutdown
if ($request_filename ~ _shutdown) {return 403;break;}
只开放特定查询接口
location ~* ^(/_cluster|/_nodes) {proxy_pass http://elasticsearch;}location / {return 403;break;}
多角色验证
server {listen 80;server_name gy.es.com;location / {return 401;}#auth_basic "please log in first";#auth_basic_user_file password;location = / {proxy_pass http://es;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 Connection "Keep-Alive";proxy_set_header Proxy-Connection "Keep-Alive";}}server {listen 81;server_name gy.es.com;auth_basic "users";auth_basic_user_file password1;location / {return 403;}location ~* ^(/_search|/_analyze) {proxy_pass http://es;}}server {listen 82;server_name gy.es.com;auth_basic "admin";auth_basic_user_file password;location / {proxy_pass http://es;}}
nginx 通过 proxy_cache_path和proxy_cache来配置缓存,proxy_cache_path用来设置缓存的路径和配置,proxy_cache用来启用缓存。
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=es_cache:10m max_size=10g inactive=60m;server {listen 80;server_name gy.es.com;#auth_basic "please log in first";#auth_basic_user_file password;location / {proxy_pass http://es;proxy_cache es_cache;proxy_cache_valid 200 302 10m;proxy_cache_valid 404 1m;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 Connection "Keep-Alive";proxy_set_header Proxy-Connection "Keep-Alive";}