千家信息网

Nginx实现动静分离处理

发表于:2024-09-29 作者:千家信息网编辑
千家信息网最后更新 2024年09月29日,这篇文章给大家分享的是Nginx实现动静分离处理的方法,相信大部分人都还没学会这个技能,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。Nginx动静分离介绍Nginx的静态处理能力很强
千家信息网最后更新 2024年09月29日Nginx实现动静分离处理

这篇文章给大家分享的是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实现动静分离处理的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0