{"id":139199,"date":"2019-03-18T09:32:59","date_gmt":"2019-03-18T01:32:59","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=139199"},"modified":"2019-03-08T09:33:57","modified_gmt":"2019-03-08T01:33:57","slug":"nginx-config-url","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-config-url.html","title":{"rendered":"Nginx URL\u91cd\u5199\u89c4\u5219\u914d\u7f6e\u8be6\u89e3"},"content":{"rendered":"
nginx\u7684\u91cd\u5199\u6a21\u5757\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0e\u4e00\u4e2a\u865a\u62df\u5806\u53e0\u673a\u7ed3\u5408\u3002\u4f9d\u8d56\u4e8ePCRE\u5e93\uff0c\u56e0\u6b64\u9700\u8981\u5b89\u88c5pcre\u3002\u6839\u636e\u76f8\u5173\u53d8\u91cf\u91cd\u5b9a\u5411\u548c\u9009\u62e9\u4e0d\u540c\u7684\u914d\u7f6e\uff0c\u4ece\u4e00\u4e2alocation\u8df3\u8f6c\u5230\u53e6\u4e00\u4e2alocation\uff0c\u4e0d\u8fc7\u8fd9\u6837\u7684\u5faa\u73af\u6700\u591a\u53ef\u4ee5\u6267\u884c10\u6b21\uff0c\u8d85\u8fc7\u540enginx\u5c06\u8fd4\u56de500\u9519\u8bef\u3002\u540c\u65f6\uff0c\u91cd\u5199\u6a21\u5757\u5305\u542bset\u6307\u4ee4\uff0c\u6765\u521b\u5efa\u65b0\u7684\u53d8\u91cf\u5e76\u8bbe\u5176\u503c\uff0c\u8fd9\u5728\u6709\u4e9b\u60c5\u666f\u4e0b\u975e\u5e38\u6709\u7528\u7684\uff0c\u5982\u8bb0\u5f55\u6761\u4ef6\u6807\u8bc6\u3001\u4f20\u9012\u53c2\u6570\u5230\u5176\u4ed6location\u3001\u8bb0\u5f55\u505a\u4e86\u4ec0\u4e48\u7b49\u7b49\u3002<\/p>\n
\r\n\u8bed\u6cd5\uff1abreak\r\n\u9ed8\u8ba4\u503c\uff1anone\r\n\u4f7f\u7528\u5b57\u6bb5\uff1aserver, location, if\r\n<\/pre>\n\u4f5c\u7528:\u5b8c\u6210\u5f53\u524d\u8bbe\u7f6e\u7684\u91cd\u5199\u89c4\u5219\uff0c\u505c\u6b62\u6267\u884c\u5176\u4ed6\u7684\u91cd\u5199\u89c4\u5219\u3002<\/p>\n
if<\/strong><\/span><\/div>\n\r\n\u8bed\u6cd5\uff1aif (condition) { \u2026 }\r\n\u9ed8\u8ba4\u503c\uff1anone\r\n\u4f7f\u7528\u5b57\u6bb5\uff1aserver, location\r\n<\/pre>\n\u6ce8\u610f\uff1a\u5c3d\u91cf\u8003\u8651\u4f7f\u7528trp_files\u4ee3\u66ff\u3002<\/p>\n
\u5224\u65ad\u7684\u6761\u4ef6\u53ef\u4ee5\u6709\u4ee5\u4e0b\u503c\uff1a<\/p>\n
\r\n\u4e00\u4e2a\u53d8\u91cf\u7684\u540d\u79f0\uff1a\u7a7a\u5b57\u7b26\u4f20\u201d\u201c\u6216\u8005\u4e00\u4e9b\u201c0\u201d\u5f00\u59cb\u7684\u5b57\u7b26\u4e32\u4e3afalse\u3002\r\n\u5b57\u7b26\u4e32\u6bd4\u8f83\uff1a\u4f7f\u7528=\u6216!=\u8fd0\u7b97\u7b26\r\n\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff1a\u4f7f\u7528~(\u533a\u5206\u5927\u5c0f\u5199)\u548c~(\u4e0d\u533a\u5206\u5927\u5c0f\u5199)\uff0c\u53d6\u53cd\u8fd0\u7b97!~ \u548c!~\u3002\r\n\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a\u4f7f\u7528-f\u548c!-f\u64cd\u4f5c\u7b26\r\n\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff1a\u4f7f\u7528-d\u548c!-d\u64cd\u4f5c\u7b26\r\n\u6587\u4ef6\u3001\u76ee\u5f55\u3001\u7b26\u53f7\u94fe\u63a5\u662f\u5426\u5b58\u5728\uff1a\u4f7f\u7528-e\u548c!-e\u64cd\u4f5c\u7b26\r\n\u6587\u4ef6\u662f\u5426\u53ef\u6267\u884c\uff1a\u4f7f\u7528-x\u548c!-x\u64cd\u4f5c\u7b26\r\n<\/pre>\nreturn<\/strong><\/span><\/div>\n\r\n\u8bed\u6cd5\uff1areturn code\r\n\u9ed8\u8ba4\u503c\uff1anone\r\n\u4f7f\u7528\u5b57\u6bb5\uff1aserver, location, if\r\n<\/pre>\n\u505c\u6b62\u5904\u7406\u5e76\u4e3a\u5ba2\u6237\u7aef\u8fd4\u56de\u72b6\u6001\u7801\u3002\u975e\u6807\u51c6\u7684444\u72b6\u6001\u7801\u5c06\u5173\u95ed\u8fde\u63a5\uff0c\u4e0d\u53d1\u9001\u4efb\u4f55\u54cd\u5e94\u5934\u3002\u53ef\u4ee5\u4f7f\u7528\u7684\u72b6\u6001\u7801\u6709\uff1a204\uff0c400\uff0c402-406\uff0c408\uff0c410, 411, 413, 416\u4e0e500-504\u3002\u5982\u679c\u72b6\u6001\u7801\u9644\u5e26\u6587\u5b57\u6bb5\u843d\uff0c\u8be5\u6587\u672c\u5c06\u88ab\u653e\u7f6e\u5728\u54cd\u5e94\u4e3b\u4f53\u3002\u76f8\u53cd\uff0c\u5982\u679c\u72b6\u6001\u7801\u540e\u9762\u662f\u4e00\u4e2aURL\uff0c\u8be5URL\u5c06\u6210\u4e3alocation\u5934\u8865\u503c\u3002\u6ca1\u6709\u72b6\u6001\u7801\u7684URL\u5c06\u88ab\u89c6\u4e3a\u4e00\u4e2a302\u72b6\u6001\u7801\u3002<\/p>\n
rewrite<\/strong><\/span><\/div>\n\r\n\u8bed\u6cd5\uff1arewrite regex replacement flag\r\n\u9ed8\u8ba4\u503c\uff1anone\r\n\u4f7f\u7528\u5b57\u6bb5\uff1aserver, location, if\r\n<\/pre>\n\u6309\u7167\u76f8\u5173\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u5b57\u7b26\u4e32\u4fee\u6539URI\uff0c\u6307\u4ee4\u6309\u7167\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u987a\u5e8f\u6267\u884c\u3002\u53ef\u4ee5\u5728\u91cd\u5199\u6307\u4ee4\u540e\u9762\u6dfb\u52a0\u6807\u8bb0\u3002<\/p>\n
\u6ce8\u610f\uff1a\u5982\u679c\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u4ee5http:\/\/\u5f00\u5934\uff0c\u8bf7\u6c42\u5c06\u88ab\u91cd\u5b9a\u5411\uff0c\u5e76\u4e14\u4e0d\u518d\u6267\u884c\u591a\u4f59\u7684rewrite\u6307\u4ee4\u3002<\/p>\n
\u5c3e\u90e8\u7684\u6807\u8bb0(flag)\u53ef\u4ee5\u662f\u4ee5\u4e0b\u7684\u503c\uff1a<\/p>\n
\r\nlast \u2013 \u505c\u6b62\u5904\u7406\u91cd\u5199\u6a21\u5757\u6307\u4ee4\uff0c\u4e4b\u540e\u641c\u7d22location\u4e0e\u66f4\u6539\u540e\u7684URI\u5339\u914d\u3002\r\nbreak \u2013 \u5b8c\u6210\u91cd\u5199\u6307\u4ee4\u3002\r\nredirect \u2013 \u8fd4\u56de302\u4e34\u65f6\u91cd\u5b9a\u5411\uff0c\u5982\u679c\u66ff\u6362\u5b57\u6bb5\u7528http:\/\/\u5f00\u5934\u5219\u88ab\u4f7f\u7528\u3002\r\npermanent \u2013 \u8fd4\u56de301\u6c38\u4e45\u91cd\u5b9a\u5411\u3002\r\n<\/pre>\nrewrite_log<\/strong><\/span><\/div>\n\r\n\u8bed\u6cd5\uff1arewrite_log on | off\r\n\u9ed8\u8ba4\u503c\uff1arewrite_log off\r\n\u4f7f\u7528\u5b57\u6bb5\uff1aserver, location, if\r\n\u53d8\u91cf\uff1a\u65e0\r\n<\/pre>\n\u542f\u7528\u65f6\u5c06\u5728error log\u4e2d\u8bb0\u5f55notice\u7ea7\u522b\u7684\u91cd\u5199\u65e5\u5fd7\u3002<\/p>\n
set<\/strong><\/span><\/div>\n\r\n\u8bed\u6cd5\uff1aset variable value\r\n\u9ed8\u8ba4\u503c\uff1anone\r\n\u4f7f\u7528\u5b57\u6bb5\uff1aserver, location, if\r\n<\/pre>\n\u4e3a\u7ed9\u5b9a\u7684\u53d8\u91cf\u8bbe\u7f6e\u4e00\u4e2a\u7279\u5b9a\u503c\u3002<\/p>\n
uninitialized_variable_warn<\/strong><\/span><\/div>\n\r\n\u8bed\u6cd5\uff1auninitialized_variable_warn on|off\r\n\u9ed8\u8ba4\u503c\uff1auninitialized_variable_warn on\r\n\u4f7f\u7528\u5b57\u6bb5\uff1ahttp, server, location, if\r\n<\/pre>\n\u63a7\u5236\u662f\u5426\u8bb0\u5f55\u672a\u521d\u59cb\u5316\u53d8\u91cf\u7684\u8b66\u544a\u4fe1\u606f\u3002<\/p>\n
\u91cd\u5199\u89c4\u5219\u7ec4\u6210\u90e8\u5206<\/strong><\/div>\n\u7b2c\u4e00\u90e8\u5206\u2013\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/span><\/div>\n\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u6765\u6355\u83b7\uff0c\u540e\u7eed\u53ef\u4ee5\u6839\u636e\u4f4d\u7f6e\u6765\u5c06\u5176\u5f15\u7528\uff0c\u4f4d\u7f6e\u53d8\u91cf\u503c\u53d6\u51b3\u4e8e\u6355\u83b7\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u987a\u5e8f\uff0c$1\u5f15\u7528\u7b2c\u4e00\u4e2a\u62ec\u53f7\u4e2d\u7684\u503c\uff0c$2\u5f15\u7528\u7b2c\u4e8c\u4e2a\u62ec\u53f7\u4e2d\u7684\u503c\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u5982\uff1a<\/p>\n
\r\n^\/images\/([a-z]{2})\/([a-z0-9]{5})\/(.*)\\.(png|jpg|gif)$\r\n# $1\u662f\u4e24\u4e2a\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\r\n# $2\u662f\u7531\u5c0f\u5199\u5b57\u6bcd\u548c0\u52309\u7684\u6570\u5b57\u7ec4\u6210\u76845\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\r\n# $3\u5c06\u662f\u4e2a\u6587\u4ef6\u540d\r\n# $4\u662fpng\u3001jpg\u3001gif\u4e2d\u7684\u5176\u4e2d\u4e00\u4e2a\u3002\r\n<\/pre>\n\u7b2c\u4e8c\u90e8\u5206\u2013URI<\/strong><\/span><\/div>\n\u8bf7\u6c42\u88ab\u6539\u5199\u3002\u8be5URI\u53ef\u80fd\u5305\u542b\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u7684\u6355\u83b7\u7684\u4f4d\u7f6e\u53c2\u6570\u6216\u8fd9\u4e2a\u7ea7\u522b\u4e0b\u7684nginx\u4efb\u4f55\u914d\u7f6e\u53d8\u91cf\u3002\u5982\uff1a<\/p>\n
\/data?file=$3.$4<\/pre>\n\u5982\u679c\u8fd9\u4e2aURI\u4e0d\u5339\u914dnginx\u914d\u7f6e\u7684\u4efb\u4f55location\uff0c\u90a3\u4e48\u5c06\u7ed9\u5ba2\u6237\u7aef\u8fd4\u56de301(\u6c38\u4e45\u91cd\u5b9a\u5411)\u6216302(\u4e34\u65f6\u91cd\u5b9a\u5411)\u7684\u72b6\u6001\u7801\u6765\u8868\u793a\u91cd\u5b9a\u5411\u7c7b\u578b\u3002\u8be5\u72b6\u6001\u7801\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e09\u4e2a\u53c2\u6570\u6765\u660e\u786e\u6307\u5b9a\u3002<\/p>\n
\u7b2c\u4e09\u90e8\u5206\u2013\u6807\u8bb0<\/strong><\/span><\/div>\n\u7b2c\u4e09\u90e8\u5206\u4e5f\u5c31\u662f\u5c3e\u90e8\u7684\u6807\u8bb0(flag)\u3002 last\u6807\u8bb0\u5c06\u5bfc\u81f4\u91cd\u5199\u540e\u7684URI\u641c\u7d22\u5339\u914dnginx\u7684\u5176\u4ed6location\uff0c\u6700\u591a\u53ef\u5faa\u73af10\u6b21\u3002\u5982\uff1a<\/p>\n
rewrite '^\/images\/([a-z]{2})\/([a-z0-9]{5})\/(.*)\\.(png|jpg|gif)$' \/data?file=$3.$4 last;<\/pre>\nbreak\u6307\u4ee4\u53ef\u4ee5\u5f53\u505a\u81ea\u8eab\u6307\u4ee4\u3002\u5982\uff1a<\/p>\n
\r\nif ($bwhog) {\r\n limit_rate 300k;\r\n break;\r\n}\r\n<\/pre>\n\u5b9e\u4f8b<\/strong><\/span><\/div>\n\r\nhttp {\r\n #\u5b9a\u4e49image\u65e5\u5fd7\u683c\u5f0f\r\n log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;\r\n # \u5f00\u542f\u91cd\u5199\u65e5\u5fd7\r\n rewrite_log on;\r\n\r\n server {\r\n root \/home\/www;\r\n\r\n location \/ {\r\n # \u91cd\u5199\u89c4\u5219\u4fe1\u606f\r\n error_log logs\/rewrite.log notice; \r\n # \u6ce8\u610f\u8fd9\u91cc\u8981\u7528\u2018\u2019\u5355\u5f15\u53f7\u5f15\u8d77\u6765\uff0c\u907f\u514d{}\r\n rewrite '^\/images\/([a-z]{2})\/([a-z0-9]{5})\/(.*)\\.(png|jpg|gif)$' \/data?file=$3.$4;\r\n # \u6ce8\u610f\u4e0d\u80fd\u5728\u4e0a\u9762\u8fd9\u6761\u89c4\u5219\u540e\u9762\u52a0\u4e0a\u201clast\u201d\u53c2\u6570\uff0c\u5426\u5219\u4e0b\u9762\u7684set\u6307\u4ee4\u4e0d\u4f1a\u6267\u884c\r\n set $image_file $3;\r\n set $image_type $4;\r\n }\r\n\r\n location \/data {\r\n # \u6307\u5b9a\u9488\u5bf9\u56fe\u7247\u7684\u65e5\u5fd7\u683c\u5f0f\uff0c\u6765\u5206\u6790\u56fe\u7247\u7c7b\u578b\u548c\u5927\u5c0f\r\n access_log logs\/images.log mian;\r\n root \/data\/images;\r\n # \u5e94\u7528\u524d\u9762\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u5224\u65ad\u9996\u5148\u6587\u4ef6\u5728\u4e0d\u5728\uff0c\u4e0d\u5728\u518d\u5224\u65ad\u76ee\u5f55\u5728\u4e0d\u5728\uff0c\u5982\u679c\u8fd8\u4e0d\u5728\u5c31\u8df3\u8f6c\u5230\u6700\u540e\u4e00\u4e2aurl\u91cc\r\n try_files \/$arg_file \/image404.html;\r\n }\r\n location = \/image404.html {\r\n # \u56fe\u7247\u4e0d\u5b58\u5728\u8fd4\u56de\u7279\u5b9a\u7684\u4fe1\u606f\r\n return 404 \"image not found\\n\";\r\n }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"nginx\u7684\u91cd\u5199\u6a21\u5757\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u4e0e\u4e00\u4e2a\u865a\u62df\u5806\u53e0\u673a\u7ed3\u5408\u3002\u4f9d\u8d56\u4e8ePCRE\u5e93\uff0c\u56e0\u6b64\u9700\u8981\u5b89\u88c5pcre\u3002\u6839 […]<\/p>\n","protected":false},"author":668,"featured_media":30136,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-139199","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=139199"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139199\/revisions"}],"predecessor-version":[{"id":139275,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/139199\/revisions\/139275"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/30136"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=139199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=139199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=139199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}