文章目录
前言
-
nginx的静态处理能力很强,但是动态处理能力不足,因此在企业中常采用动静分离技术
-
针对PHP,将静态页面交给nginx处理,动态页面交给PHP-FPM模块或Apache处理
-
在nginx的配置文件中,是通过location配置段配合正则匹配来实现静态与动态页面的不同处理方式
-
在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况进行相应参数配置,以达到最优的用户体验
-
默认的nginx安装参数只能供最基本的服务,还需要调整如网页缓存时间,连接超时,网页压缩等相应参数,餐能发挥出服务器的最大作用
一、部署Nginx+Apache动静分离
动静分离的概念
-
nginx动静分离简单来说就是将动态和静态请求分开,不能单纯的理解成 把动态页面和静态页面
-
物理分离。严格意义上来说应该是动态请求和静态请求分开。动静分离从目前实现角度来说大致分为两种:
-
将静态文件放在静态资源服务器上,也是目前主流的方案
-
将静态资源和动态资源在一起发布,通过nginx来进行区分
-
1、Nginx动静分离介绍
动静分离的概念
-
nginx动静分离简单来说就是将动态和静态请求分开,不能单纯的理解成 把动态页面和静态页面
-
物理分离。严格意义上来说应该是动态请求和静态请求分开。动静分离从目前实现角度来说大致分为两种:
-
将静态文件放在静态资源服务器上,也是目前主流的方案
-
将静态资源和动态资源在一起发布,通过nginx来进行区分
Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术
-
针对PHP的动静分离
静态页面交给 Nginx处理
动态页面交给 PHP-FPM模块或 Apache处理
在 Nginx的配置中,是通过 ocation配置段配合正则匹配实现静态与动态页面的不同处理方式
2、具体配置
Nginx负责静态解析,Apache负责动态网页解析。
Apache服务端:192.168.110.132
Nginx服务端:192.168.110.133
win 10 : 用于验证
(1)需求
根据需要,将配置 Nginx实现动静分离,对php页面的请求转发给LAMP处理,而静态页面交给Nginx处理,以实现动静分离
(2)配置动态apache网站
yum install httpd httpd-devel -y
systemctl start httpd.service
firewall-cmd --permanent --zone=public --add-service=http
success
firewall-cmd --permanent --zone=public --add-service=https
success
firewall-cmd --reload
success
yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
systemctl start mariadb
mysql_secure_installation
yum -y install php
yum install php-mysql -y
yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath '安装php插件'
cd /var/www/html
vim index.php
<?php
phpinfo();
?>
(3)验证php部署是否成功
(4) 静态nginx主机设置
yum -y install gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel \
pcre
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
useradd -M -s /sbin/nologin nginx
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -S HUP $MAINPID
ExecStop=/usr/bin/kill -S QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
proxy_pass http://192.168.110.132;
}
'//proxy_pass :反向代理,请求重定向'
systemctl stop firewalld
setenforce 0
(5)验证
验证nginx的网页
其他主机访问 192.168.110.133/index.html (静态)
192.168.110.133/index.php (动态)
二、开启php_fpm模块
这里要部署前提要LNMP架构 安装php
[root@localhost php-fpm.d]# vim /usr/local/nginx/conf/nginx.conf ## 进入nginx文件 在server里面写
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; ## 这里要注意路径
include fastcgi_params;
}