LInux系统架构—-Apache与Nginx动静分离

一.动静分离概述

  • Nginx的静态处理能力比较强,但是动态处理能力不足,因此在企业中常采用动静分离技术
  • 在LNMP架构中,静态页面交给Nginx处理,动态页面交给PHP-FPM模块处理。在动静分离技术中动态页面交给Apache处理
  • Nginx不仅能作为web服务器,还具有方向代理、负载均衡和缓存的功能
  • Nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与上游服务器的连接是通过http协议进行的
  • Nginx在实现反向代理功能时的最重要指令为proxy_pass,它能够根据URL、客户参数或者其他的处理逻辑将用户请求调度至上游服务器
  • 动静分离技术我们需要做一台LAMP平台服务器处理动态请求和一台Nginx处理静态请求,将对php页面的请求转发给LAMP处理,将静态页面的请求交给Nginx处理

LAMP服务器Nginx服务器
10.1.1.17110.1.1.172

二.Server1安装LAMP

安装apache服务

[root@server1 ~]# yum -y install httpd[root@server1 ~]# systemctl enable --now httpdCreated symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.[root@server1 ~]# systemctlrestart httpd

安装Mysql服务

[root@server1 ~]# yum -y install mysql mysql-server[root@server1 ~]# systemctl enable --now mysqld[root@server1 ~]# ss -ntlStateRecv-Q Send-QLocal Address:PortPeer Address:Port Process LISTEN 0100 127.0.0.1:25 0.0.0.0:*LISTEN 0128 0.0.0.0:22 0.0.0.0:*LISTEN 0100 [::1]:25[::]:*LISTEN 070*:33060*:*LISTEN 0128 *:3306 *:*LISTEN 0128 *:80 *:*LISTEN 0128[::]:22[::]:*

安装php

[root@server1 ~]# yum install -y php[root@server1 ~]# systemctl restart php[root@server1 ~]# systemctl status php-fpm.service 

安装php-mysql

作用于使用MySQL数据库的PHP应用程序的模块

[root@server1 ~]# yum -y install php-mysql

验证LAMP平台服务

三、server2安装Nginx

[root@server2 ~]# yum -y install nginx[root@server2 ~]# systemctl enable --now nginx.service Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.[root@server2 ~]# systemctl restart nginx.service 

创建测试页面

[root@server2 ~]# echo "nginx html page" > /usr/share/nginx/html/test.html

四.动静分离

  • 修改Nginx服务器(10.1.1.172)上的配置文件,将访问PHP文件的请求(10.1.1.172/index.php)转发到LAMP服务器上(10.1.1.171)
[root@server2 ~]# vim /etc/nginx/nginx.conflocation ~ \.php$ {proxy_pass http://10.1.1.171}[root@server2 ~]# systemctl restart nginx.service
  • 验证动静分离