一,部署LNMP环境,实现动态网站解析
动态网站 在不同环境下,网站内容有可能发生变化
静态网站 在不同环境下,网站内容不会发生变化
L linux
N nginx
M mariadb(mysql)
P PHP
1,准备nginx以及相关软件包
yum -y install gcc make pcre-devel openssl-devel psmisc killall nginx
rm -rf /usr/local/nginx //删除nginx原有目录
cd /root/lnmp_soft/nginx-1.17.6
./configure --with-http_ssl_module //配置
make //编译
make install //安装
yum -y install mariadb //安装数据库客户端
yum -y install mariadb-server //安装数据库服务端
yum -y install mariadb-devel //安装数据库依赖包
yum -y install php //安装php环境包(相当于解释器)
yum -y install php-mysql //安装php与数据库关联的软件包
yum -y install php-fpm //安装可以让nginx具备php语言解析能力的软件包
systemctl start mariadb //开启数据库
systemctl start php-fpm //开启php-fpm
netstat -ntulp | grep mysql
netstat -ntulp | grep :3306
netstat -ntulp | grep php-fpm
2,准备动态网站页面的测试文件
cd ~/lnmp_soft/php_scripts/
cp test.php /usr/local/nginx/html //拷贝动态网站测试页面到nginx中
cd /usr/local/nginx
sbin/nginx //启动nginx服务
使用火狐访问http://192.168.2.5/test.php 可以看到下载文件
3,修改nginx配置
打开nginx配置文件,第65到71行去掉注释,69行不用去
location ~ \.php$ { //~是使用正则表达式,匹配以.php结尾
root html; //网站页面位置,不用改,保持默认
fastcgi_pass 127.0.0.1:9000; //一旦用户访问了.php结尾的文件,就让nginx找后台的php-fpm(端口号9000)
fastcgi_index index.php; //动态网站的默认页面,无需修改
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //无用行,保持注释状态
include fastcgi.conf; //这里需要改扩展名
}
/usr/local/nginx/sbin/nginx //开启nginx,如果已经开启就使用/usr/local/nginx/sbin/nginx -s reload 如果均不能正常开启,就用killall nginx然后重新试
4,然后使用火狐浏览器访问
http://192.168.2.5/test.php可以看到以下内容说明nginx 实现动静分离
This is HTML message
c is bigger
FastCGI 公共网关接口
可以连接比如像nginx这样的网站程序到网站语言解释器
Nginx --- php
测试有数据库的动态网站
cd ~/lnmp_soft/php_scripts/ //到php目录
cp mysql.php /usr/local/nginx/html/ //拷贝另外一个测试页面到nginx
浏览器打开http://192.168.2.5/mysql.php 可以看到网站显示了数据的账户信息
mysql //进入数据库
create user dc@localhost identified by '123'; //创建测试账户
quit //退出
刷新http://192.168.2.5/mysql.php 可以看到新创建的用户
二,地址重写
rewrite 旧地址 新地址 选项
[root@proxy nginx]# echo "nginx-a~~" > html/a.html
[root@proxy nginx]# echo "nginx-b~~" > html/b.html
地址重写测试1:相同网站不同页面
vim conf/nginx.conf
rewrite ^/a.html$ /b.html; //在42行添加
sbin/nginx -s reload
systemctl stop firewalld
//然后打开火狐使用http://192.168.2.5/a.html路径访问网站可以看到b页面
//如果访问http://192.168.2.5/a.htmldc 或者http://192.168.2.5/dc/a.html
//可能造成误跳转,所以在rewrite 后匹配旧路径时要增加^ 和$
地址重写测试2:相同网站不同页面
rewrite ^/a.html$ /b.html redirect; //在刚刚的配置中添加redirect
sbin/nginx -s reload
//使用http://192.168.2.5/a.html路径访问网站
地址重写测试3:从旧网站跳到新网站
rewrite / http://www.tmooc.cn; //访问192.168.2.5就跳到tmooc
sbin/nginx -s reload
//使用火狐浏览器访问192.168.2.5 会跳到tmooc.cn
地址重写测试4: 不同网站的相同页面的跳转
rewrite /(.*) http://www.tmooc.cn/$1; //小括号在正则中的效果是保留,相当于复制,后面使用$1将之前保存的页面地址粘贴到新网站
sbin/nginx -s reload //重加载配置文件
//使用火狐浏览器访问192.168.2.5/b.html可以转到www.tmooc.cn/b.html (由于该网站没有b.html页面,会出现404报错属于正常)
地址重写测试5:为火狐浏览器用户设置专属页面
火狐专用页面 192.168.2.5/abc.html html/firefox/abc.html
其他专用页面 192.168.2.5/abc.html html/abc.html
修改配置文件,删除原有地址重写,原地添加
if ($http_user_agent ~* firefox){ //如果用户使用了火狐浏览器
rewrite /(.*) /firefox/$1; //就进行地址重写操作,让用户看到火狐专属页面
}
//$http_user_agent是nginx的内置变量,存储了用户的信息,比如用的什么浏览器
~匹配正则 *忽略大小写
使用火狐浏览器查看192.168.2.5/abc.html可以看到之前html/firefox目录下的页面,非火狐浏览器看到的是html下的页面
地址重写的选项:
last 不在读其他rewrite
修改配置文件,删除原有地址重写,原地添加
rewrite /a.html /b.html last;
rewrite /b.html /c.html;
break 不再读其他语句
location / { //此处为默认的location
rewrite /a.html /b.html break; //将last改为break可以阻止后面的语句,此处如果是last则无法阻止后面location语句中的rewrite语句
root html;
index index.html index.htm;
}
location /b.html { //这里是新添加的location
rewrite /b.html /c.html;
}
redirect 临时重定向 状态码302 爬虫不感兴趣
permanent 永久重定向 状态码 301 爬虫感兴趣
网友评论