首页 技术 正文
技术 2022年11月19日
0 收藏 303 点赞 4,246 浏览 3300 个字

一、nginx  rewrite标签

rewrite 实现URL的改写主要是实现伪静态

1、  rewrite指令语法

指令语法:rewrite regex replacement[flag]

默认值:none

应用位置:server,location,if

rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement部分内容,结尾是flag标记,下面是一个简单的URL Rewrite跳转

Rewrite ^/(.*)http://www.etiantian.org/$1 permanent

上述rewrite【指令说明:】

rewrite为固定关键字,表示开启一条rewrite匹配规则,regex部分

这是一个正则表达式

【匹配所有】,匹配成功后跳转到http://www.etiantian.org/$1,这是取前面regex部分()里的内容,结尾permanent;表示永久301重定向标记

nginx rewrite标签配置以及用户认证配置

nginx rewrite标签配置以及用户认证配置

nginx rewrite标签配置以及用户认证配置

在以上flag标记中,last和break用来实现URL重写,浏览器地址URL地址不变,但是在服务器端访问的程序及路径发生变化,redirect和permanent用来实现URL跳转,浏览器地址栏会显示跳转后的URL地址

last和break标记的实现功能类似但是二者之间有细微的差别,使用alias指令的时候必须用last标记,使用proxy_pass指令的时候要使用break标记,last标记在本条rewrite规则执行完毕之后,会对其所在的server{…}标签重新发起请求,而break标记则在本条规则匹配完成后终止匹配,不再匹配后面的规则

、配置301跳转的方法如下:[root@A conf]# vim extra/www.conf
server {
listen ;
server_name www.cnblogs.c; --》老域名
rewrite ^(.*) http://www.cnblogs.co/$1 permanent;
}
server {
listen ;
server_name www.cnblogs.co; --》新域名
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}别名状态是200,rewrite状态码是301
区别:
用别名,效率高 但是看不到改过的域名
跳转,可以看到新的域名,但是这是新的跳转,对于pv来说是两次请求、检查语法,然后重启nginx
、用curl -I 查看是否为301
[root@oldboy extra]# curl -I www.cnblogs.c
HTTP/1.1 Moved Permanently
Server: nginx/1.8.
Date: Sun, Mar :: GMT
Content-Type: text/html
Content-Length:
Connection: keep-alive
Location: http://www.cnblogs.co//[root@oldboy extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.cnblogs.c

301跳转步骤

NginxRewrite的应用

、    可以调整用户浏览的URL ,看起来更规范,合乎开发及产品的需求
(这个rewrite主要是开发的事情,没有要求就不做301跳转)
、 为了让搜索引擎收录网站内容及用户体验更好,企业会将动摇URL地址伪装成静态地址提供服务
、 网站更换新域名后,让旧的域名的访问跳转到新的域名商上,例如:让京东的360buy变成了jd.com
、 根据特殊变量、目录、客户端的信息进行URL跳转等

二、nginx访问认证

可以在官网找到auth basic_module

Http状态码301和302的区别
、什么是301重定向?
301重定向/跳转一般,表示本网页永久性转移到另一个地址。
301是永久性转移(Permanently Moved),SEO常用的招式,会把旧页面的PR等信息转移到新页面;
、什么是302重定向?
302重定向表示临时性转移(Temporarily Moved ),当一个网页URL需要短期变化时使用。
、301重定向与302重定向的区别
301重定向是永久的重定向,搜索引擎在抓取新内容的同时也将旧的网址替换为重定向之后的网址。
302重定向是临时的重定向,搜索引擎会抓取新的内容而保留旧的网址。因为服务器返回302代码,搜索引擎认为新的网址只是暂时的

301和302的区别

打开一个网站需要用户名密码,例如企业网站后台、Mysql客户端phpmyaadmin

auth_basic      提示
语法 auth_basic string|off
默认值 auth_basic off
使用位置http,server,location,limit_exceptauth_basic_user_file 密码文件
语法 auth_basic_user_file file
默认值-
使用位置:http,server,location,limit_except

用户认证需要注意参数

auth_basic_user_file 参数后接认证密码文件,格式为#comment
name1:password1
name2:password2:comment
name3:password3

auth_basic_user_file 参数后接认证密码文件,格式为

1、首先安装httpd模块

2、检查

[root@oldboy conf]# which /usr/bin/htpasswd
/usr/bin/htpasswd
[root@oldboy conf]# rpm -qf /usr/bin/htpasswd
httpd-tools-2.2.-.el6.centos..x86_64

3、配置配置文件

[root@oldboy extra]# cat bbs.conf
server {
listen ;
server_name bbs.cnblogs.co cnblog.co;
location / {
auth_basic "pyrene RZ";
auth_basic_user_file /application/nginx/conf/htpasswd;
root html/bbs;
index index.html index.htm;
}
access_log logs/www_access.log main;
}

这里的配置文件也可以自己设置

location /pyrene/{
auth_basic "pyrene RZ";
auth_basic_user_file /application/nginx/conf/htpasswd;
root html/bbs;
index index.html index.htm;
}}
这样就属于网站后台自己访问的时候需要

4、设置密码文件

[root@oldboy conf]# cd extra/
[root@oldboy extra]# htpasswd -cb /application/nginx/conf/htpasswd pyrene
Adding password for user pyrene
查看密码 这里密码是加密的
[root@oldboy conf]# chmod htpasswd ---给密码文件设置权限
[root@oldboy extra]# cat /application/nginx/conf/htpasswd
pyrene:slT12PdNWhfkc

5、重启

[root@oldboy bbs]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.8//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8//conf/nginx.conf test is successful
[root@oldboy bbs]# /application/nginx/sbin/nginx -s reload

示例nginx rewrite标签配置以及用户认证配置nginx rewrite标签配置以及用户认证配置

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894