search
HomeBackend DevelopmentPHP TutorialCentos+nginx configures load balancing from scratch, centosnginx_PHP tutorial

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
centos用什么命令可查版本号centos用什么命令可查版本号Mar 03, 2022 pm 06:10 PM

查版本号的命令:1、“cat /etc/issue”或“cat /etc/redhat-release”,可输出centos版本号;2、“cat /proc/version”、“uname -a”或“uname -r”,可输出内核版本号。

centos重启网卡的方法是什么centos重启网卡的方法是什么Feb 22, 2023 pm 04:00 PM

centos重启网卡的方法:1、对于centos6的网卡重启命令是“service network restart”;2、对于centos7的网卡重启命令是“systemctl restart network”。

centos php怎么安装opcachecentos php怎么安装opcacheJan 19, 2023 am 09:50 AM

centos php安装opcache的方法:1、执行“yum list php73* | grep opcache”命令;2、通过“yum install php73-php-opcache.x86_64”安装opcache;3、使用“find / -name opcache.so”查找“opcache.so”的位置并将其移动到php的扩展目录即可。

centos 怎么离线安装 mysqlcentos 怎么离线安装 mysqlFeb 15, 2023 am 09:56 AM

centos离线安装mysql的方法:1、将lib中的所有依赖上传到linux中,并用yum命令进行安装;2、解压MySQL并把文件复制到想要安装的目录;3、修改my.cnf配置文件;4、复制启动脚本到资源目录并修改启动脚本;5、将mysqld服务加入到系统服务里面;6、将mysql客户端配置到环境变量中,并使配置生效即可。

centos 7安装不出现界面怎么办centos 7安装不出现界面怎么办Jan 03, 2023 pm 05:33 PM

centos7安装不出现界面的解决办法:1、选择“Install CentOS 7”,按“e”进入启动引导界面;2、 将“inst.stage2=hd:LABEL=CentOS\x207\x20x86_64”改为“linux dd”;3、重新进入“Install CentOS 7”,按“e”将“hd:”后的字符替换成“/dev/sdd4”,然后按“Ctrl+x”执行即可。

centos 怎么删除 phpcentos 怎么删除 phpFeb 24, 2021 am 09:15 AM

centos删除php的方法:1、通过“#rpm -qa|grep php”命令查看全部php软件包;2、通过“rpm -e”命令卸载相应的依赖项;3、重新使用“php -v”命令查看版本信息即可。

如何在 CentOS 9 Stream 上安装 Nagios如何在 CentOS 9 Stream 上安装 NagiosMay 10, 2023 pm 07:58 PM

我们的PC中有一个磁盘驱动器专门用于所有与Windows操作系统相关的安装。该驱动器通常是C驱动器。如果您还在PC的C盘上安装了最新的Windows11操作系统,那么所有系统更新(很可能是您安装的所有软件)都会将其所有文件存储在C盘中。因此,保持此驱动器没有垃圾文件并在C驱动器中拥有足够的存储空间变得非常重要,因为该驱动器拥有的空间越多,您的Windows11操作系统运行起来就越顺畅。但是您可以在磁盘驱动器上增加多少空间以及可以删除多少文件是有限制的。在这种情况下,

centos中ls命令不显示颜色怎么办centos中ls命令不显示颜色怎么办Apr 20, 2022 pm 03:16 PM

方法:1、利用“vim ~/.bashrc”编辑用户目录(~)下的“.bashrc”文件;2、在文件内添加“alias ls="ls --color"”;3、利用“:wq!”命令保存文件内的更改;4、“exit”命令退出终端后重新连接即可。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),