整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

nginx如何重定向信息

nginx如何重定向信息

单且快速的 return

这是一个非常简单的设置方式,只需要个return语句就可以了

return 301 https://example.com$request_uri;

你需要把这段代码放到nginx配置文件的server代码块中,301是永久重定向,你也可以设置成302做一个临时重定向(不建议)。

一个完整的例子:

return 301 https://example.com$request_uri;

正则表达式 rewrite

如果return不能满足你的复杂业务需求,你可以考虑下正则匹配重定向:

rewrite ^/foo/(bar)/(.*)$ https://$server_name// permanent;

同样这也是需要在server代码块中,其中permanent为301永久跳转,若需要302可修改为redirect

一个完整的例子:

server {

listen 80;

listen [::]:80;

hostname example.com www.example.com;

root /var/www/example.com/public;

rewrite ^/foo/(bar)/(.*)$ $scheme://$server_name// permanent;

}

又如:

server {

listen 80;

server_name www.fangyongle.com fangyongle.cn;

if ($host !='www.fangyongle.com' ) {

rewrite ^/(.*)$ https://www.fangyongle.com/ permanent;

}

}

再如:

# 根据文件类型设置过期时间

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {

if (-f $request_filename) {

expires 1h;

break;

}

}

使用Maps

如果你有一堆需要重定向的连接映射,你可以考虑在一个地方定义它,然后再通过if来手动判断重定向。

首先定义重定向链接映射redirect-map.conf

map $request_uri $redirect_uri {

/about.html /about-us;

/customers.html /our-customers;

/products.html /our-products;

}

然后在server代码块使用:

include redirect-map.conf;

server {

[…]

if ( $redirect_uri ) {

return 301 $redirect_uri;

}

}

映射也可以有一些语法:

map $request_uri $redirect_uri {

/about.html /about-us;

/customers.html /our-customers;

/products.html /our-products;

# Match any url that ends in products.html or producs.htm

~products\.html?$ /our-products;

# case-insensitive version of the above

~*products\.html?$ /our-products;

# A named capture that maps

# e.g. product-1234.html into /products/item-1234/overview

~product-(?<sku>\d+)\.html /products/item-$sku/overview;

}

一些实用的重定向例子

http 重定向为 https

return 301 https://$host$request_uri;

统一规范域名

server_name example.com www.example.com example.net www.example.net _;

if ( $host !=$server_name ) {

return 301 $scheme://$server_name$request_uri;

}

含 www 和 不含 www 之间的重定向

# non-www to www

if ( $host !~ ^www\. ) {

return 301 $scheme://www.$host$request_uri;

}

# www to non-www

if ( $host ~ ^www\.(?<domain>.+)$ ) {

return 301 $scheme://$domain$request_uri;

}

附录

重定向中常用全局变量

$scheme // HTTP方法(如http,https),如:http

$host // 请求主机头字段,否则为服务器名称,如:blog.fangyongle.com

$server_name // 服务器名称,如:blog.fangyongle.com

$request_uri // 包含请求参数的原始URI,不包含主机名,如:/2018/81.html?a=1&b=2

$request_filename // 当前请求的文件的路径名,由root或alias和URI request组合而成,如:/2013/81.htmlnginx 部分常用全局变量

nginx 部分常用全局变量

$remote_addr//获取客户端ip

$binary_remote_addr//客户端ip(二进制)

$remote_port//客户端port,如:50472

$remote_user//已经经过Auth Basic Module验证的用户名

$host//请求主机头字段,否则为服务器名称,如:blog.fangyongle.com

$request//用户请求信息,如:GET ?a=1&b=2 HTTP/1.1

$request_filename//当前请求的文件的路径名,由root或alias和URI request组合而成,如:/2013/81.html

$status//请求的响应状态码,如:200

$body_bytes_sent // 响应时送出的body字节数数量。即使连接中断,这个数据也是精确的,如:40

$content_length // 等于请求行的“Content_Length”的值

$content_type // 等于请求行的“Content_Type”的值

$http_referer // 引用地址

$http_user_agent // 客户端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36

$args //与$query_string相同 等于当中URL的参数(GET),如a=1&b=2

$document_uri //与$uri相同 这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2018/81.html

$document_root //针对当前请求的根路径设置值

