{"id":421216,"date":"2021-04-01T23:37:31","date_gmt":"2021-04-01T15:37:31","guid":{"rendered":"http:\/\/4563.org\/?p=421216"},"modified":"2021-04-01T23:37:31","modified_gmt":"2021-04-01T15:37:31","slug":"nginx-%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86-js-%e5%88%b0-js-gz%ef%bc%8c-%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e6%ad%a3%e7%a1%ae%e7%9a%84%e5%93%8d%e5%ba%94%e5%a4%b4%ef%bc%9f-%e9%9d%9e%e9%9d%99%e6%80%81","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=421216","title":{"rendered":"nginx \u53cd\u5411\u4ee3\u7406 .js \u5230 .js.gz\uff0c \u5982\u4f55\u8fd4\u56de\u6b63\u786e\u7684\u54cd\u5e94\u5934\uff1f \u975e\u9759\u6001\u6587\u4ef6\uff0c \u4f7f\u7528 proxy_pass"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  nginx \u53cd\u5411\u4ee3\u7406 .js \u5230 .js.gz\uff0c \u5982\u4f55\u8fd4\u56de\u6b63\u786e\u7684\u54cd\u5e94\u5934\uff1f \u975e\u9759\u6001\u6587\u4ef6\uff0c \u4f7f\u7528 proxy_pass               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : skypyb <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4f7f\u7528 <code>rewrite ^(.*)$ \/$1.gz break;<\/code> \u6765\u91cd\u5199\u8bf7\u6c42\u3002 \u53ef\u4ee5\u6b63\u5e38\u8bf7\u6c42\u5e76\u8fd4\u56de\u3002<br \/> \u53ef\u5c31\u662f\u8fd4\u56de\u7684\u4e1c\u897f\u6d4f\u89c8\u5668\u89e3\u6790\u4e0d\u4e86\u3002 <\/p>\n<p>\u56e0\u4e3a content-type \u503c\u4f1a\u4e3a application\/gzip, \u6211\u60f3\u8ba9\u4ed6\u53d8\u4e3a\u6211\u60f3\u8981\u7684\uff0c \u8bd5\u8fc7\u4e86\u5404\u79cd\u65b9\u6cd5\u90fd\u6ca1\u7528.. \u5c31\u5f88\u96be\u53d7\u3002<\/p>\n<p>\u6298\u817e\u597d\u4e45\uff0c \u4e0d\u77e5\u9053\u600e\u4e48\u641e\u4e86\uff0cNginx gzip_static \u6a21\u5757\u4f3c\u4e4e\u53ea\u80fd\u5bf9\u9759\u6001\u6587\u4ef6\u751f\u6548\uff0c \u6211\u8fd9\u573a\u666f\u662f\u8bbf\u95ee\u5bf9\u8c61\u50a8\u5b58\u91cc\u8fb9\u7684\u8d44\u6e90\u3002<\/p>\n<pre><code>location ~* ^.+.(css|js)$ {   rewrite ^(.*)$ \/$1.gz break;                   #\u4e0b\u9762\u662f\u8bd5\u8fc7\u7684\u4e1c\u897f, \u5168\u90fd\u6ca1\u7528  #gzip_static on;   #default_type application\/javascript;   #types {  #       application\/javascript gz;  #}                   #add_header content-type application\/javascript   proxy_pass https:\/\/cos;  proxy_redirect default;   #......  } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>11<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5614048\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ETiV <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             curl -v \u770b\u4e00\u4e0b\uff0c\u662f\u4e0d\u662f\u591a\u53d1\u4e86 content-type \u5934<\/p>\n<p>\u4e0d\u8fc7\u4e3a\u5565\u8981\u8fd9\u4e48\u505a\u5462\u2026\u2026\u611f\u89c9\u662f\u4e2a X-Y problem                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614049\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : skypyb <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ETiV \u5176\u5b9e\u5c31\u662f\u9759\u6001\u7f51\u7ad9\u8fdb\u884c\u4e86\u4e00\u9053 gz \u540e\u653e\u5728\u5bf9\u8c61\u50a8\u5b58\u91cc\uff0c \u7136\u540e\u60f3\u901a\u8fc7\u670d\u52a1\u5668\u53cd\u4ee3\u4e00\u4e0b\u3002<br \/>\u8bd5\u4e86\u6b63\u5e38\u8bbf\u95ee\u5f88\u6b63\u5e38\u3002<br \/>\u6211\u60f3\u8bbf\u95ee.gz \u540e\u7f00\u7684\u6587\u4ef6 (\u5b58\u5728) \u5e76\u8fd4\u56de\u6b63\u786e\u7684 conntent-type \u548c content-encoding\uff0c \u7528 Nginx \u6ca1\u627e\u5230\u600e\u4e48\u5f04..<\/p>\n<p>\u4e4b\u6240\u4ee5\u8981\u8fd9\u6837\u5f04\uff0c \u4e3b\u8981\u662f\u4e0d\u60f3\u6253\u5305\u540e\u8fd8\u8981\u53bb\u624b\u52a8\u4e0a\u4f20\u5230\u670d\u52a1\u5668\uff0c \u76f4\u63a5 serverless \u4e00\u884c\u547d\u4ee4\u4f20\u5230\u5bf9\u8c61\u50a8\u5b58\u91cc\u5c31\u5f88\u65b9\u4fbf                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614050\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Firxiao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Nginx \u53ea\u662f\u505a\u4e86\u900f\u4f20\uff0c\u5982\u679c\u6ca1\u6709\u76f8\u5173\u914d\u7f6e\uff0c\u5e76\u4e0d\u4f1a\u6539\u53d8\u540e\u7aef\u670d\u52a1\u5668\u4f20\u56de\u6765\u7684 header \u3002<br \/>\u5efa\u8bae<br \/>1\uff0c\u76f4\u63a5\u8bbf\u95ee\u540e\u7aef\u770b header \u662f\u5426\u6b63\u5e38<br \/>2\uff0c\u68c0\u67e5\u5176\u4ed6\u5730\u65b9\u662f\u4e0d\u662f\u6709\u9488\u5bf9 js \u7684 gzip \u914d\u7f6e<br \/>3\uff0c\u53ef\u4ee5\u5c1d\u8bd5 proxy_set_header                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614051\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : eason1874 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60 rewrite \u4e0b\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u6ca1\u6709\u673a\u4f1a\u6267\u884c\u4e86\uff0c\u600e\u4e48\u6539\u90fd\u662f\u5f92\u52b3\uff0c\u56e0\u4e3a\u4f60\u7684 rewrite \u5df2\u7ecf\u5168\u91cf\u91cd\u5199\u5230 .gz \u540e\u7f00\uff0c\u800c\u4f60\u8fd9\u4e2a location \u53ea\u5339\u914d .css \u548c .js \u540e\u7f00\u3002<\/p>\n<p>\u4f7f\u7528\u672c\u5730\u7684\u9884\u538b\u7f29\u6587\u4ef6\u5f88\u7b80\u5355\u3002\u8fd9\u6837\u914d\u7f6e\uff0c\u8bbf\u95ee \/test.css \u7684\u65f6\u5019 Nginx \u4f1a\u5148\u53bb\u627e test.css.gz \uff0c\u5982\u679c\u6709\u5c31\u8fd4\u56de\uff0c\u65e0\u5219\u627e test.css \u8fd4\u56de\u3002<\/p>\n<p>location = \/test.css {<br \/>gzip_static on;<br \/>gunzip on;<br \/>}<\/p>\n<p>\u4f46\u662f\u53cd\u4ee3\u8fd9\u6837\u914d\u7f6e\u5c31\u4e0d\u884c\uff08\u4e0d\u786e\u5b9a\u652f\u4e0d\u652f\u6301\uff0c\u6211\u4e0d\u77e5\u9053\u539f\u56e0\uff09\u3002<\/p>\n<p>\u5982\u679c\u4f60\u786e\u5b9a\u8bf7\u6c42\u8def\u5f84\u5728\u540e\u7aef\u4e00\u5b9a\u6709 gz \u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u91cd\u5199\u8def\u5f84\uff0c\u5b9a\u5411\u5230 gz \u6587\u4ef6\u5e76\u6dfb\u52a0\u4e00\u4e2a content-encoding=gzip \u7684 header\uff0c\u8fd9\u6837\u5c31\u80fd\u8fd4\u56de\u9884\u538b\u7f29\u6587\u4ef6\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>rewrite \/test.css \/test.css.gz break;<br \/>location = \/test.css.gz {<br \/>proxy_pass https:\/\/cos;<br \/>add_header &#8220;content-encoding&#8221; &#8220;gzip&#8221;;<br \/>}<\/p>\n<p>\u4f46\u4e5f\u6709\u4e2a\u95ee\u9898\uff0c\u8fd9\u6837\u4e0d\u517c\u5bb9\u4e0d\u652f\u6301 gzip \u7684\u5ba2\u6237\u7aef\uff0c\u8981\u517c\u5bb9\uff0c\u4f60\u5f97\u5148\u5224\u65ad\u5ba2\u6237\u7aef Accept-Encoding \u6709\u6ca1\u6709 gzip\uff0c\u5982\u679c\u6ca1\u6709\u8fd8\u662f\u5f97\u53e6\u5916\u8fd4\u56de\u539f\u6587\u3002<\/p>\n<p>\u7528\u5bf9\u8c61\u5b58\u50a8\u505a\u540e\u7aef\u6700\u65b9\u4fbf\u7684\u8fd8\u662f\u5bf9\u8c61\u5b58\u50a8\u653e\u672a\u538b\u7f29\u7684 css\/js \u6587\u4ef6\uff0c\u524d\u7aef Nginx proxy \u7684\u65f6\u5019\u5f00\u542f gzip on \u5c31\u597d\u4e86\uff0c\u53cd\u6b63\u662f\u9759\u6001\u6587\u4ef6\uff0c\u53ef\u4ee5\u8bbe\u7f6e proxy_cache \u8bfb\u53d6\u4e00\u6b21\u5c31\u7f13\u5b58\u5728\u672c\u5730\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614052\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lookookok <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @eason1874 <br \/>\u5bf9\u7684\u3002\u518d\u5efa\u8bae\u52a0\u4e2a Vary: Accept-Encoding \u5934<\/p>\n<p>proxy \u4f7f\u7528 rewrite \u5b8c\u6210 gzip \u4f1a\u589e\u52a0\u590d\u6742\u7a0b\u5ea6\uff0c\u4e0d\u786e\u5b9a\u5ba2\u6237\u7aef\u7684\u517c\u5bb9\u6027\uff0c\u4e0d\u5efa\u8bae\u8fd9\u4e48\u641e\u3002<\/p>\n<p>\u8fd8\u662f\u5efa\u8bae\u4f7f\u7528 nginx \u539f\u58f0\u6a21\u5757\u5b9e\u73b0 gzip \u7a33\u59a5\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614053\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dongtingyue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5bf9\u8c61\u5b58\u50a8\u8fd9\u6837\u7528\u7684\uff1f\u6ca1\u63d0\u4f9b\u57df\u540d\u8bbf\u95ee\u7684\u4e48\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614054\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dany813 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ng \u4e0d\u662f\u53ef\u4ee5\u5f00\u542f gzip \u538b\u7f29\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614055\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Firxiao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5148\u4e0d\u8ba8\u8bba\u662f\u5426\u4f7f\u7528 Nginx \u505a\u538b\u7f29. <br \/>\u4e3b\u7684\u95ee\u9898\u5e94\u8be5\u662f\u901a\u8fc7 Nginx \u7ed9\u540e\u7aef\u7684\u8fd4\u56de\u589e\u52a0\u6216\u8005\u66f4\u6539 Header<br \/>\u4e2a\u4eba\u7406\u89e3 \u6700\u597d\u8fd8\u662f\u540e\u7aef\u670d\u52a1\u63a7\u5236 \u5982\u679c\u66f4\u6539\u4e0d\u4e86, \u90a3\u5c31\u4e0d\u8981\u66f4\u6539\u4e86. \/\u72d7\u5934                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614056\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Firxiao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53e6\u5916\u5bf9\u8c61\u5b58\u50a8\u662f\u6309\u8bf7\u6c42\u91cf\u7b97\u94b1\u7684, \u538b\u7f29\u540e\u653e\u5bf9\u8c61\u5b58\u50a8\u95ee\u9898\u4e0d\u5927.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614057\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ddsfeng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             proxy_pass \u524d\u9762\u52a0\u4e00\u4e2a add_header Content-Encoding gzip; \u58f0\u660e\u4e00\u4e0b \u5185\u5bb9\u7684 Encoding \u5373\u53ef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5614058\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : skypyb <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @eason1874 #4 \u8c22\u8c22\u6307\u70b9\uff0c \u6211\u665a\u4e0a\u56de\u53bb\u8bd5\u4e00\u4e0b\uff0c\u4e0d\u8ba9 nginx \u81ea\u5df1 gzip \u4e3b\u8981\u662f\u7f13\u5b58\u4e86\u4e0d\u65b9\u4fbf\u5237\u65b0\u3002 \u6240\u4ee5\u8ba9\u4ed6\u6bcf\u6b21\u90fd\u8d70\u5185\u7f51\u62ff\u5bf9\u8c61\u50a8\u5b58\u91cc\u7684\u6570\u636e\uff0c \u5feb+\u514d\u8d39                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>nginx \u53cd\u5411\u4ee3\u7406 .js \u5230 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/421216"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=421216"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/421216\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=421216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=421216"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=421216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}