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

Source: Internet
Author: User
Tags nginx server nginx reverse proxy
This article to share the content is about Nginx and PHP installation and configuration six Nginx reverse proxy and Load Balancer Deployment Guide, has a certain reference value, the need for friends can refer to

1. Locate and open the Conf file


2. Load Balancing Configuration
Nginx upstream by default is a poll-based load balancing, in this way, each request in chronological order to a different back-end server, if the backend server down, can be automatically rejected.
Another way is Ip_hash: Each request is allocated according to the hash result of the access IP, so that each visitor fixed access to a back-end server can solve the session problem.
Load balancing is our big traffic site to do a thing, let me introduce you to the Nginx server load Balancing configuration method, I hope to have the necessary students help OH.

Load Balancing


Let's start with a quick look at what is load balancing, which can be explained by literally meaning. N server to load on average, not because a server is under load and a server is idle. Then the premise of load balancing is to have more than one server to achieve, that is, more than two units can be.

Test environment

Test Domain name :a.com

A Server IP : 192.168.5.149 (Master)

B Server IP : 192.168.5.27

c Server ip : 192.168.5.126

Deployment Ideas
A server as the primary server, the domain name directly resolves to a server (192.168.5.149), by a Server load balancer to B Server (192.168.5.27) and C server ( 192.168.5.126).

A Server Nginx . conf Set
Open nginx.conf , the file location is Nginx of the installation directory conf directory.

in the http segment by adding the following code

Upstream a.com {       server  192.168.5.126:80;       Server  192.168.5.27:80;}   server{     Listen;     server_name a.com;     Location/{        proxy_pass         http://a.com;         Proxy_set_header  Host            $host;         Proxy_set_header  x-real-ip        $remote _addr;         Proxy_set_header  x-forwarded-for  $proxy _add_x_forwarded_for;     }} Save restart NGINXB, C server nginx.conf settings open Nginx.confi, add the following code to the HTTP segment server{     listen;     server_name a.com;     Index index.html;     root/data0/htdocs/www; }

Save Restart Nginx

test a.com when, in order to distinguish between which server to turn to processing I was in b , c The server writes a different content index.html file to make a distinction.

Open Browser Access a.com as a result, the refresh discovers that all requests are by the primary server ( 192.168.5.149 ) is assigned to B Server ( 192.168.5.27 ) and C Server ( 192.168.5.126 ), the load balancing effect is achieved.


What if one of the servers goes down?
When a server goes down, does it affect access?

Let's take a look at the example and, based on the above example, assume C Server 192.168.5.126 the machine went down (because it couldn't simulate the outage, so I C server shut down) and then come back to see.


we found that while C Server ( 192.168.5.126 ) is down, but does not affect website access. This way, there is no fear of dragging the entire site in a load-balancing mode because of a machine outage.

if B.Com What if you want to set up load balancing?
very simple, with a.com settings. as follows:

Assumptions B.Com the primary server IP is a 192.168.5.149 , load-balanced to 192.168.5.150 and the 192.168.5.151 on the machine

now the domain name B.Com resolves to 192.168.5.149IP on.

on the primary server (192.168.5.149) of the nginx.conf Add the following code:

U

Pstream B.Com {       server  192.168.5.150:80;       Server  192.168.5.151:80;}   server{     Listen;     server_name B.Com;     Location/{         proxy_pass        http://b.com;         Proxy_set_header  Host            $host;         Proxy_set_header  x-real-ip        $remote _addr;         Proxy_set_header  x-forwarded-for  $proxy _add_x_forwarded_for;     }} Save restart Nginx set Nginx on 192.168.5.150 and 192.168.5.151 machine, open nginx.conf Add the following code at the end: server{     listen;     server_name B.Com;     Index index.html;     root/data0/htdocs/www; }


Save Restart Nginx

after you complete the next step B.Com load-balancing configuration.

Does the primary server not provide services?
In the above example, we are all applied to the primary server load balancer to other servers, then the primary server itself can also be added to the server list, so that will not be wasted to take a server purely as a forwarding function, but also participate in the provision of services.

If the above case three servers:

A Server IP : 192.168.5.149 (Master)

B Server IP : 192.168.5.27

C Server IP : 192.168.5.126

We resolve the domain name to A server, and then by A server forwards to B Server and C server, then A the server only does a forwarding function, and now we let A The server also provides site services.

let's analyze it first if we add the master server to Upstream , the following two scenarios may occur:

1 , the primary server is forwarded to the other IP on, other IP server normal processing;

2 , the master server forwards to its own IP And then go back to the primary server to assign IP there, if it has been assigned to this machine, it will cause a dead loop.

How to solve this problem? Because the port has been used to monitor the processing of load balancing, the server can no longer use the port to process a.com access requests, Have to use a new one. So we add the nginx.conf of the main server to the following code:

server{     listen 8080;     server_name a.com;     Index index.html;     root/data0/htdocs/www; }


Restart Nginx , in the browser input a.com:8080 try to see if you can access it. Results can be accessed normally

now that we have normal access, we can add the master server to the Upstream , but the port to change, the following code:

Upstream a.com {       server  192.168.5.126:80;       Server  192.168.5.27:80;       Server  127.0.0.1:8080;}

since the primary server can be added here IP192.168.5.149 or 127.0.0.1 yes, all of them represent access to themselves.

Restart Nginx , and then to access a.com See if it will be assigned to the master server.

The primary server is also able to join the service normally.

At last
One, load balancing is not Nginx unique, well-known dingding Apache also, but performance may not be as good as Nginx .

second, more than one server to provide services, but the domain name only resolves to the primary server, and the real server IP will not be Ping and add a certain amount of security.

Third, Upstream in the IP Not necessarily the intranet, outside the network IP or you can. However, the classic case is that a LAN in an IP exposure network, the domain name directly resolved to this IP. The primary server is then forwarded to the intranet server IP .

Four, a server downtime, will not affect the normal operation of the website, Nginx The request is not forwarded to the IP on

Reference article:


Http://www.php100.com/html/program/nginx/2013/0905/5525.html

http://blog.csdn.net/xyang81/article/details/51702900

Http://www.linuxdiyf.com/linux/10205.html

Http://www.cnblogs.com/jacktang/p/3669115.html

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.