Several methods for nginx server load balancer to process session sharing

Source: Internet
Author: User
: This article mainly introduces several methods for nginx server load balancer to process session sharing. For more information about PHP tutorials, see. 1) use cookie instead of session

By changing the session into a cookie, you can avoid some drawbacks of the session. in a previously read J2EE book, it also indicates that the session cannot be used in the cluster system, otherwise, it will be difficult to solve the problem. If the system is not complex, consider removing the session first. if the modification is very troublesome, use the following method.

2) the application server shares the data on its own.

It is known that php can use a database or memcached to store sessions, so that a session cluster is established in php itself. in this way, session stability can be ensured even if a node fails, the session will not be lost, which is suitable for scenarios with strict but low request volumes. However, the efficiency is not very high, and it is not applicable to scenarios with high efficiency requirements.

The above two methods have nothing to do with nginx. The following describes how to deal with nginx:

3) ip_hash

Ip_hash technology in nginx can direct requests from an ip address to the same backend, so that a client and a backend under this ip address can establish a stable session, ip_hash is defined in upstream configuration:

Upstream backend {
Server 127.0.0.1: 8001;
Server 127.0.0.1: 8002;
Ip_hash;
}

Ip_hash is easy to understand. However, ip_hash is flawed because it can only be used to allocate backend ip addresses. in some cases, ip_hash cannot be used:

1/nginx is not the frontend server. Ip_hash requires nginx to be the frontend server. Otherwise, if nginx fails to obtain the correct ip address, it cannot be used as a hash based on the ip address. For example, if squid is used as the frontend, only the squid server ip address can be obtained when nginx obtains the ip address. it is certainly confusing to use this address for traffic distribution.

2. there are other load balancing methods at the nginx backend. If the nginx backend has another server load balancer and requests are diverted in another way, the requests of a client cannot be located on the same session application server. In this case, the nginx backend can only direct to the application server, or create another squid and then point to the application server. The best way is to use location for one-time traffic distribution. part of the requests that require session are distributed through ip_hash, and the rest are distributed through other backend servers.

4) upstream_hash

To solve ip_hash problems, you can use upstream_hash, a third-party module, which is generally used as url_hash, but does not prevent it from being used for session sharing:

If the front-end is squid, it will add the ip address to the x_forwarded_for http_header. with upstream_hash, this header can be used as a factor to direct the request to the specified backend:

See this document:

Http://www.oschina.net/discuss/thread/622

In this document, $ request_uri is used as a factor. change it a bit:

Hash $ http_x_forwarded_for;

In this way, the header x_forwarded_for is used as a factor. in the new nginx version, the cookie value can be read, so you can also change it:

Hash $ cookie_jsessionid;

If the session configured in php is non-cookie-free, you can use nginx to generate a cookie with a userid_module module of nginx. For more information, see the userid module's English documentation:

Http://wiki.nginx.org/NginxHttpUserIdModule

The above describes several methods for nginx server load balancer to process session sharing, including some content. I hope my friends who are interested in PHP tutorials can help me.

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.