千家信息网

Nginx动静分离(实战!)

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,Nginx动静分离介绍Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术针对PHP的动静分离静态页面交给Nginx处理动态页面交给PHP-FPM模块或Apache处理在
千家信息网最后更新 2026年11月04日Nginx动静分离(实战!)

Nginx动静分离介绍

  • Nginx的静态处理能力很强,但是动态处理能力不足,因此,在企业中常用动静分离技术
  • 针对PHP的动静分离
    静态页面交给Nginx处理
    动态页面交给PHP-FPM模块或Apache处理
  • 在Nginx的配置中,是通过location配置段配个正则匹配实现静态与动态页面的不同处理方式

反向代理原理

  • Nginx不仅能作为web服务器,还具有反向代理,负载均衡和缓存的功能
  • Nginx通过proxy模块实现将客户端的请求代理至上游服务器,此时Nginx与上游服务器的连接是通过http协议进行的
  • Nginx在实现反向代理功能时最重要指令为proxy_pass,它能够根据URI,客户端参数或其他的处理逻辑将用户请求调度至上游服务器

实验环境

LAMP服务器(192.168.13.139)Nginx服务器(192.168.13.140)

一,搭建LAMP(简易搭建)

1,安装Apache服务,并允许通过防火墙进行访问

[root@lamp ~]# yum install httpd httpd-devel -y   ##安装http服务及开发包[root@lamp ~]# systemctl start httpd.service   ##开启服务[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=http   ##防火墙允许httpsuccess[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=httpssuccess[root@lamp ~]# firewall-cmd --reload   ##重启防火墙success

2,安装mariadb数据库

