Home  >  Article  >  Backend Development  >  Centos+nginx configures load balancing from scratch, centosnginx_PHP tutorial

Centos+nginx configures load balancing from scratch, centosnginx_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:15958browse

centos+nginx configure load balancing from scratch, centosnginx

Understanding of nginx load balancing

nginx is a lightweight, high-performance webserver. It can mainly do the following two things:

  • As an http server (same effect as apache)
  • Achieve load balancing as a reverse proxy server

Nowadays, nginx can be seen everywhere. It is often seen that the words nginx will be displayed on the webpage after a downtime. This also shows that nginx is used by more and more users due to its high performance, simple use and configuration, and open source. accepted, used.

The first one acts as an http server, combined with the php-fpm process, to process incoming requests. nginx itself does not parse php. It just acts as a server to accept requests from the client. If it is php The request is handed over to the PHP process for processing, and the result after PHP processing is completed is sent to the client. This is very simple. After installing nginx+php-fpm, configure the respective configuration files and start it. The operating principle can be explained in the following paragraph:

<p>Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。这就是Nginx+FastCGI的整个运作过程,如图下图所示。</p>

Centos+nginx configures load balancing from scratch, centosnginx_PHP tutorialCentos+nginx configures load balancing from scratch, centosnginx_PHP tutorialReverse proxy is the opposite of forward proxy (or proxy). You must have heard of proxy. In order to access B resources more conveniently, B resources are indirectly accessed through A resources. The characteristics are Users know what website they want to visit eventually, but reverse proxy users do not know what processing is done behind the proxy server. The real processing server of services in reverse proxy is placed on the intranet, and only the reverse proxy can be accessed by the external network. To the proxy server, this also greatly improves security.

Install software

nginx installation is very simple

1. The environment required to install nginx, pcre (for rewrite), zlib (for compression), ssl, you can also download, compile and install this yourself

<p>yum -y install zlib;</p>
<p>yum &ndash;y install pcre;</p>
<p>yum &ndash;y install openssl;</p>

2. Download and install nginx-*.tar.gz.

<p><span>tar &ndash;zxvf nginx-1.2.8.tar.gz &ndash;C ./;</span></p>
<p><span>cd nginx-1.2.8;</span></p>
<p><span>./congigure --prefix=/usr/local/nginx;</span></p>
<p><span>make && make install;</span></p>

3. Configuration

