Nginx load Balancing lamp and PHP session saving based on memcached

Source: Internet
Author: User
Tags php session nginx reverse proxy nginx load balancing

Logical architecture Diagram:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/2A/wKiom1c5quXyxNSdAACmHYjI8Ps319.png "title=" 981G] Z8$IWWMW (_v6g9a~1x.png "alt=" Wkiom1c5quxyxnsdaacmhyji8ps319.png "/>

Experiment Preparation:

Virtual machine: 172.18.250.75 nginx keepalived High-availability

Virtual machine: 172.18.250.76 nginx keepalived High-availability

Virtual machine: 172.18.250.77 LAMP memcached Service

Virtual machine: 172.18.250.78 LAMP sharing memcached

Experimental requirements: Two Nginx reverse proxy service to achieve load balancing, and achieve high availability, two back-end servers to provide Web services, PHP session saved in memcached, to achieve session sharing.

one: Nginx high availability

]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm      two rear ends are installed nginxwarning:  nginx-1.8.0-1.el6.ngx.x86_64.rpm: header v4 rsa/sha1 signature, key id  7bd9bf62: NOKEYPreparing...                 ########################################### [100%]   1:nginx                   ######## ################################### [100%]--------------------------------------------------------------- -------thanks for using nginx! please find the official documentation for nginx here:* http:// nginx.org/en/docs/commercial subscriptions for nginx are available on:*  http://nginx.com/products/----------------------------------------------------------------------]# yum -y install keepalived               two rear ends are installed keepalived

edit Keepalived Profile: Implement dual-master model

vrrp_script chk_nginx_down {     script  "Killall -0 nginx"      weight -5     interval 2}vrrp_instance  vi_1 {    state master    interface eth0     virtual_router_id 151    priority 100    advert_ Int 1    authentication {        auth_ type pass        auth_pass 1111    }     virtual_ipaddress {        172.18.250.99  dev eth0 label eth0:0    }     track_script  {        ck_down         Chk_nginx_down    }}vrrp_instance vi_2 {    state backup     interface eth0    virtual_router_id 152    priority  98    advert_int 1    authentication {         auth_type pass        auth_ pass 2222    }    virtual_ipaddress {         172.18.250.100 dev eth0 label eth0:1     }    track_script {     ck_down      chk_nginx_down    }}

start keepalived to see if you implement dual main mode

]# ifconfigeth0      link encap:ethernet  hwaddr 00:0c : 29:c5:a4:6b            inet addr :172.18.250.75  bcast:172.18.255.255  mask:255.255.0.0           inet6 addr: fe80::20c:29ff:fec5:a46b/64 Scope:Link           up broadcast running multicast  mtu:1500   Metric:1          RX packets:199576  errors:0 dropped:0 overruns:0 frame:0           TX packets:15786 errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1000            rx bytes:23440249  (22.3 MIB)   TX bytes:3193410  (3.0 MIB) eth0:0     Link encap:Ethernet  HWaddr 00:0C:29:C5:A4:6B             inet addr:172.18.250.99  Bcast:0.0.0.0   Mask:255.255.255.255          up broadcast running  MULTICAST  MTU:1500  Metric:1           ]# ifconfigeth0      Link encap:Ethernet  HWaddr  00:0c:29:f6:9e:df            inet addr :172.18.250.76  bcast:172.18.255.255  mask:255.255.0.0           inet6 addr: fe80::20c:29ff:fef6:9edf/64 Scope:Link           up broadcast running multicast  mtu:1500  metric:1           RX packets:5448 errors:0 dropped:0  overruns:0 frame:0          tx packets:202  errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1000           rx  bytes:670054  (654.3 kib)   TX bytes:19959  (19.4 kib) eth0:1     Link encap:Ethernet  HWaddr 00:0C:29:F6:9E:DF             inet addr:172.18.250.100  Bcast:0.0.0.0   Mask:255.255.255.255          up broadcast running  multicast  mtu:1500  metric:1 

Second, configure Nginx, to achieve load balancing

]# vim/etc/nginx/nginx.confupstream webserver {server 172.18.250.77:80 weight=1;    Server 172.18.250.78:80 weight=1;        }location/{Proxy_pass http://webserver; Index index.html index.htm; }

Start Nginx Service


Third, configure the back-end server

]# yum-y Install httpd php php-mysql Edit test page:]# echo "Hello RS1" >index.html]# echo "Hello RS2" >index.html

To start the HTTPD service, test the following home page:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/80/2C/wKiom1c5wTyBUGNVAAAm1-uU4IE652.png "title=" 1{n]b] m6$k) ln@s@h3wv32k.png "alt=" Wkiom1c5wtybugnvaaam1-uu4ie652.png "/>

650) this.width=650, "src=" Http://s5.51cto.com/wyfs02/M01/80/29/wKioL1c5wjTAfQ5cAAAqqXYo1Pg151.png "title=")) Q (K8 }W26A_D6GEQ4) [cua.png "alt=" Wkiol1c5wjtafq5caaaqqxyo1pg151.png "/>