[root@lamp ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y##安装数据库[root@lamp ~]# systemctl start  mariadb   ##开启数据库[root@lamp ~]# netstat -ntap | grep 3306   ##查看端口号3306tcp    0     0 0.0.0.0:3306       0.0.0.0:*     LISTEN     2200/mysqld  [root@lamp ~]# mysql_secure_installation   ##设置数据库Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n] y    ##设置root密码New password:       ##输入密码Re-enter new password:     ##确认密码Remove anonymous users? [Y/n] n   ##允许匿名访问 ... skipping.Disallow root login remotely? [Y/n] n  ##允许远程登录 ... skipping.Remove test database and access to it? [Y/n] n    ##保留测试数据库 ... skipping.Reload privilege tables now? [Y/n] y     ##重启数据库 ... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB!

3,安装PHP,并进行数据库关联

[root@lamp ~]# yum install php -y             ##安装PHP[root@lamp ~]# yum install php-mysql -y   ##建立php和mysql关联[root@lamp ~]# yum install -y \> php-gd                                                    ##GD库是php处理图形的扩展库> php-ldap                                                ##轻量级目录访问协议> php-odbc                                              ##应用程序编程接口> php-pear                                             ##扩展应用代码库> php-xml php-xmlrpc                           ##xml文件> php-mbstring                                     ##多字节字符串> php-snmp                                          ##管理端开发> php-soap                                           ##SOAP 扩展可以用来提供和使用 Web Services> curl curl-devel                                   ##支持数据文件下载工具> php-bcmath                                      ##BCMath库来支持更加精确的计算

4,切换到站点,编辑网页内容

[root@lamp ~]# cd /var/www/html/   ##切换到站点[root@lamp html]# vim index.php    ##编辑php网页内容

5,测试网页

[root@lamp html]# vim index.php    ##编辑php网页内容

二,安装Nginx

1,在Linux上使用远程共享获取文件并挂载到mnt目录下

[root@localhost ~]# smbclient -L //192.168.100.3/   ##远程共享访问Enter SAMBA\root's password:                                 Sharename       Type      Comment                                ---------       ----      -------                                LNMP-C7         Disk       [root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt  ##挂载到/mnt目录下

2,解压源码包到/opt下,并查看

[root@localhost ~]# cd /mnt    ##切换到挂载点目录[root@localhost mnt]# lsDiscuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gzmysql-boost-5.7.20.tar.gz  php-7.1.20.tar.gz[root@localhost mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt   ##解压Nginx源码包到/opt下[root@localhost mnt]# cd /opt/    ##切换到解压的目录下[root@localhost opt]# lsnginx-1.12.2  rh

3,安装编译需要的环境组件包

[root@localhost opt]# yum -y install \gcc \                                       //c语言gcc-c++ \                        //c++语言pcre-devel \                     //pcre语言工具zlib-devel                       //数据压缩用的函式库

4,创建程序用户nginx并编译Nginx

[root@localhost opt]# useradd -M -s /sbin/nologin nginx  ##创建程序用户,安全不可登陆状态[root@localhost opt]# id nginxuid=1001(nginx) gid=1001(nginx) 组=1001(nginx)[root@localhost opt]# cd nginx-1.12.0/                 ##切换到nginx目录下[root@localhost nginx-1.12.0]# ./configure \         ##配置nginx> --prefix=/usr/local/nginx \        ##安装路径> --user=nginx \                         ##用户名> --group=nginx \                       ##用户组> --with-http_stub_status_module     ##状态统计模块

5,编译和安装

[root@localhost nginx-1.12.0]# make     ##编译...[root@localhost nginx-1.12.0]# make install   ##安装...[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##创建软连接让系统识别nginx启动脚本

6,制作管理脚本,便于使用service管理使用

[root@localhost nginx]# cd /etc/init.d/   ##切换到启动配置文件目录[root@localhost init.d]# lsfunctions  netconsole  network  README[root@localhost init.d]# vim nginx         ##编辑启动脚本文件#!/bin/bash# chkconfig: - 99 20                                    ##注释信息# description: Nginx Service Control ScriptPROG="/usr/local/nginx/sbin/nginx"           ##设置变量为nginx命令文件PIDF="/usr/local/nginx/logs/nginx.pid"       ##设置变量PID文件 进程号为5346case "$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 1esacexit 0[root@localhost init.d]# chmod +x /etc/init.d/nginx    ##给启动脚本执行权限[root@localhost init.d]# chkconfig --add nginx          ##添加到service管理器中[root@localhost init.d]# service nginx stop                ##就可以使用service控制nginx[root@localhost init.d]# service nginx start

7,安装elinks测试Nginx网页

[root@localhost init.d]# yum install elink -y  ##安装软件[root@localhost init.d]# netstat -ntap | grep 80   ##查看Nginx端口tcp     0   0 0.0.0.0:80    0.0.0.0:*    LISTEN    44663/nginx: master [root@localhost init.d]# systemctl stop firewalld.service   ##关闭防火墙[root@localhost init.d]# setenforce 0[root@localhost init.d]# elinks http://192.168.13.140/  ##测试网页

8,访问Nginx网页


三,修改Nginx配置文件开启动静分离

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf  ##修改配置文件     location ~ \.php$ {    ##找到此处将注释去除,开启动静分离        proxy_pass   http://192.168.13.139;   ##填写动态处理的Apache的服务器地址        }[root@localhost init.d]# service nginx stop   ##关闭[root@localhost init.d]# service nginx start   ##开启

四,测试网页(192.168.13.140)


实现了动静分离处理,Nginx处理静态,Apache处理动态信息

谢谢阅读!

服务 处理 数据 文件 网页 数据库 服务器 目录 动静 切换 配置 动态 用户 测试 脚本 防火墙 静态 代理 管理 编译 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广州紫鲸互联网科技有限公司 互联网的科技 互联网科技有限公司经营范围 网络科技有限互联网 互联网科技网站 大白互联网科技 四个字互联网科技公司取名 互联网科技股午盘股票涨跌 汉立互联网科技有限公司联系电话 科技与互联网的利与弊 辽宁互联网新科技 本届互联网博览会上的黑科技 乌鲁木齐新华智原互联网科技学校 西安小鹰速贷互联网科技有限公司 工业互联网科技成果转化 南京一网一红互联网科技有限公司 科技信息时代互联网发展 广东猫王互联网科技有限公司电话 瑞安市蟠桃会互联网科技有限公司 伍佰万互联网科技是干嘛的 亿人帮互联网科技有限公司 狮子座互联网科技 以房互联网科技 互联网科技视频 衡水科技产业互联网平台 华夏互联网科技业 江阴互联网智能科技产品进口 合肥互联网科技公司公章图片 互联网金融科技公司的优势 信承诺互联网科技有限公司
0