栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

2/17(动态网站、地址重写、选项)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2/17(动态网站、地址重写、选项)

动态网站

cp ~/lnmp_soft/php_scripts/test.php /usr/local/nginx/html #复制
cat html/test.php #查看

准备nginx以及相关软件包

killall nginx //停止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 mariadb-server //安装数据库客户端与服务端
systemctl start mariadb //开启数据库服务
yum -y install mariadb-devel //安装数据库开发::环境依赖包

yum -y install php //安装php,相当于解释器
yum -y install php-fpm //安装可以帮助nginx解析php语言编写的动态网站的服务
yum -y install php-mysql //安装php与mysql关联的软件包
systemctl start php-fpm //开启php-fpm服务

yum -y install net-tools
netstat -ntulp | grep mysql //检查数据库
netstat -ntulp | grep php-fpm //检查php-fpm服务

准备动态网站页面的测试文件

1.修改配置文件(vim conf/nginx.conf)
打开nginx配置文件,第65到71行去掉注释(可以用:65,71s/#//),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; //这里是另外一个配置文件,需要改扩展名
}

2.拷贝
cp ~/lnmp_soft/php_scripts/test.php /usr/local/nginx/html //拷贝动态网站测试页面到nginx中
ls ~/lnmp_soft/php_scripts/test.php #查看

3.起服务
sbin/nginx //启动nginx服务

4.主机浏览器测试
使用火狐访问http://192.168.2.5/test.php 可以看到页面内容

测试有数据库的动态网站

1.拷贝
cp ~/lnmp_soft/php_scripts/mysql.php /usr/local/nginx/html/ //拷贝另外一个测试页面到nginx

2.进数据库,创建测试用户,退出
mysql //进入数据库
create user dc@localhost identified by ‘123’; //创建测试账户
quit //退出

3.主机浏览器测试
刷新http://192.168.2.5/mysql.php 可以看到新创建的用户

地址重写

rewirte 匹配路径 实际看到的页面 选项

地址重写测试1:相同网站不同页面
cp conf/nginx.conf.default conf/nginx.conf //可以先还原配置文件
cp:是否覆盖"conf/nginx.conf"? y
Vim conf/nginx.conf 打开配置文件,在38行添加
rewrite /a.html /b.html; //用户访问的路径中包含a.html的话就跳转到b.html页面

然后准备测试页面
echo “nginx-a~~” > html/a.html
echo “nginx-b~~” > html/b.html
[root@proxy nginx]# sbin/nginx -s reload
使用浏览器访问192.168.2.5/a.html看到的是b.html的内容

地址重写测试2:相同网站不同页面
rewrite ^/a.html$ /b.html redirect; //在刚刚的配置中添加redirect
sbin/nginx -s reload //重启加载配置文件
使用http://192.168.2.5/a.html路径访问网站时,地址栏同时发生变化

地址重写测试3:不同网站间跳转
rewrite / http://www.baidu.com; //访问192.168.2.5的网站可以跳转到www.baiducom
sbin/nginx -s reload //重启加载配置文件

地址重写测试4:不同网站间跳转
rewrite /(.*) http://www.tmooc.cn/$1; //访问老网站会跳到新网站,同时会携带所访问的页面
sbin/nginx -s reload //重新加载配置文件

地址重写测试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/KaTeX parse error: Expected 'EOF', got '}' at position 29: …操作,让用户看到火狐专属页面 }̲ //http_user_agent是nginx的内置变量,存储了用户的信息,比如用的什么浏览器
~匹配正则 *忽略大小写
改完后sbin/nginx -s reload

使用火狐浏览器查看192.168.2.5/abc.html可以看到之前html/firefox目录下的页面
非火狐浏览器打开192.168.2.5/abc.html看到的是html下的页面

选项

edirect 临时重定向 状态码 302 爬虫不感兴趣
permanent 永久重定向 状态码 301 爬虫感兴趣

last 不再读其他rewrite
echo “nginx-c~~” > html/c.html //准备素材c页面
rewrite /a.html /b.html last; //不加last的话nginx会把多个rewrite语句综合处理
结果就是看a页面结果跳到c页面了,加了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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/741204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号