Nginx动静分离介绍
1.Nginx的静态处理能力很强,但是动态处理不足,因此,在企业中常用动静分离技术
2.针对PHP的动静分离
静态页面交给Nginx处理
动态页面交给PHP+FPM模块或Apache处理
3.在Nginx的配置中,是通过location配置段配合正则匹配实现静态与动态页面的不同处理方式
反向代理原理
1.Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能
2.Nginx通过proxy模块实现将客户端的请求代理至,上游服务器,此时nginx与.上游服务器的连接是通过http协议进行的
3.Nginx在实现反向代理功能时的最重要指令为proxy_pass, 它能够并能够根据URI、客户端参数或其它的处理逻辑将用户请求调度至,上游服务器
配置Nginx实现动静分离
1.本案例根据企业需要,将配置Nginx实现动静分离,对php页面的请求转发给LAMP处理,而静态页面交给Nginx处理,以实现动静分离
2.架构如图所示:
配置步骤:
1.配置Nginx处理动态页面请求,在server{};中加入
2.在Apache.工作目录新建test.php
3.重启Nginx并测试
[[email protected] php5]#vim /usr/local/httpd/conf/nginx.conf
server {
.....
location ~ \.php$ {
proxy_pass http://192.168.9.237:8080;
...... //LAMP的IP地址
Demo:
环境准备:两台CentOS 7,其中7-3做为lamp,7-4做为nginx
第一步:安装httpd
[[email protected] ~]# yum install httpd httpd-devel -y
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[[email protected] ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[[email protected] ~]# firewall-cmd --reload
success
[[email protected] ~]# systemctl start httpd
此时可以使用宿主机访问Apache的主页如下图所示:
第二步:安装mariadb数据库(快捷轻量化的数据库)
[[email protected] ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# systemctl start mariadb
[[email protected] ~]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 16836/mysqld
[[email protected] ~]# mysql_secure_installation //对数据库进行设置
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we‘ll need the current
password for the root user. If you‘ve just installed MariaDB, and
you haven‘t set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): //给root管理员设定密码,直接回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y //是否设置,选择yes
New password: //输入新密码
Re-enter new password: //重复输入新密码
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] n //是否删除匿名用户,选择no
... skipping.
Normally, root should only be allowed to connect from ‘localhost‘. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n //是否拒绝root用户远程登陆,选择no
... skipping.
By default, MariaDB comes with a database named ‘test‘ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n //是否删除测试数据库,选择no
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y //是否加载权限列表,选择yes
... 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!
第三步:安装php
[[email protected] ~]# yum install php -y
[[email protected] ~]# yum install php-mysql -y
[[email protected] ~]# 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
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# vim index.php
<?php
phpinfo();
?>
输入:wq保存退出
[[email protected] html]# systemctl restart httpd.service
这时在宿主机的浏览器中输入地址:http://192.168.18.128/index.php,就可以访问到lamp的php主页
测试准备:
[[email protected] html]# vim index.php
<?php
echo "apache web !"
?>
输入:wq保存退出
此时 http://192.168.18.128/index.php 这个地址上显示的内容如下:
Nginx上的操作:
[[email protected] ~]# mkdir /aaa
[[email protected] ~]# mount.cifs //192.168.10.193/rpm /aaa
Password for [email protected]//192.168.10.193/rpm:
[[email protected] ~]# cd /aaa
[[email protected] aaa]# ls
apr-1.6.2.tar.gz error.png nginx-1.12.2.tar.gz
apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2 php-7.1.10.tar.bz2
awstats-7.6.tar.gz lf.jpg php-7.1.20.tar.gz
cronolog-1.6.2-14.el7.x86_64.rpm mysql-5.6.26.tar.gz
Discuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz
[[email protected] aaa]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[[email protected] aaa]# cd /opt
[[email protected] opt]# ls
nginx-1.12.2 rh
[[email protected] opt]# cd nginx-1.12.2/
[[email protected] nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[[email protected] nginx-1.12.2]# yum install gcc gcc-c++ pcre-devel zlib-devel -y
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[[email protected] nginx-1.12.2]# make && make install
[[email protected] nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] 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
输入:wq保存退出
[[email protected] nginx-1.12.2]# chmod +x /etc/init.d/nginx
[[email protected] nginx-1.12.2]# chkconfig --add nginx
[[email protected] nginx-1.12.2]# yum install elinks -y
[[email protected] nginx-1.12.2]# service nginx start
[[email protected] nginx-1.12.2]# netstat -ntap | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42028/nginx: master
[[email protected] nginx-1.12.2]# systemctl stop firewalld.service
[[email protected] nginx-1.12.2]# setenforce 0
[email protected] nginx-1.12.2]# elinks http://192.168.18.136/
此时得到如下界面,可按q,选择yes,回车退出
此时在宿主机输入:http://192.168.18.136/index.html 这个网址,会得到以下界面
此时如果输入:http://192.168.18.136/index.php 则无法处理,得到如下界面:
第四步:做转发处理:
[[email protected] nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
proxy_pass http://192.168.18.128;
}
#以上内容意思为:动态请求转交给谁去处理
#我们找到以上内容将前面的注释去掉,并将其中的IP地址改为另外一台7-3的地址
输入:wq保存退出
[[email protected] nginx-1.12.2]# service nginx stop
[[email protected] nginx-1.12.2]# service nginx start
此时输入:http://192.168.18.136/index.php 这个网址可以得到如下界面:
因为此处交给Apache去处理php的请求
结论:后缀为html就是静态元素,后缀为php就是动态元素,地址是不用改变的,因为我们在其中做了转发处理!动静分离实验成功!
原文地址:https://blog.51cto.com/14464303/2450076
时间: 2024-10-06 11:03:42