Test whether load balancing:

]# Curl Hello rs2]# Curl Http://172.18.250.99Hello rs1]# Curl Http://172.18.250.100Hello rs2]# Curl http://172.18.250 .100Hello RS1

Test if you can request a PHP test page:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/29/wKioL1c5xEDjDs-xAABueguJQ6I264.png "title=" 2F) 9] Efkja9$7b6ej]hey$r.png "width=" 673 "height=" 195 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:673px;height:195px; " alt= "Wkiol1c5xedjds-xaabuegujq6i264.png"/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/29/wKioL1c5xGWCZptOAAJcLDUSTMA935.jpg "title=" K19NSQG (c3~rqr~nrgo0f8l.jpg "width=" 675 "height=" 198 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:675px;height : 198px; "alt=" wkiol1c5xgwczptoaajcldustma935.jpg "/>

Four: Configure PHP applications

]# mount-t NFS 172.18.250.77:/var/www/html//var/www/html/250.78 Mount 250.77 NFS, for data sharing]# cd/var/www/html]#]# unzip PHP wind_utf8_8.7]# yum-y install MySQL on installation mariadb-server/250.78, two back-end servers share a MySQL

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/29/wKioL1c5xxOj9Db6AAI9TQ_EML4085.png "title=" E) @[}] AW ' 5grv5iwu47n0~0.png "width=" 620 "height=" 395 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:620px;height:395px; " alt= "Wkiol1c5xxoj9db6aai9tq_eml4085.png"/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/2C/wKiom1c5x1CTxQC2AABYM31BFKo297.png "title=" xeg6{j %w~9xqvij}1v$ ' 6pj.png "alt=" Wkiom1c5x1ctxqc2aabym31bfko297.png "/>

Refresh page:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/2C/wKiom1c5yf2wBr3fAABpR2O7H7Y210.png "title=" C $) 1b@i9lrgr3s5qnrt$0mr.png "width=" 634 "height=" 463 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:634PX;HEIGHT:463PX; " alt= "Wkiom1c5yf2wbr3faabpr2o7h7y210.png"/>

No pictures ...

Edit the Nginx configuration file on the reverse proxy:

//add the following four statements to the location:proxy_next_upstream error timeout  invalid_header http_500 http_502 http_504;      //If the back end is 500, 502,504 Status Code error, dispatch to the next back-end host proxy_set_header host  $host;       //Set host hosts, Because Nginx is used as a reverse proxy, and if the backend server is set up with a similar anti-theft chain or according to the Host field in the HTTP request header for routing or judging functions, if nginx of the reverse proxy layer does not rewrite the host field in the request header, it will cause the request to fail.                                                           proxy_set_header X-Real-IP  $remote _addr;      //Backend Gets the ipproxy_ of the client set_header x-forwarded-for  $proxy _add_x_forwarded_for;      // Add the proxy server IP and client IP to the backend 

Refresh here: Show ok

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/3A/wKioL1c75nmA2UKxAAG1l65AY7Q999.png "title=" capture. PNG "width=" 579 "height=" 225 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:579px;height:225px; "alt=" Wkiol1c75nma2ukxaag1l65ay7q999.png "/>


V: Configure the memcached cache

]# yum-y install memcached php-pecl-memcached]# service memcached start

Configure PHP to save the session to Memcached, edit the php.ini file, and make sure that the values of the following two parameters are as follows:

Session.save_handler = Memcachesession.save_path = "tcp://172.16.200.11:11211?persistent=1&weight=1& Timeout=1&retry_interval=15 "

Restart the Apache service to see if the memcached has been loaded successfully:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/43/wKiom1c8KLzD4KE2AABbH0OGFdY694.png "title=" capture. PNG "alt=" Wkiom1c8klzd4ke2aabbh0ogfdy694.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/40/wKioL1c8KdvQr-4fAAAUEIunMJ4245.png "title=" Capture (2). PNG "alt=" Wkiol1c8kdvqr-4faaaueiunmj4245.png "/>


Then refresh it several times to see if the session is written in memcached:

]# memcached-tool 127.0.0.1:11211 # item_size max_age Pages Count full? Evicted Evict_time OOM 1 96B 1009s 1 1 Yes 0 0 0

Nginx reverse Proxy, PHP application may appear garbled, just add charset utf-8 in the Nginx configuration is OK.


Six: To open the cache for Nginx reverse proxy front end, reduce the pressure on the back-end host:

Proxy_cache_path/var/cache/nginx/proxy levels=2:1 keys_zone=pxcache:10m;         Define Proxy_cache Pxcache in HTTP; Defined in location Proxy_cache_key $request _uri;proxy_cache_valid 301 302 10m;proxy_cache_valid 403 404 5m;

Reboot, refresh, see if the cache is generated:

]# Ls00 0f 2c, 3c, 4c, 5e, 7b, 7f 8b, A6, ab b4 bb c7 f5 fc02, 8e, 1e 3a 4d, 5f, 7c, 8c 8f, A2 A9 ad b7 C6 E2 F9 fd]# Pwd/var/cache/nginx/proxy

Nginx load Balancing lamp and PHP session saving based on memcached

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.