$hostname //如:centos53.localdomain

$http_cookie //客户端cookie信息

$cookie_COOKIE //cookie COOKIE变量的值

$is_args//如果有$args参数,这个变量等于”?”,否则等于”",空值,如?

$limit_rate//这个变量可以限制连接速率,0表示不限速

$query_string // 与$args相同 等于当中URL的参数(GET),如a=1&b=2

$request_body // 记录POST过来的数据信息

$request_body_file//客户端请求主体信息的临时文件名

$request_method //客户端请求的动作,通常为GET或POST,如:GET

$request_uri //包含请求参数的原始URI,不包含主机名,如:/2018/81.html?a=1&b=2

$scheme //HTTP方法(如http,https),如:http

$uri//这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2018/81.html

$request_completion//如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty),如:OK

$server_protocol//请求使用的协议,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1

$server_addr//服务器IP地址,在完成一次系统调用后可以确定这个值

$server_name//服务器名称,如:blog.fangyongle.com

$server_port//请求到达服务器的端口号,如:80

Rewrite正则相关指令详解

nginx的rewrite相当于apache的rewriterule(大多数情况下可以把原有apache的rewrite规则加上引号就可以直接使用),它可以用在server,location和IF条件判断块中,命令格式如下:

rewrite <regex> <replacement> <flag>

正则表达式匹配

  • ~为区分大小写匹配
  • ~*为不区分大小写匹配
  • !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

文件及目录匹配判断

  • -f和!-f用来判断是否存在文件
  • -d和!-d用来判断是否存在目录
  • -e和!-e用来判断是否存在文件或目录
  • -x和!-x用来判断文件是否可执行

flag标记

  • last - 基本上都用这个Flag。
  • break - 中止rewirte,不在继续匹配
  • redirect - 返回临时重定向的HTTP状态302
  • permanent - 返回永久重定向的HTTP状态301

使用last和break实现URI重写,浏览器地址栏不变。而且两者有细微差别:

  • 使用alias指令必须用last标记
  • 使用proxy_pass指令时,需要使用break标记
  • last标记在本条rewrite规则执行完毕后,会对其所在server{......}标签重新发起请求,而break标记则在本条规则匹配完成后,终止匹配。

、定义

<meta> 标签提供关于 HTML 文档的元数据。它不会显示在页面上,但是对于机器是可读的。可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 web 服务。

2、作用

meta里的数据是供机器解读的,告诉机器该如何解析这个页面,还有一个用途是可以添加服务器发送到浏览器的http头部内容,例如我们为页面中添加如下meta标签:


  1. <meta http-equiv="charset" content="iso-8859-1">
  2. <meta http-equiv="expires" content="31 Dec 2008">

浏览器的头部就会包括这些:


  1. charset:iso-8859-1
  2. expires:31 Dec 2008

只有浏览器可以接受这些附加的头部字段,并能以适当的方式使用它们时,这些字段才有意义。

3、meta的必需属性和可选属性

meta的必需属性是content,当然并不是说meta标签里一定要有content,而是当有http-equiv或name属性的时候,一定要有content属性对其进行说明。例如:

必需属性

<meta name="keywords" content="HTML,ASP,PHP,SQL">

这里面content里的属性就是对keywords进行的说明,所以呢也可以理解成一个键值对吧,就是{keywords:"HTML,ASP,PHP,SQL"}。

可选属性

在W3school中,对于meta的可选属性说到了三个,分别是http-equiv、name和scheme。考虑到scheme不是很常用,所以就只说下前两个属性吧。

http-equiv

http-equiv属性是添加http头部内容,对一些自定义的,或者需要额外添加的http头部内容,需要发送到浏览器中,我们就可以是使用这个属性。在上面的meta作用中也有简单的说明,那么现在再举个例子。例如我们不想使用js来重定向,用http头部内容控制,那么就可以这样控制:

<meta http-equiv="Refresh" content="5;url=http://blog.yangchen123h.cn" />

在页面中加入这个后,5秒钟后就会跳转到指定页面啦,效果可看W3school的例子

name

第二个可选属性是name,这个属性是供浏览器进行解析,对于一些浏览器兼容性问题,name属性是最常用的,当然有个前提就是浏览器能够解析你写进去的name属性才可以,不然就是没有意义的。还是举个例子吧:

