hi,欢迎访问本站!
当前位置: 首页学习笔记正文

Nginx重定向配置

用户投稿 学习笔记 21阅读
简介

重定向(Redirect)就是通过各种方法将各种网络请求重新定个方向转到其它位置 我们在网站建设中,时常会遇到需要网页重定向的情况: 1.网站调整(如改变网页目录结构); 2.网页被移到一个新地址; 3.网页扩展名改变(如应用需要把.php改成.Html或.shtml)。 这种情况下,如果不做重定向,则用户收藏夹或搜索引擎数据库中旧地址只能让访问客户得到一个404页面错误信息,访问流量白白丧失;再者某些注册了多个域名的网站,也需要通过重定向让访问这些域名的用户自动跳转到主站点等常用的重定向方式 301 redirect:301代表永久性转移(Permanently Moved),301重定向是网页更改地址后对搜索引擎友好的最好方法,只要不是暂时搬移的情况,都建议使用301来做转址 302 redirect:302代表暂时性转移(Temporarily Moved ),在前些年,不少Black Hat SEO(黑帽SEO)曾广泛应用这项技术作弊,目前,各大主要搜索引擎均加强了打击力度,像Google前些年对域名之王(Business)以及近来对BMW德国网站的惩罚。即使网站客观上不是spam,也很容易被搜寻引擎容易误判为spam而遭到惩罚 meta fresh:这在2000年前比较流行,不过现在已很少见。其具体是通过网页中的meta指令,在特定时间后重定向到新的网页,如果延迟的时间太短(约5秒之内),会被判断为spam

常用参数 $args:GET请求的参数$binary_remote_addr:二进制的客户地址$body_bytes_sent:响应时送出的body字节数数量$content_length:请求头中的Content-length字段$content_type:请求头中的Content-Type字段$cookie_COOKIE:cookie COOKIE变量的值$document_root:当前请求在root指令中指定的值$host:请求主机头字段,否则为服务器名称$is_args:如果有$args参数,这个变量等于”?”,否则等于”",空值。$http_user_agent:客户端agent信息$http_cookie:客户端cookie信息$limit_rate:这个变量可以限制连接速率$request_body_file:客户端请求主体信息的临时文件名$request_method:客户端请求的动作$remote_addr:客户端的IP地址$remote_port:客户端的端口$remote_user:客户端用户名$request_completion:请求是否结束的标志值为OK或空(Empty)$request_filename:当前请求的文件路径,由root或alias指令与URI请求生成。$request_uri:包含请求参数的原始URI,不包含主机名$scheme:HTTP方法(如http,https)。$server_protocol:请求使用的协议,通常是HTTP/1.0或HTTP/1.1。$server_addr:服务器地址$server_name:服务器名称$server_port:请求到达服务器的端口号$uri :不带请求参数的当前URI,$uri不包含主机名$request_uri:浏览器发过来的值 标记 break:完成当前设置的规则,停止执行其他的重写指令last:完成重写指令permanent:返回301永久重定向redirect 返回302临时重定向 正则表达式匹配 ~:区分大小写匹配~*:不区分大小写匹配!~:区分大小写不匹配!~*:不区分大小写不匹配 文件与目录匹配参数 -f和!-f:判断是否存在文件-d和!-d:判断是否存在目录-e和!-e:判断是否存在文件或目录-x和!-x:判断文件是否可执行 应用

从http://localhost/abc/a/1.html跳转至http://localhost/ccc/bbb /2.html

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } location /abc { root /usr/share/nginx/html; index index.html index.htm index.php; rewrite .* /ccc/bbb/2.html permanent; }}

从http://localhost/abc/a/1.html跳转到http://localhost/abc/b/1.html

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } location /abc { root /usr/share/nginx/html; index index.html index.htm index.php; #$1表示括号中正则表达式匹配到的内容 rewrite ^/abc/a/(.*)$ /abc/b/$1 permanent; }}

从http://www.haha.com跳转至http://remote_ip

#绑定IP地址和域名[root@localhost ~]#vi /etc/hostslocalhost www.haha.comserver { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; #~*表示不区分大小写匹配 if ( $host ~* haha.com ){ rewrite .* http://remote_ip; break; } } }

从http://localhost/1.html跳转至http://remote_ip/1.html

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; if ( $host ~* haha.com ){ rewrite .* http://remote_ip$request_uri; break; } } }

从http://localhost/abc/11-22-33/1.html跳转至http://localhost/abc/11/22/33/1.html

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; #$1...$4表示括号中正则表达式匹配到的内容 rewrite ^/abc/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /abc/$1/$2/$3/$4 permanent; } }

从http://www.abc.haha.com跳转至http://www.haha.com/abc

#绑定IP地址和域名[root@localhost ~]#vi /etc/hostslocalhost www.haha.comlocalhost www.abc.haha.comserver { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; #避免死循环 if ( $host ~* "^www.haha.com$" ){ break; } if ( $host ~* "www.(.*)\.haha\.com" ){ set $user $1; rewrite .* http://www.haha.com/$user permanent; } }}

将请求访问脚本重定向到错误页面

server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } location ~* \.sh$ { return 403;}}

将http://localhost跳转为https://localhost

#方法一server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; return 301 https://localhost; }}#方法二server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; rewrite (.*) https://localhost permanent; }}

注:对Apache进行HTTPS重定向设置

RewriteEngine OnRewriteRule ^(.*)$ https://localhost$1
标签:
声明:无特别说明,转载请标明本文来源!
发布评论
正文 取消