微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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   ##防火墙允许http
success
[root@lamp ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[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   ##查看端口号3306
tcp    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 MariaDB
root 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 MariaDB
installation should Now be secure.

Thanks for using MariaDB!

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

[root@lamp ~]# yum install PHP -y             ##安装PHP
[root@lamp ~]# yum install PHP-MysqL -y   ##建立PHPMysqL关联
[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网页内容
<?PHP
    PHPinfo();
?>

5,测试网页

Nginx实现动静分离处理

[root@lamp html]# vim index.PHP    ##编辑PHP网页内容
<?PHP
echo "apache web !"
?>

Nginx实现动静分离处理

二,安装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]# ls
discuz_X3.4_SC_UTF8.zip    Nginx-1.12.2.tar.gz
MysqL-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]# ls
Nginx-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 Nginx
uid=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]# ls
functions  netconsole  network  README
[root@localhost init.d]# vim Nginx         ##编辑启动脚本文件

#!/bin/bash
# chkconfig: - 99 20                                    ##注释信息
# description: Nginx Service Control Script
PROG="/usr/local/Nginx/sbin/Nginx"           ##设置变量为Nginx命令文件
PIDF="/usr/local/Nginx/logs/Nginx.pid"       ##设置变量PID文件 进程号为5346
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@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/  ##测试网页

Nginx实现动静分离处理

8,访问Nginx网页

Nginx实现动静分离处理


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实现动静分离处理


Nginx实现动静分离处理

看完上述内容,你们掌握Nginx实现动静分离处理的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程之家行业资讯频道,感谢各位的阅读!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