centos+nginx从零开始配置负载均衡
nginx负载均衡的理解
nginx是一个轻量级的、高性能的webserver,他主要可以干下面两件事:
- 作为http服务器(和apache的效果一样)
- 作为反向代理服务器实现负载均衡
现在nginx到处都可以见到,经常会看到宕机后的网页会显示nginx的字样,这也说明nginx由于高性能、使用配置简、开源单这些特点被越来越多的用户所接受,所使用。
其中第一种作为http服务器,结合php-fpm进程,对发来的请求进行处理,nginx本身并不会解析php,他只是作为一个服务器,接受客户端发来的请求,如果是php请求,则交给php进程处理,并将php处理完成之后的结果发送给客户端。这个很简单,安装好nginx+php-fpm之后配置好各自的配置文件,启动就可以实现。运行原理可以看下面这段解释:
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的整个运作过程,如图下图所示。
上面这段话解释了nginx+fastcgi的运行机制,在nginx配置文件中会对请求进行匹配,并作做出相应的处理,比如说直接返回错误文件(这里和上面说的有点区别,我估计是nginx内部对html等这些静态文件可以做类似上图的解析),使用php进程对php请求进行处理(这里的php进程可以是多个)。
第二种是用反向代理事项负载均衡,这个其实其实很简单,说起来就是自己定义一组server,对请求进行匹配,并将请求转给server中的任意一个处理,来减轻每个server的压力,先看看网上对反向向代理的定义:
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
反向代理是和正向代理(或者叫代理) 相反的,代理大家定听过吧,为了更方便的访问B资源,通过A资源间接的访问B资源,特点就是用户知道自己最终要访问的网站是什么,但是反向代理用户是不知道代理服务器后边做了什么处理的,反向代理中服务真正的处理服务器放在内网,对外网而言只可以访问反向代理服务器,这也大大提高了安全性。
安装软件
nginx安装很简单
1、安装nginx需要的环境,pcre(作用rewrite)、zlib(作用压缩)、ssl,这个也可以自己下载编译安装
yum -y install zlib;
yum –y install pcre;
yum –y install openssl;
2、下载安装nginx-*.tar.gz。
tar –zxvf nginx-1.2.8.tar.gz –C ./;
cd nginx-1.2.8;
./congigure --prefix=/usr/local/nginx;
make && make install;
3、配置
这里配置的时候只需要修改http{}之间的内容就行了,修改的第一个地方就是设置服务器组,在http节点之间添加
upstream myServer{
server www.myapp2.com:80; #这里是你自己要做负载均衡的服务器地址1
server www.myapp1.com:8080; #这里是要参与负载均衡的地址2
}nginx中的upstream支持下面几种方式:轮询(默认,按照时间顺序对所有服务器一个一个的访问,如果有服务器宕机,会自动剔除)、weight(服务器的方位几率和weight成正比,这个可以在服务器配置不均的时候进行配置)、ip_hash(对每个请求的ip进行hash计算,并按照一定的规则分配对应的服务器)、fair(按照每台服务器的响应时间(rt)来分配请求,rt晓得优先分配)、url_hash(按照访问url的hash值来分配请求),我这里使用了默认的轮训方式。
将请求指向myServer
location / {
proxy_pass http://myServer;
}完整的文件(删除注释)如下:
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>Copier après la connexion
设置反向代理后端作为负载均衡的两个服务器
可以看到上一步骤有两个服务器地址,www.myapp1.com:80和www.myapp2.com:8080,上面的nginx我是安装在虚拟机上面的,这两个服务器我是安装在本机win8系统中的,使用apache的virtualhost,设置了两个域名,这两个域名下的代码是互相独立的,设置也很简单:
1、设置apache配置文件
我使用的是xampp集成环境,要修改的地方有两个,在httpd.conf中监听端口的地方添加
Listen 8080
也就是说这个地方监听了两个端口
Listen 80
Listen 8080看看下面这个句是否打开,没有打开的话,打开,打开如下面所示
<span># Virtual hosts Include conf</span>/extra/httpd-vhosts.confCopier après la connexion在httpd-vhosts.conf中添加下面的内容,
<virtualhost>80><span> ServerName www.myapp1.com #对应的域名,负载均衡的服务器地址 DocumentRoot E:\soft\xampp\htdocs\www.myapp1.com #代码文件夹 </span></virtualhost> <virtualhost>8080><span> ServerName www.myapp2.com DocumentRoot E:\soft\xampp\htdocs\www.myapp2.com </span></virtualhost>Copier après la connexion修改windows的hosts文件,追加下面的内容
<span>127.0</span>.<span>0.1</span><span> www.myapp1.com </span><span>127.0</span>.<span>0.1</span> www.myapp2.comCopier après la connexion修改linux的/etc/hosts文件,追加下面的内容
<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.comCopier après la connexion
我在www.myapp1.com:80中放了一个文件index.php【E:\soft\xampp\htdocs\www.myapp1.com\index.php】
www.myapp2.com:8080中也放了一个文件index.php【E:\soft\xampp\htdocs\www.myapp2.com\index.php】
文件中的内容基本相同,只是I'm the myapp2这个地方有区别,一个是myapp1,另一个是myapp2。
如果你可以在win8浏览器中输入www.myapp1.com:80和www.myapp2.com:8080看到不同的效果
并且在centos下面看到下面的结果(自己美化了下)说明配置成功了
[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>Copier après la connexion
<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']}";Copier après la connexion
看看效果
等所有都ok之后可硬通过浏览器访问看看效果
忘了说了,nginx代理服务器的地址为http://192.168.1.113,
浏览器输入http://192.168.1.113/index.php之后,不停的刷新,你会发现,会在
I'm the myapp2、I'm the myapp1
这两个页面之间来回交换,view会没刷新两下增加一次,这也证明了前面所说的默认是轮训的方式,但这里又有一个比较常见的问题了,当用户访问网站时,未做处理的情况下,session会保存在不同的服务器上(我这里用两个不同的文件夹模拟两台服务器),session数据可能出现多套,这个问题怎么解决呢,下篇文章说说这个问题,其实也很简单。
本文版权归作者iforever(luluyrt@163.com)所有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
以上就介绍了centos+nginx从零开始配置负载均衡,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Étapes pour démarrer Nginx dans Linux: Vérifiez si Nginx est installé. Utilisez SystemCTL Start Nginx pour démarrer le service NGINX. Utilisez SystemCTL Activer Nginx pour activer le démarrage automatique de Nginx au démarrage du système. Utilisez SystemCTL Status Nginx pour vérifier que le démarrage est réussi. Visitez http: // localhost dans un navigateur Web pour afficher la page de bienvenue par défaut.

Le serveur n'a pas l'autorisation d'accéder à la ressource demandée, ce qui donne une erreur NGINX 403. Les solutions incluent: vérifier les autorisations de fichiers. Vérifiez la configuration .htaccess. Vérifiez la configuration de Nginx. Configurez les autorisations Selinux. Vérifiez les règles du pare-feu. Dépanner d'autres causes telles que les problèmes de navigateur, les défaillances du serveur ou d'autres erreurs possibles.

Comment configurer Nginx pour l'équilibrage de charge? Définit le pool de serveur en amont et spécifie l'IP et le port du serveur. Définissez les hôtes virtuels, écoutez les connexions et transmettez-les au pool en amont. Spécifiez l'emplacement, faites correspondre la demande et transmettez-le au pool en amont.

Les méthodes pour afficher l'état en cours d'exécution de Nginx sont: utilisez la commande PS pour afficher l'état du processus; Afficher le fichier de configuration Nginx /etc/nginx/nginx.conf; Utilisez le module d'état NGINX pour activer le point de terminaison d'état; Utilisez des outils de surveillance tels que Prometheus, Zabbix ou Nagios.

Comment configurer Nginx dans Windows? Installez Nginx et créez une configuration d'hôte virtuelle. Modifiez le fichier de configuration principale et incluez la configuration de l'hôte virtuel. Démarrer ou recharger nginx. Testez la configuration et affichez le site Web. Activer sélectivement SSL et configurer les certificats SSL. Définissez sélectivement le pare-feu pour permettre le trafic Port 80 et 443.

Comment confirmer si Nginx est démarré: 1. Utilisez la ligne de commande: SystemCTl Status Nginx (Linux / Unix), netStat -ano | Findstr 80 (Windows); 2. Vérifiez si le port 80 est ouvert; 3. Vérifiez le message de démarrage NGINX dans le journal système; 4. Utilisez des outils tiers, tels que Nagios, Zabbix et Icinga.

Le journal d'erreur est situé dans / var / log / nginx (linux) ou / usr / local / var / log / nginx (macOS). Utilisez la ligne de commande pour nettoyer les étapes: 1. Sauvegarder le journal d'origine; 2. Créez un fichier vide en tant que nouveau journal; 3. Redémarrez le service Nginx. Le nettoyage automatique peut également être utilisé avec des outils tiers tels que Logrotate ou configurés.

Réponse à la question: 304 Erreur non modifiée indique que le navigateur a mis en cache la dernière version de ressource de la demande du client. Solution: 1. Effacer le cache du navigateur; 2. Désactiver le cache du navigateur; 3. Configurer Nginx pour permettre le cache client; 4. Vérifier les autorisations du fichier; 5. Vérifier le hachage du fichier; 6. Désactiver le CDN ou le cache proxy inversé; 7. Redémarrez Nginx.