<meta name="renderer" content="webkit">

这个meta标签的意思就是告诉浏览器,用webkit内核进行解析,当然前提是浏览器有webkit内核才可以,不然就是没有意义的啦。当然看到这个你可能会有疑问,这个renderer是从哪里冒出来的,我要怎么知道呢?这个就是在对应的浏览器的开发文档里就会有表明的,例如这个renderer是在360浏览器里说明的。360浏览器内核控制Meta标签说明文档

常用meta标签大总结

接下来就是常用的meta标签大总结啦,我会尽可能的做到全

charset

charset是声明文档使用的字符编码,解决乱码问题主要用的就是它,值得一提的是,这个charset一定要写第一行,不然就可能会产生乱码了。

charset有两种写法


  1. <meta charset="utf-8">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

两个都是等效的。

百度禁止转码

百度会自动对网页进行转码,这个标签是禁止百度的自动转码

<meta http-equiv="Cache-Control" content="no-siteapp" />

SEO 优化部分


  1. <!-- 页面标题<title>标签(head 头部必须) -->
  2. <title>your title</title>
  3. <!-- 页面关键词 keywords -->
  4. <meta name="keywords" content="your keywords">
  5. <!-- 页面描述内容 description -->
  6. <meta name="description" content="your description">
  7. <!-- 定义网页作者 author -->
  8. <meta name="author" content="author,email address">
  9. <!-- 定义网页搜索引擎索引方式,robotterms 是一组使用英文逗号「,」分割的值,通常有如下几种取值:none,noindex,nofollow,all,index和follow。 -->
  10. <meta name="robots" content="index,follow">

viewport

viewport主要是影响移动端页面布局的,例如:


  1. <meta name="viewport" content="width=device-width, initial-scale=1.0">

content 参数:

  • width viewport 宽度(数值/device-width)
  • height viewport 高度(数值/device-height)
  • initial-scale 初始缩放比例
  • maximum-scale 最大缩放比例
  • minimum-scale 最小缩放比例
  • user-scalable 是否允许用户缩放(yes/no)

各浏览器平台

Microsoft Internet Explorer


  1. <!-- 优先使用最新的ie版本 -->
  2. <meta http-equiv="x-ua-compatible" content="ie=edge">
  3. <!-- 是否开启cleartype显示效果 -->
  4. <meta http-equiv="cleartype" content="on">
  5. <meta name="skype_toolbar" content="skype_toolbar_parser_compatible">
  6. <!-- Pinned Site -->
  7. <!-- IE 10 / Windows 8 -->
  8. <meta name="msapplication-TileImage" content="pinned-tile-144.png">
  9. <meta name="msapplication-TileColor" content="#009900">
  10. <!-- IE 11 / Windows 9.1 -->
  11. <meta name="msapplication-config" content="ieconfig.xml">

Google Chrome


  1. <!-- 优先使用最新的chrome版本 -->
  2. <meta http-equiv="X-UA-Compatible" content="chrome=1" />
  3. <!-- 禁止自动翻译 -->
  4. <meta name="google" value="notranslate">

360浏览器


  1. <!-- 选择使用的浏览器解析内核 -->
  2. <meta name="renderer" content="webkit|ie-comp|ie-stand">

UC手机浏览器

UCBrowser_U3_API

QQ手机浏览器


  1. <!-- 锁定屏幕在特定方向 -->
  2. <meta name="x5-orientation" content="landscape/portrait">
  3. <!-- 全屏显示 -->
  4. <meta name="x5-fullscreen" content="true">
  5. <!-- 页面将以应用模式显示 -->
  6. <meta name="x5-page-mode" content="app">

Apple iOS


  1. <!-- Smart App Banner -->
  2. <meta name="apple-itunes-app" content="app-id=APP_ID,affiliate-data=AFFILIATE_ID,app-argument=SOME_TEXT">
  3. <!-- 禁止自动探测并格式化手机号码 -->
  4. <meta name="format-detection" content="telephone=no">
  5. <!-- Add to Home Screen添加到主屏 -->
  6. <!-- 是否启用 WebApp 全屏模式 -->
  7. <meta name="apple-mobile-web-app-capable" content="yes">
  8. <!-- 设置状态栏的背景颜色,只有在 “apple-mobile-web-app-capable” content=”yes” 时生效 -->
  9. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  10. <!-- 添加到主屏后的标题 -->
  11. <meta name="apple-mobile-web-app-title" content="App Title">

