Nginx and PHP installation and configuration eight Nginx session sharing

Source: Internet
Author: User
Tags nginx reverse proxy
This article to share the content is about Nginx and PHP installation and configuration eight of the Nginx session sharing, has a certain reference value, the need for friends can refer to

Looked up some information, read some other people's written documents, summarized as follows, to achieve Nginx Session the shared

PHP server has more than one, with Nginx do load balancing so that the same IP access to the same page will be assigned to a different server, if Session different steps, there will be many problems, such as the most common login status, the following provides several ways to solve Session Shared issues:

1 , do not use Session , swap Cookies

Sessionis stored on the server side,Cookiesis stored on the client, we can put the user access page generated by theSessionput inCookiesinside, is toCookiesas a transit point. You visitWebServerA, produced aSessionand put it inCookiesinside, when your request is assigned toBserver, the serverBfirst determine if the server has thisSession, if not, go and see the client'sCookiesIs there any of this ?Session, if not, the descriptionSessionreally does not exist, ifCookiesIf you have one inside, putCookiesinside theSessoinsync to serverB, so that you can achieveSessionin sync.

Description: This method is simple, convenient and does not increase . Database the burden, but if the client Cookies if it's forbidden, then Session There is no synchronization, this will bring a loss to the website; Cookies the security is not high, although it has been added to the secret, but still can be forged.

2 , Session exists database ( MySQL etc.) in

php Span style= "color: #362E2B;" > You can configure the session saved in the database, this method is to store the Span style= "color: #362E2B;" >session table and other database tables are put together, if mysql also made a cluster of words, each mysql nodes have this table, and this session

Description: Synchronizing with a database Session , will add Big Data Library's IO , increasing the burden on the database. and the database reading and writing speed is slow, not conducive to timely synchronization session.

3 , Session exist memcache or Redis in

memcache Can be distributed, php configuration files are stored in memcache php session cluster, session data stored in memcache > in.

session cookie greatly improved, put session in memory, it's much faster than reading from a file. But memcache The memory is divided into many types of storage blocks, there is a block size, This method is also determined, memcache The memory can not be fully exploited, resulting in memory fragmentation, If the storage block is insufficient, a memory overflow is also generated.

4 , nginx ip_hash technology enables a ip The request is directed to the same back end, so this ip session , ip_hash upstream configuration defined:

[HTML] View plain copy


1.   upstream nginx.example.com  2.       {   3.                server 192.168.74.235:80;   4.                server 192.168.74.236:80;  5.                Ip_hash;  6.       }  7.       Server  8.       {  9.                Listen;  Location               /One  .               {  proxy_pass                      . http://nginx.example.com;  .}   



Ip_hash is easy to understand, but because it can only be used IP this factor to allocate the backend, so Ip_hash is defective and cannot be used in some cases:
1.nginx is not the most front-end server.

Ip_hashRequirementsNginxmust be the most front-end server, otherwiseNginxnot getting the rightIP, it cannot be based onIPmadeHash. For example, using aSquidas the most front-end, thenNginxTakeIPyou can only getSquidthe serverIPaddress, using this address to make a diversion is certainly a disorder.
2.nginxThe backend also has other ways of load balancing.

if Nginx The backend has other load balancing, and the request is diverted in another way, then a client's request must not be located to the same Session on the application server. In this way, thenginx backend can only point directly to the application server, or another squid, then point to the application server. The best way to do this is to use the location for a diversion, to divert part of the session request through Ip_hash . , and the rest goes to the other side.

Related recommendations:

Nginx and PHP installation and configuration seven of the 5 strategies of the Nginx load balancer

Nginx and PHP installation and configuration six Nginx reverse proxy and Load Balancer Deployment Guide

Nginx and PHP installation and configuration five Linux with phpize install PHP gd extension

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.