整合營銷服務(wù)商

          電腦端+手機端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          nginx如何重定向信息

          nginx如何重定向信息

          單且快速的 return

          這是一個非常簡單的設(shè)置方式,只需要個return語句就可以了

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

          你需要把這段代碼放到nginx配置文件的server代碼塊中,301是永久重定向,你也可以設(shè)置成302做一個臨時重定向(不建議)。

          一個完整的例子:

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

          正則表達式 rewrite

          如果return不能滿足你的復(fù)雜業(yè)務(wù)需求,你可以考慮下正則匹配重定向:

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

          同樣這也是需要在server代碼塊中,其中permanent為301永久跳轉(zhuǎn),若需要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;

          }

          }

          再如:

          # 根據(jù)文件類型設(shè)置過期時間

          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;

          統(tǒng)一規(guī)范域名

          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 // 請求主機頭字段,否則為服務(wù)器名稱,如:blog.fangyongle.com

          $server_name // 服務(wù)器名稱,如:blog.fangyongle.com

          $request_uri // 包含請求參數(shù)的原始URI,不包含主機名,如:/2018/81.html?a=1&b=2

          $request_filename // 當(dāng)前請求的文件的路徑名,由root或alias和URI request組合而成,如:/2013/81.htmlnginx 部分常用全局變量

          nginx 部分常用全局變量

          $remote_addr//獲取客戶端ip

          $binary_remote_addr//客戶端ip(二進制)

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

          $remote_user//已經(jīng)經(jīng)過Auth Basic Module驗證的用戶名

          $host//請求主機頭字段,否則為服務(wù)器名稱,如:blog.fangyongle.com

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

          $request_filename//當(dāng)前請求的文件的路徑名,由root或alias和URI request組合而成,如:/2013/81.html

          $status//請求的響應(yīng)狀態(tài)碼,如:200

          $body_bytes_sent // 響應(yīng)時送出的body字節(jié)數(shù)數(shù)量。即使連接中斷,這個數(shù)據(jù)也是精確的,如: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相同 等于當(dāng)中URL的參數(shù)(GET),如a=1&b=2

          $document_uri //與$uri相同 這個變量指當(dāng)前的請求URI,不包括任何參數(shù)(見$args) 如:/2018/81.html

          $document_root //針對當(dāng)前請求的根路徑設(shè)置值

          $hostname //如:centos53.localdomain

          $http_cookie //客戶端cookie信息

          $cookie_COOKIE //cookie COOKIE變量的值

          $is_args//如果有$args參數(shù),這個變量等于”?”,否則等于”",空值,如?

          $limit_rate//這個變量可以限制連接速率,0表示不限速

          $query_string // 與$args相同 等于當(dāng)中URL的參數(shù)(GET),如a=1&b=2

          $request_body // 記錄POST過來的數(shù)據(jù)信息

          $request_body_file//客戶端請求主體信息的臨時文件名

          $request_method //客戶端請求的動作,通常為GET或POST,如:GET

          $request_uri //包含請求參數(shù)的原始URI,不包含主機名,如:/2018/81.html?a=1&b=2

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

          $uri//這個變量指當(dāng)前的請求URI,不包括任何參數(shù)(見$args) 如:/2018/81.html

          $request_completion//如果請求結(jié)束,設(shè)置為OK. 當(dāng)請求未結(jié)束或如果該請求不是請求鏈串的最后一個時,為空(Empty),如:OK

          $server_protocol//請求使用的協(xié)議,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1

          $server_addr//服務(wù)器IP地址,在完成一次系統(tǒng)調(diào)用后可以確定這個值

          $server_name//服務(wù)器名稱,如:blog.fangyongle.com

          $server_port//請求到達服務(wù)器的端口號,如:80

          Rewrite正則相關(guān)指令詳解

          nginx的rewrite相當(dāng)于apache的rewriterule(大多數(shù)情況下可以把原有apache的rewrite規(guī)則加上引號就可以直接使用),它可以用在server,location和IF條件判斷塊中,命令格式如下:

          rewrite <regex> <replacement> <flag>

          正則表達式匹配

          • ~為區(qū)分大小寫匹配
          • ~*為不區(qū)分大小寫匹配
          • !~和!~*分別為區(qū)分大小寫不匹配及不區(qū)分大小寫不匹配

          文件及目錄匹配判斷

          • -f和!-f用來判斷是否存在文件
          • -d和!-d用來判斷是否存在目錄
          • -e和!-e用來判斷是否存在文件或目錄
          • -x和!-x用來判斷文件是否可執(zhí)行

          flag標(biāo)記

          • last - 基本上都用這個Flag。
          • break - 中止rewirte,不在繼續(xù)匹配
          • redirect - 返回臨時重定向的HTTP狀態(tài)302
          • permanent - 返回永久重定向的HTTP狀態(tài)301

          使用last和break實現(xiàn)URI重寫,瀏覽器地址欄不變。而且兩者有細微差別:

          • 使用alias指令必須用last標(biāo)記
          • 使用proxy_pass指令時,需要使用break標(biāo)記
          • last標(biāo)記在本條rewrite規(guī)則執(zhí)行完畢后,會對其所在server{......}標(biāo)簽重新發(fā)起請求,而break標(biāo)記則在本條規(guī)則匹配完成后,終止匹配。

          、定義

          <meta> 標(biāo)簽提供關(guān)于 HTML 文檔的元數(shù)據(jù)。它不會顯示在頁面上,但是對于機器是可讀的??捎糜跒g覽器(如何顯示內(nèi)容或重新加載頁面),搜索引擎(關(guān)鍵詞),或其他 web 服務(wù)。

          2、作用

          meta里的數(shù)據(jù)是供機器解讀的,告訴機器該如何解析這個頁面,還有一個用途是可以添加服務(wù)器發(fā)送到瀏覽器的http頭部內(nèi)容,例如我們?yōu)轫撁嬷刑砑尤缦耺eta標(biāo)簽:


          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

          只有瀏覽器可以接受這些附加的頭部字段,并能以適當(dāng)?shù)姆绞绞褂盟鼈儠r,這些字段才有意義。

          3、meta的必需屬性和可選屬性

          meta的必需屬性是content,當(dāng)然并不是說meta標(biāo)簽里一定要有content,而是當(dāng)有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頭部內(nèi)容,對一些自定義的,或者需要額外添加的http頭部內(nèi)容,需要發(fā)送到瀏覽器中,我們就可以是使用這個屬性。在上面的meta作用中也有簡單的說明,那么現(xiàn)在再舉個例子。例如我們不想使用js來重定向,用http頭部內(nèi)容控制,那么就可以這樣控制:

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

          在頁面中加入這個后,5秒鐘后就會跳轉(zhuǎn)到指定頁面啦,效果可看W3school的例子

          name

          第二個可選屬性是name,這個屬性是供瀏覽器進行解析,對于一些瀏覽器兼容性問題,name屬性是最常用的,當(dāng)然有個前提就是瀏覽器能夠解析你寫進去的name屬性才可以,不然就是沒有意義的。還是舉個例子吧:

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

          這個meta標(biāo)簽的意思就是告訴瀏覽器,用webkit內(nèi)核進行解析,當(dāng)然前提是瀏覽器有webkit內(nèi)核才可以,不然就是沒有意義的啦。當(dāng)然看到這個你可能會有疑問,這個renderer是從哪里冒出來的,我要怎么知道呢?這個就是在對應(yīng)的瀏覽器的開發(fā)文檔里就會有表明的,例如這個renderer是在360瀏覽器里說明的。360瀏覽器內(nèi)核控制Meta標(biāo)簽說明文檔

          常用meta標(biāo)簽大總結(jié)

          接下來就是常用的meta標(biāo)簽大總結(jié)啦,我會盡可能的做到全

          charset

          charset是聲明文檔使用的字符編碼,解決亂碼問題主要用的就是它,值得一提的是,這個charset一定要寫第一行,不然就可能會產(chǎn)生亂碼了。

          charset有兩種寫法


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

          兩個都是等效的。

          百度禁止轉(zhuǎn)碼

          百度會自動對網(wǎng)頁進行轉(zhuǎn)碼,這個標(biāo)簽是禁止百度的自動轉(zhuǎn)碼

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

          SEO 優(yōu)化部分


          1. <!-- 頁面標(biāo)題<title>標(biāo)簽(head 頭部必須) -->
          2. <title>your title</title>
          3. <!-- 頁面關(guān)鍵詞 keywords -->
          4. <meta name="keywords" content="your keywords">
          5. <!-- 頁面描述內(nèi)容 description -->
          6. <meta name="description" content="your description">
          7. <!-- 定義網(wǎng)頁作者 author -->
          8. <meta name="author" content="author,email address">
          9. <!-- 定義網(wǎng)頁搜索引擎索引方式,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 參數(shù):

          • width viewport 寬度(數(shù)值/device-width)
          • height viewport 高度(數(shù)值/device-height)
          • initial-scale 初始縮放比例
          • maximum-scale 最大縮放比例
          • minimum-scale 最小縮放比例
          • user-scalable 是否允許用戶縮放(yes/no)

          各瀏覽器平臺

          Microsoft Internet Explorer


          1. <!-- 優(yōu)先使用最新的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. <!-- 優(yōu)先使用最新的chrome版本 -->
          2. <meta http-equiv="X-UA-Compatible" content="chrome=1" />
          3. <!-- 禁止自動翻譯 -->
          4. <meta name="google" value="notranslate">

          360瀏覽器


          1. <!-- 選擇使用的瀏覽器解析內(nèi)核 -->
          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. <!-- 頁面將以應(yīng)用模式顯示 -->
          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. <!-- 設(shè)置狀態(tài)欄的背景顏色,只有在 “apple-mobile-web-app-capable” content=”yes” 時生效 -->
          9. <meta name="apple-mobile-web-app-status-bar-style" content="black">
          10. <!-- 添加到主屏后的標(biāo)題 -->
          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" /><!-- 設(shè)置蘋果工具欄顏色 -->
          8. <meta name="format-detection" content="telphone=no, email=no" /><!-- 忽略頁面中的數(shù)字識別為電話,忽略email識別 -->
          9. <!-- 啟用360瀏覽器的極速模式(webkit) -->
          10. <meta name="renderer" content="webkit">
          11. <!-- 避免IE使用兼容模式 -->
          12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
          13. <!-- 針對手持設(shè)備優(yōu)化,主要是針對一些老的不識別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應(yīng)用模式 -->
          26. <meta name="browsermode" content="application">
          27. <!-- QQ應(yīng)用模式 -->
          28. <meta name="x5-page-mode" content="app">
          29. <!-- windows phone 點擊無高光 -->
          30. <meta name="msapplication-tap-highlight" content="no">
          31. <!-- 適應(yīng)移動端end -->

          求轉(zhuǎn)發(fā)

          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 設(shè)置請求和響應(yīng)的字符集
            req.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=UTF-8");
          
            System.out.println("AServlet ----> BServlet");
          
            // 請求轉(zhuǎn)發(fā)
            req.getRequestDispatcher("response.html").forward(req, resp);
          }
          • 請求轉(zhuǎn)發(fā)的特點

          從一個Servlet跳轉(zhuǎn)到另一個Servlet/JSP/HTML;

          瀏覽器地址欄不會發(fā)生改變,只發(fā)一次請求;

          請求轉(zhuǎn)發(fā)是服務(wù)器內(nèi)部行為;

          請求轉(zhuǎn)發(fā)使用的是同一個請求和響應(yīng)對象(設(shè)置共享資源,涉及到域?qū)ο?;

            • 請求轉(zhuǎn)發(fā)到html文件會出現(xiàn)亂碼,問題不在瀏覽器和服務(wù)器編碼/解碼
              • 解決方法: 需配置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 {
            // 設(shè)置請求和響應(yīng)的字符集
            req.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=UTF-8");
          
            System.out.println("RedirectAServlet ----> RedirectBServlet");
          
            // 重定向到BServlet
            resp.sendRedirect("bb");
          }
          • 重定向特點

          一個資源跳轉(zhuǎn)到另一個資源,會發(fā)送新的請求;

          一個Servlet跳轉(zhuǎn)到另一個Servlet/JSP/HTML;

          瀏覽器地址欄會發(fā)生改變,發(fā)送多個請求;

          重定向是客戶端(瀏覽器)的行為;

          重定向使用的不是同一個請求對象和響應(yīng)對象;

          每次發(fā)送請求都會創(chuàng)建新的請求和響應(yīng)對象;

          轉(zhuǎn)發(fā)和重定向的區(qū)別


          主站蜘蛛池模板: 风间由美在线亚洲一区| 国产精品亚洲一区二区三区| 国产精品男男视频一区二区三区 | 青青青国产精品一区二区| 国产人妖视频一区在线观看| 一区二区三区免费看| 精品无码人妻一区二区三区不卡| 成人午夜视频精品一区| 亚洲一区无码中文字幕| 亚洲日本乱码一区二区在线二产线 | 国产精品久久久久久一区二区三区| 日本精品无码一区二区三区久久久 | 在线观看中文字幕一区| 一区视频在线播放| 久久精品国产一区| 亚洲色无码专区一区| 国产三级一区二区三区| 精品一区二区高清在线观看| 国产一区二区在线视频播放| 中文字幕一区二区三区有限公司| 色一乱一伦一图一区二区精品 | 熟妇人妻AV无码一区二区三区| 精品人妻无码一区二区色欲产成人 | 中文人妻无码一区二区三区| 亚洲一区二区久久| 中文字幕日韩人妻不卡一区| 在线观看一区二区三区av| 国产乱码精品一区二区三| 色狠狠一区二区三区香蕉| 精品一区二区三区3d动漫| 日韩精品无码一区二区三区四区| 国模私拍福利一区二区| 好吊视频一区二区三区| 亚洲欧美一区二区三区日产| 亚洲一区二区三区香蕉| 亚洲福利视频一区二区| 国产精品久久久久一区二区三区 | 97久久精品一区二区三区| 亚洲AV综合色区无码一区| 国产一区二区三区高清视频 | 午夜视频在线观看一区二区|