Google Android


  1. <meta name="theme-color" content="#E64545">
  2. <!-- 添加到主屏 -->
  3. <meta name="mobile-web-app-capable" content="yes">
  4. <!-- More info: https://developer.chrome.com/multidevice/android/installtohomescreen -->

App Links


  1. <!-- iOS -->
  2. <meta property="al:ios:url" content="applinks://docs">
  3. <meta property="al:ios:app_store_id" content="12345">
  4. <meta property="al:ios:app_name" content="App Links">
  5. <!-- Android -->
  6. <meta property="al:android:url" content="applinks://docs">
  7. <meta property="al:android:app_name" content="App Links">
  8. <meta property="al:android:package" content="org.applinks">
  9. <!-- Web Fallback -->
  10. <meta property="al:web:url" content="http://applinks.org/documentation">
  11. <!-- More info: http://applinks.org/documentation/ -->

最后——移动端常用的meta


  1. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
  2. <meta name="apple-mobile-web-app-capable" content="yes" />
  3. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  4. <meta name="format-detection"content="telephone=no, email=no" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
  6. <meta name="apple-mobile-web-app-capable" content="yes" /><!-- 删除苹果默认的工具栏和菜单栏 -->
  7. <meta name="apple-mobile-web-app-status-bar-style" content="black" /><!-- 设置苹果工具栏颜色 -->
  8. <meta name="format-detection" content="telphone=no, email=no" /><!-- 忽略页面中的数字识别为电话,忽略email识别 -->
  9. <!-- 启用360浏览器的极速模式(webkit) -->
  10. <meta name="renderer" content="webkit">
  11. <!-- 避免IE使用兼容模式 -->
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13. <!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->
  14. <meta name="HandheldFriendly" content="true">
  15. <!-- 微软的老式浏览器 -->
  16. <meta name="MobileOptimized" content="320">
  17. <!-- uc强制竖屏 -->
  18. <meta name="screen-orientation" content="portrait">
  19. <!-- QQ强制竖屏 -->
  20. <meta name="x5-orientation" content="portrait">
  21. <!-- UC强制全屏 -->
  22. <meta name="full-screen" content="yes">
  23. <!-- QQ强制全屏 -->
  24. <meta name="x5-fullscreen" content="true">
  25. <!-- UC应用模式 -->
  26. <meta name="browsermode" content="application">
  27. <!-- QQ应用模式 -->
  28. <meta name="x5-page-mode" content="app">
  29. <!-- windows phone 点击无高光 -->
  30. <meta name="msapplication-tap-highlight" content="no">
  31. <!-- 适应移动端end -->

求转发

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  // 设置请求和响应的字符集
  req.setCharacterEncoding("UTF-8");
  resp.setContentType("text/html;charset=UTF-8");

  System.out.println("AServlet ----> BServlet");

  // 请求转发
  req.getRequestDispatcher("response.html").forward(req, resp);
}
  • 请求转发的特点

从一个Servlet跳转到另一个Servlet/JSP/HTML;

浏览器地址栏不会发生改变,只发一次请求;

请求转发是服务器内部行为;

请求转发使用的是同一个请求和响应对象(设置共享资源,涉及到域对象);

    • 请求转发到html文件会出现乱码,问题不在浏览器和服务器编码/解码
      • 解决方法: 需配置web.xml
<jsp-config>
  <jsp-property-group>
  <url-pattern>*.html</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>

请求重定向

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  // 设置请求和响应的字符集
  req.setCharacterEncoding("UTF-8");
  resp.setContentType("text/html;charset=UTF-8");

  System.out.println("RedirectAServlet ----> RedirectBServlet");

  // 重定向到BServlet
  resp.sendRedirect("bb");
}
  • 重定向特点

一个资源跳转到另一个资源,会发送新的请求;

一个Servlet跳转到另一个Servlet/JSP/HTML;

浏览器地址栏会发生改变,发送多个请求;

重定向是客户端(浏览器)的行为;

重定向使用的不是同一个请求对象和响应对象;

每次发送请求都会创建新的请求和响应对象;

转发和重定向的区别