<p><span>这里配置的时候只需要修改http{}之间的内容就行了,修改的第一个地方就是设置服务器组,在http节点之间添加</span></p>
<p>upstream myServer{<br />        server www.myapp2.com:80;  #这里是你自己要做负载均衡的服务器地址1<br />        server www.myapp1.com:8080; #这里是要参与负载均衡的地址2<br />    }</p>
<p>nginx中的upstream支持下面几种方式:轮询(默认,按照时间顺序对所有服务器一个一个的访问,如果有服务器宕机,会自动剔除)、weight(服务器的方位几率和weight成正比,这个可以在服务器配置不均的时候进行配置)、ip_hash(对每个请求的ip进行hash计算,并按照一定的规则分配对应的服务器)、fair(按照每台服务器的响应时间(rt)来分配请求,rt晓得优先分配)、url_hash(按照访问url的hash值来分配请求),我这里使用了默认的轮训方式。</p>
<p>将请求指向myServer</p>
<p>location / {<br />         proxy_pass   http://myServer;<br />     }</p>
<p>完整的文件(删除注释)如下:</p>
<p> </p>

<pre class="code">worker_processes  <span>1</span><span>;
events {
    worker_connections  </span><span>1024</span><span>;
}
http {
    include       mime.types;
    default_type  application</span>/octet-<span>stream;
    sendfile        on;
    keepalive_timeout  </span><span>65</span><span>;
    upstream myServer{
       server www.myapp1.com:</span><span>80</span><span>;
       server www.myapp2.com:</span><span>8080</span><span>;
    }
    server {
        listen       </span><span>80</span><span>;
        server_name  my22;
        location </span>/<span> {
            proxy_pass   http:</span><span>//</span><span>myServer;</span>
<span>        }
    }
}</span>

Set up a reverse proxy backend as a load balancer between the two servers

You can see that there are two server addresses in the previous step, www.myapp1.com:80 and www.myapp2.com:8080. I installed the above nginx on the virtual machine. I installed these two servers. In the local win8 system, using apache's virtualhost, two domain names are set up. The codes under the two domain names are independent of each other, and the settings are also very simple:

1. Set apache configuration file

<p><span>我使用的是xampp集成环境,要修改的地方有两个,在httpd.conf中监听端口的地方添加</span></p>
<p>Listen 8080</p>
<p>也就是说这个地方监听了两个端口</p>
<p>Listen 80<br />     Listen 8080</p>
<p>看看下面这个句是否打开,没有打开的话,打开,打开如下面所示</p>

<pre class="code"><span># Virtual hosts
Include conf</span>/extra/httpd-vhosts.conf

Add the following content in httpd-vhosts.conf,

<VirtualHost *:<span>80</span>><span>
    ServerName www.myapp1.com #对应的域名,负载均衡的服务器地址
    DocumentRoot E:\soft\xampp\htdocs\www.myapp1.com #代码文件夹
</span></VirtualHost>
<VirtualHost *:<span>8080</span>><span>
    ServerName www.myapp2.com
    DocumentRoot E:\soft\xampp\htdocs\www.myapp2.com
</span></VirtualHost>

Modify the windows hosts file and add the following content

<span>127.0</span>.<span>0.1</span><span>        www.myapp1.com
</span><span>127.0</span>.<span>0.1</span>        www.myapp2.com

Modify the /etc/hosts file of Linux and add the following content

<span>192.168</span>.<span>1.12</span><span>        www.myapp1.com #这里前面的地址对应我win8本机的ip地址
</span><span>192.168</span>.<span>1.12</span>        www.myapp2.com

I put a file index.php in www.myapp1.com:80 [E:softxampphtdocswww.myapp1.comindex.php]

A file index.php is also placed in www.myapp2.com:8080 [E:softxampphtdocswww.myapp2.comindex.php]

The content in the file is basically the same, except that I'm the myapp2 is different, one is myapp1 and the other is myapp2.

If you can enter www.myapp1.com:80 and www.myapp2.com:8080 in the win8 browser to see different effects

And when you see the following results under centos (beautified by yourself), it means that the configuration is successful


<pre class="code">[root@bogon nginx]# curl www.myapp1.com:<span>80</span><span>
I</span><span>'</span><span>m the myapp1<br>【view】1</span>
[root@bogon nginx]# curl www.myapp2.com:<span>8080</span><span>
I</span><span>'</span><span>m the myapp2<br>【view】1</span>

<pre class="code"><?<span>php
</span><span>session_save_path</span>("./"<span>);
</span><span>session_start</span><span>();
</span><span>header</span>("Content-type:text/html;charset=utf-8"<span>);
</span><span>if</span>(<span>isset</span>(<span>$_SESSION</span>['view'<span>])){
    </span><span>$_SESSION</span>['view'] = <span>$_SESSION</span>['view'] + 1<span>;
}</span><span>else</span><span>{
    </span><span>$_SESSION</span>['view'] = 1<span>;
}
</span><span>echo</span> "I'm the myapp2<br>"<span>;
</span><span>echo</span> "【view】{<span>$_SESSION</span>['view']}";

Look at the effect

After everything is ok, you can access it through the browser to see the effect

Forgot to mention, the address of nginx proxy server is http://192.168.1.113,

After entering http://192.168.1.113/index.php in the browser, keep refreshing, you will find that it will be in

<p>I'm the myapp2、I'm the myapp1</p>

When these two pages are exchanged back and forth, the view will be added once without refreshing twice. This also proves that the default method mentioned earlier is rotation training, but there is another common problem here. When When a user visits a website, the session will be saved on different servers without processing (I use two different folders to simulate two servers here). There may be multiple sets of session data. How to solve this problem? Next This article talks about this issue, it is actually very simple.

The copyright of this article belongs to the author iforever (luluyrt@163.com). Any form of reprinting is prohibited without the author's consent. After reprinting the article, the author and the original text link must be given in an obvious position on the article page, otherwise we will reserve the right to pursue it. Legal liability rights.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947512.htmlTechArticlecentos+nginx configures load balancing from scratch, centosnginx nginx load balancing understanding nginx is a lightweight, A high-performance webserver can mainly do the following two things: As...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn