温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nginx动静分离(实战!)

发布时间:2020-10-25 00:48:05 来源:网络 阅读:57728 作者:一拳超人007 栏目:系统运维

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 ##建立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网页内容 <?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处理静态,Apache处理动态信息

谢谢阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI