Several ways to implement session pasting in Nginx load balancer server

Source: Internet
Author: User

1. Use Nginx Ip_hash as Load Balancer Service and support session sticky

2, using nginx sticky third-party module to achieve cookie-based load balancing

3, using the Nginx map instruction according to the cookie shunt:

Map $COOKIE _abcdexpid $group {  ~*1$apache001;  ~*2$apache002;  Defaultroot;} Upstream apache001 {  server 192.168.1.1:8080 weight=1 Max_fails=1 fail_timeout=30s;} upstream apache002 {  Server 192.168.1.2:8080 weight=1 Max_fails=1 fail_timeout=30s;} Upstream root {  server 192.168.1.0:8080 weight=1 max_fails=1 fail_timeout=30s;} server {  listen       8080;  server_name  neoremind.net;   Log_format  main  ' $remote _addr-$remote _user [$time _local] "$request" "            $status $body _bytes_sent" $ Http_referer "group= $group" "            $http _user_agent" $gzip _ratio $request _time "$http _x_forwarded_for";   Access_log  Logs/access_log main;  Error_log   Logs/error_log;   Location/{        Proxy_pass/HTTP $group;    Proxy_set_header x-forwarded-for $remote _addr;    }}

4. Using Set and If...else ... Based on Cookie diversion

Upstream apache001 {  server 192.168.1.1:8080 weight=1 Max_fails=1 fail_timeout=30s;} upstream apache002 {  Server 192.168.1.2:8080 weight=1 Max_fails=1 fail_timeout=30s;} Upstream root {  server 192.168.1.0:8080 weight=1 max_fails=1 fail_timeout=30s;} server {  listen       8080;  server_name  beidoutest.baidu.com;   #match Cookie  Set $group "root";  if ($http _cookie ~* "abcdexpid= ([^;] +) (1$) {    set $group apache001;  }  if ($http _cookie ~* "abcdexpid= ([^;] +) (2$) {    set $group apache002;  }   Log_format  main  ' $remote _addr-$remote _user [$time _local] "$request" "            $status $body _bytes_sent" $ Http_referer "group= $group" "            $http _user_agent" $gzip _ratio $request _time "$http _x_forwarded_for";   Access_log  Logs/access_log main;  Error_log   Logs/error_log;   Location/{    Proxy_pass/HTTP $group;    Proxy_set_header x-forwarded-for $remote _addr;  } }

5, the nginx1.7.2 version after the hash method provided:

# HTTP Context  upstream backend_hosts {      hash $cookie _jsessionid consistent;      Server host1.example.com;     Server host2.example.com;     

}

    • This article is from: Linux Learning Network

Several ways to implement session pasting in Nginx load balancer server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.