Nginx动静分离
Nginx动静分离介绍
Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术
针对PHP的动静分离
静态页面交给Nginx处理动态页面交给PHP-FPM模块或Apache处理
在Nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式
反向代理原理
Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能
Nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时nginx与上游服务器的连接是通过http协议进行的
Nginx在实现反向代理功能时的最重要指令为proxy_pass, 它能够并能够根据URI、客户端参数或其它的处理逻辑将用户请求调度至上游服务器
配置Nginx实现动静分离
本案例根据企业需要,将配置Nginx实现动静分离,对php页面的请求转发给LAMP处理,而静态页面交给Nginx处理以实现动静分离
配置Nginx实现动静分离
配置步骤
架设并调试后端LAMP环境
安装配置Nginx处理静态页面请求,在server {};段中加入
[root@nginx php5]#vim /usr/local/httpd/conf/nginx.conf
location ~ .*.(gifliglipeg|bmp|swf)$ {
root html;
index index.html index.htm;
};
配置步骤
配置Nginx处理动态页面请求,在server{};中加入
在Apache.工作目录新建test.php
重启Nginx并测试
[root@nginx php5]#vim /usr/local/httpd/conf/nginx.conf
server {
......
location ~ .php$ {
proxy_ pass http://192.168.9.237:8080; //LAMP的IP地址
......
Nginx动静分离实验
简易搭建LAMP架构
yum install httpd httpd-devel -y //使用yum安装架构
systemctl start httpd.service //启动服务
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http //防火墙公共区域增加http协议
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https //防火墙公共区域增加https协议
success
[root@localhost ~]# firewall-cmd --reload //重载防火墙
success
[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
//使用yum安装MYSQL数据库,mariadb数据库管理系统是MYSQL数据库的分支
[root@localhost ~]# systemctl start mariadb //启动数据库
[root@localhost ~]# mysql_secure_installation //设置数据库
Enter current password for root (enter for none): //此处单击回车键
Set root password? [Y/n] y //确定设置密码
New password: //输入密码,自己定义
Re-enter new password: //再次确认密码输入
Remove anonymous users? [Y/n] n //否定移除所有匿名用户
Disallow root login remotely? [Y/n] n //否定使用root身份远程登录
Remove test database and access to it? [Y/n] n //否定删除测试数据库并访问它
Reload privilege tables now? [Y/n] y //确定重载数据库
[root@localhost ~]# yum -y install php //使用yum安装php
[root@localhost ~]# yum install php-mysql -y //建立php和mysql关联
[root@localhost ~]# 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插件
[root@localhost ~]# cd /var/www/html //进入站点目录
[root@localhost html]# vim index.php //编辑php网页
[root@localhost html]# systemctl restart httpd.service //重启服务
使用测试机输入192.168.131./index.php访问Apache网页
在另一台虚拟机上安装配置Nginx
mount.cifs //192.168.100.3/LNMP-C7 /mnt //挂载到/mnt目录下
解压源码包到/opt目录下
[root@localhost ~]# cd /abc //切换到挂载点目录
[root@localhost abc]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.gz
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt //解压Nginx源码包到/opt下
[root@localhost abc]# cd /opt/ //切换到解压的目录下
[root@localhost opt]# ls
nginx-1.12.2 rh
安装编译需要的环境组件包
[root@localhost opt]# yum -y install \
gcc \ //c语言
gcc-c++ \ //c++语言
pcre-devel \ //pcre语言工具
zlib-devel //数据压缩用的函式库
创建程序名为nginx的用户并编译Nginx
[root@localhost opt]# useradd -M -s /sbin/nologin nginx //创建程序用户,限定其
[root@localhost opt]# cd nginx-1.12.2/ //切换到nginx目录下
[root@localhost nginx-1.12.2]# ./configure \ //配置nginx
> --prefix=/usr/local/nginx \ //安装路径
> --user=nginx \ //用户名
> --group=nginx \ //用户组
> --with-http_stub_status_module //访问状态统计模块
编译和安装
[root@localhost nginx-1.12.0]# make && make install //编译及安装
制作Nginx管理脚本,便于管理使用
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
//创建软连接 [root@nginx nginx-1.12.2]# vim /etc/init.d/nginx //编辑启动脚本
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx //给脚本执行权限
[root@nginx nginx-1.12.2]# chkconfig --add nginx //添加到service管理器中
[root@nginx nginx-1.12.2]# yum install elinks -y //
[root@nginx nginx-1.12.2]# service nginx start //启动Nginx服务
[root@nginx nginx-1.12.2]# netstat -ntap | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42028/nginx: master
[root@nginx nginx-1.12.2]# systemctl stop firewalld.service //关闭防火墙
[root@nginx nginx-1.12.2]# setenforce 0 //关闭增强型安全功能
[root@nginx nginx-1.12.2]# elinks http://192.168.18.136/
访问Nginx网页
此时访问192.168.131.133/html
如果输入:http://192.168.18.136/index.php 则无法处理
修改Nginx配置文件开启动静分离
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf ##修改配置文件
location ~ \.php$ { //找到此处将注释去除,开启动静分离 proxy_pass http://192.168.131.133; //填写动态处理的Apache的服务器地址 }
[root@localhost init.d]# service nginx stop //关闭服务
[root@localhost init.d]# service nginx start //开启服务
此时输入:http://192.168.131.133/index.php 这个网址可以得到如下界面
因为此处交给Apache去处理php的请求