一、简介
本章介绍一些架构原理基础知识,
1.1、LNMP及php-fpm
请参考https://www.cnblogs.com/zhangxingeng/p/10242902.html
1.2、透明代理、反向代理,正向代理
请参考https://www.cnblogs.com/zhangxingeng/p/10331318.html
贴一张架构图
1.3、实现反向代理配置
1 server{2 ????listen 80;3 ????location /{4 ????????proxy_pass http:192.168.216.52:8080 #上游的应用服务器5 6 ????}7 }
1.4、负载均衡
nginx通过反代可以实现负载均衡的效果,上面是通过反向代理实现负载,所以nginx实现的是七层负载均衡,它能够识别http协议,根据http报文将不同类型的请求转发到不同的后端web服务器上,后端的web服务器称为“上游服务器”,即upstream服务器。架构图和上面类似配置如下:
1 upstream myweb{ 2 ????server 192.168.216.53:8080; 3 ????server 192.168.216.54:8080; 4 } 5 ?6 server { 7 ????listen 80; 8 ????location /{ 9 ????????proxy_pass http://myweb;10 ????}11 }
1.5、nginx的反向代理有几种实现方式:
1)仅使用模块ngx_http_proxy_module实现简单的反向代理,指令为proxy_pass。
2)使用fastcgi模块提供的功能,反向代理动态内容,指令为fastcgi_pass。
3)使用ngx_http_memcached_module模块提供的功能,反向代理mencache缓存内容,指令为memcached_pass。
4)结合upstream模块实现更人性化的分组反向代理。
1.4.1、注意fastcgi_pass与proxy_pass的区别
1.6、虚拟主机
有的网站访问量大,需要负载,然而病逝所有网站都需要,对于访问量太小的可以将多个网站部署再同一台服务器上,比如你可以把www.test1.com 和www.test2.com两个网站部署再同一个服务器上,两个域名解析到同一个ip地址,但是用户通过两个域名却可以打开两个完全不同的网站,互相不影响,就像访问两个服务器一样
1 server { 2 ????listen 80 default_server; 3 ????server_name; 4 ????return 444; #过滤其他域名的请求,返回444状态码 5 } 6 ?7 server { 8 ?????9 ????listen 80;10 ????server_name www.test1.com;11 ????location /{12 ????????proxy_pass http://localhost:808013 ????}14 }15 16 server {17 ????listen 80;18 ????server_name www.test2.com;19 ????location /{20 ????????proxy_pass http://localhost:8081;21 ????}22 }
二、部署
2.1、配置简单的反代实验
2.1.1、架构
反向代理配置挺简单的,nginx-proxy(192.168.216.51)的配置不需要root、index等指令,只需要几个和代理相关的指令即可
2.2、部署web1-51,的反向代理配置
yum install nginx -y
修改配置
[root@www ~]# cat /etc/nginx/conf.d/default.conf server { ???????listen ?80; ???????server_name www.web1.com; ???????location ~ \.(png|jpg|jpeg|bmp|gif)$ { ???????????????proxy_pass http://192.168.216.53:80; ???????} ???????location / { ???????????????proxy_pass http://192.168.216.54:80; ???????} ???????location ~ \.(php|php5)$ { ???????????????proxy_pass http://192.168.216.52:80; ???????} ???????error_page 500 502 503 504 /50x.html; ???????location = /50x.html { ???????????????root html; ???????}}[root@www ~]#
2.3、部署web3,图片服务器
安装httpd服务
yum install -y httpd
放两张图片后启动服务
1 [root@www html]# pwd2 /var/www/html3 [root@www html]# ll 4 total 3405 -rw-r--r--. 1 root root 320660 Jun ?7 ?2018 123.jpg6 -rw-r--r--. 1 root root ?21177 Feb 13 12:45 345.jpg7 [root@www html]# systemctl restart httpd
验证,出错只显示一个小方块浏览器f12查看一下,文件大小明显不对,查看preview也不正常, 重新上传解决
正确效果图如下
2.3、部署静态服务器,同样是httpd
yum install -y httpd
1 [root@web4 html]# pwd2 /var/www/html3 [root@web4 html]# cat index.html 4 hi,this is web4,static5 [root@web4 html]#
systemctl restart httpd
查看一下效果,没问题,下面部署nginx+php-fpm
2.3、部署nginx+php-fpm
两台分离部署
2.3.1、web2-52
yum install nginx -y
配置
1 [root@web2 html]# vim /etc/nginx/conf.d/default.conf ?2 ?3 server { 4 ????????listen 80; 5 ????????server_name 192.168.216.52; 6 ????????index index.html index.htm index.phpi; 7 ????????root /www/web; 8 ????????location / { 9 10 ????????????????root /usr/share/nginx/html;11 ????????}12 13 ????????location ~.*\.php$ {14 15 ????????????????fastcgi_pass ???192.168.216.55:9000;16 ????????????????fastcgi_index ??index.php;17 ????????????????fastcgi_param ??SCRIPT_FILENAME $document_root$fastcgi_script_name;18 ????????????????include ????????fastcgi_params;19 ????????}
systemctl restart nginx
2.3.2、web5-55
1 ?yum install -y php php-devel php-fpm php-mysql php-common php-devel php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel
vim ??/etc/php-fpm.d/www.conf listen = 192.168.216.55:9000listen.owner = nobodylisten.group = nobodyuser = nginxgroup = nginx
1 ?mkdir -p /www/web/2 ?group -g 983 nginx3 ?groupadd -g 983 nginx4 ?useradd -u 983 -g nginx nginx5 6 chown -R nginx:nginx /www
准备phpinfo文件
1 [root@web5 web]# ll 2 total 43 -rwxrwxrwx 1 nginx nginx 21 Feb 12 12:35 index.php4 [root@web5 web]# cat index.php 5 <?php6 ????????phpinfo();7 ?>8 [root@web5 web]#
注意:如果测试结果为不运行php,下载php得话,应该是nginx配置文件目录配置不当造成得
检查结果,成功
2.4、负载均衡配置
这个实验得负载均衡再nginx主机负载php请求,做两个php-fpm的负载均衡
修改web2-52的nginx配置文件
1 [root@web2 web]# cat /etc/nginx/conf.d/default.conf ?2 upstream php-cluster { 3 ????????server 127.0.0.1:9000 max_fails=3 fail_timeout=10s; 4 ????????server 192.168.216.55:9000 max_fails=3 fail_timeout=10s; 5 ????????} 6 ?7 server { 8 ????????listen 80; 9 ????????server_name 192.168.216.52;10 ????????index index.html index.htm index.phpi;11 ????????root /www/web;12 ????????location / {13 14 ????????????????root /usr/share/nginx/html;15 ????????}16 17 ????????location ~.*\.php$ {18 19 ????????????????fastcgi_pass ???php-cluster;20 ????????????????fastcgi_index ??index.php;21 ????????????????fastcgi_param ??SCRIPT_FILENAME $document_root$fastcgi_script_name;22 ????????????????include ????????fastcgi_params;23 ????????}24 }25 [root@web2 web]#
并且安装php,php-fpm
1 ?yum install -y php php-devel php-fpm php-mysql php-common php-devel php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel
配置php-fpm配置文件
1 [root@web2 web]# vim /etc/php-fpm.d/www.conf 2 listen = 127.0.0.1:90003 listen.owner = nobody4 listen.group = nobody5 user = nginx6 group = nginx
创建index.php文件
mkdir -p /www/web
vim /www/web/index.php
1 [root@web2 web]# cat index.php 2 <?php3 ????????echo ("52"); #输出52,测试的时候容易分辨4 ????????phpinfo();5 ?>
web5-55同样输出55
1 [root@web5 web]# vim index.php 2 3 <?php4 echo ("55");5 ????????phpinfo();6 ?>
好了,重启服务测试一把
systemctl restart nginx #web2
systemctl restart php-fpm #web2与web5
测试效果如下
刷新浏览器
转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/10330735.html
Centos7 nginx 虚拟主机、反向代理服务器及负载均衡,多台主机分离php-fpm实验
原文地址:https://www.cnblogs.com/zhangxingeng/p/10330735.html