1. 编译

  1. # 依赖库 (编译需要)
  2. yum install -y pcre-devel openssl-devel
  3. # 下载代码
  4. git clone git@192.168.128.110:chromium/tengine.git tengine-2.3.3
  5. git clone https://192.168.128.110/chromium/tengine.git tengine-2.3.3
  6. # 生成makefile
  7. cd tengine-2.3.3
  8. ./configure --sbin-path=/usr/sbin/nginx \
  9. --user=nginx --group=nginx --with-compat --with-file-aio --with-threads \
  10. --with-http_addition_module --with-http_auth_request_module \
  11. --with-http_dav_module --with-http_flv_module --with-http_gunzip_module \
  12. --with-http_gzip_static_module --with-http_mp4_module \
  13. --with-http_random_index_module --with-http_realip_module \
  14. --with-http_secure_link_module --with-http_slice_module \
  15. --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module \
  16. --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream \
  17. --with-stream_realip_module --with-stream_ssl_module \
  18. --with-stream_ssl_preread_module \
  19. --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
  20. --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' \
  21. --add-module=./modules/ngx_http_upstream_consistent_hash_module \
  22. --add-module=./modules/ngx_http_upstream_session_sticky_module
  23. # 输出
  24. Configuration summary
  25. + using threads
  26. + using system PCRE library
  27. + using system OpenSSL library
  28. + using system zlib library
  29. + jemalloc library is disabled
  30. nginx path prefix: "/usr/local/nginx"
  31. nginx binary file: "/usr/local/nginx/sbin/nginx"
  32. nginx modules path: "/usr/local/nginx/modules"
  33. nginx configuration prefix: "/usr/local/nginx/conf"
  34. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  35. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  36. nginx error log file: "/usr/local/nginx/logs/error.log"
  37. nginx http access log file: "/usr/local/nginx/logs/access.log"
  38. nginx http client request body temporary files: "client_body_temp"
  39. nginx http proxy temporary files: "proxy_temp"
  40. nginx http fastcgi temporary files: "fastcgi_temp"
  41. nginx http uwsgi temporary files: "uwsgi_temp"
  42. nginx http scgi temporary files: "scgi_temp"
  43. # 编译
  44. make -j4
  45. # 安装
  46. make install

2. 配置

2.1 session_sticky

语法:session_sticky [cookie=name] [domain=your_domain] [path=your_path] [maxage=time] [mode=insert|rewrite|prefix] [option=indirect] [maxidle=time] [maxlife=time] [fallback=on|off] [hash=plain|md5]
默认值:session_sticky cookie=route mode=insert fallback=on
上下文:upstream

说明:
本指令可以打开会话保持的功能,下面是具体的参数:

  • cookie设置用来记录会话的cookie名称
  • domain设置cookie作用的域名,默认不设置
  • path设置cookie作用的URL路径,默认不设置
  • maxage设置cookie的生存期,默认不设置,即为session cookie,浏览器关闭即失效
  • mode设置cookie的模式:
    • insert: 在回复中本模块通过Set-Cookie头直接插入相应名称的cookie。
    • prefix: 不会生成新的cookie,但会在响应的cookie值前面加上特定的前缀,当浏览器带着这个有特定标识的cookie再次请求时,模块在传给后端服务前先删除加入的前缀,后端服务拿到的还是原来的cookie值,这些动作对后端透明。如:”Cookie: NAME=SRV~VALUE”。
    • rewrite: 使用服务端标识覆盖后端设置的用于session sticky的cookie。如果后端服务在响应头中没有设置该cookie,则认为该请求不需要进行session sticky,使用这种模式,后端服务可以控制哪些请求需要sesstion sticky,哪些请求不需要。
  • option 设置用于session sticky的cookie的选项,可设置成indirect或direct。indirect不会将session sticky的cookie传送给后端服务,该cookie对后端应用完全透明。direct则与indirect相反。
  • maxidle设置session cookie的最长空闲的超时时间
  • maxlife设置session cookie的最长生存期
  • fallback设置是否重试其他机器,当sticky的后端机器挂了以后,是否需要尝试其他机器
  • hash 设置cookie中server标识是用明文还是使用md5值,默认使用md5

语法: session_sticky_hide_cookie upstream=name;
默认值: none
上下文: server, location

说明:
配合proxy_pass指令使用。用于在insert+indirect模式和prefix模式下删除请求用于session sticky的cookie,这样就不会将该cookie传递给后端服务。upstream表示需要进行操作的upstream名称。

  • http cookie传入后端 ```c

    默认配置:cookie=route mode=insert fallback=on

    upstream foo { server 192.168.0.1; server 192.168.0.2; session_sticky; }

server { location / { proxy_pass http://foo; } }

  1. - http cookie不传入后端
  2. ```c
  3. #insert + indirect模式:
  4. upstream test {
  5. session_sticky cookie=uid domain=www.xxx.com fallback=on path=/ mode=insert option=indirect;
  6. server 127.0.0.1:8080;
  7. }
  8. server {
  9. location / {
  10. #在insert + indirect模式或者prefix模式下需要配置session_sticky_hide_cookie
  11. #这种模式不会将保持会话使用的cookie传给后端服务,让保持会话的cookie对后端透明
  12. session_sticky_hide_cookie upstream=test;
  13. proxy_pass http://test;
  14. }
  15. }

参考文档