{"id":911,"date":"2014-09-07T20:52:57","date_gmt":"2014-09-07T11:52:57","guid":{"rendered":"http:\/\/tech.akat.info\/?p=911"},"modified":"2014-09-16T01:12:22","modified_gmt":"2014-09-15T16:12:22","slug":"%e3%80%90nginx%e3%80%91ifisevil","status":"publish","type":"post","link":"https:\/\/tech.akat.info\/?p=911","title":{"rendered":"\u3010nginx\u3011IfIsEvil"},"content":{"rendered":"<h1>\u516c\u5f0f\u30b5\u30a4\u30c8\u3088\u308a<\/h1>\n<p>IfIsEvil\u3092\u8a33\u3057\u3066\u307f\u305f<br \/>\nhttp:\/\/wiki.nginx.org\/IfIsEvil<\/p>\n<h1>Introduction<\/h1>\n<p>if\u306flocation\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3067\u4e88\u671f\u305b\u306c\u52d5\u4f5c\u3092\u3059\u308b\u3053\u3068\u304c\u3042\u308b\u3001SIGSEGV(\u30bb\u30b0\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u9055\u53cd)\u3068\u306a\u308b\u3053\u3068\u3082<br \/>\n\u305f\u3060\u3057if\u306e\u4e2d\u304creturn&#8230;\u3068rewrite&#8230;last\u306e\u307f\u5229\u7528\u3057\u3066\u3044\u308b\u3068\u304d\u306f100%\u5b89\u5168<br \/>\n\u307e\u305f\u30e9\u30f3\u30c0\u30e0\u3067\u52d5\u4f5c\u3059\u308b\u3053\u3068\u306f\u306a\u3044\u305f\u3081\u8a55\u4fa1\u3092\u3057\u3063\u304b\u308a\u3057\u3066\u304a\u3051\u3070\u5229\u7528\u3057\u3066\u3082\u554f\u984c\u306f\u8d77\u3053\u3089\u306a\u3044<br \/>\n\u307e\u305f\u4e0b\u306e\u4f8b\u306e\u3088\u3046\u306b\u3001\u5909\u6570\u306e\u8a55\u4fa1(\u8a55\u4fa1\u7528\u306edirective\u304c\u5b58\u5728\u3057\u306a\u3044)\u3092\u3059\u308b\u3068\u304d\u306bif\u3092\u5229\u7528\u3092\u907f\u3051\u3089\u308c\u306a\u3044\u5834\u5408\u3082\u3042\u308b<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nif ($request_method = POST ) {\r\n  return 405;\r\n}\r\nif ($args ~ post=140){\r\n  rewrite ^ http:\/\/example.com\/ permanent;\r\n}\r\n<\/pre>\n<h1>What to do instead<\/h1>\n<p>return&#8230;\u3068rewrite&#8230;last\u306e\u5229\u7528\u3092\u9664\u304d\u3001try_files\u3092\u5229\u7528\u3057\u305f\u307b\u3046\u304c\u3088\u3044<br \/>\n\u307e\u305f\u4e0b\u306e\u4f8b\u306e\u3088\u3046\u306b\u3001location\u3078\u306e\u8a18\u8f09\u3067\u306a\u304f\u3001server context\u3078\u306e\u8a18\u8f09\u306b\u5909\u66f4\u3067\u304d\u308b\u5834\u5408\u3082\u3042\u308b<br \/>\n\u305d\u308c\u4ee5\u5916\u306b\u3082\u4ed6\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u56de\u907f\u3067\u304d\u308b\u3053\u3068\u3082<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n        error_page 418 = @other;\r\n        recursive_error_pages on;\r\n \r\n        if ($something) {\r\n            return 418;\r\n        }\r\n \r\n        # some configuration\r\n        ...\r\n}\r\n \r\nlocation @other {\r\n    # some other configuration\r\n    ...\r\n}\r\n<\/pre>\n<h1>Examples<\/h1>\n<p>IfIsEvil\u306e\u5b9f\u4f8b\u3001\u3053\u306e\u52d5\u4f5c\u306f\u4ed5\u69d8\u3067\u3042\u308a\u30d0\u30b0\u3067\u306f\u306a\u3044<\/p>\n<h2>only-one-if<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/only-one-if {\r\n    set $true 1;\r\n\r\n    if ($true) {\r\n        add_header X-First 1;\r\n    }\r\n\r\n    if ($true) {\r\n        add_header X-Second 2;\r\n    }\r\n\r\n    return 204;\r\n}\r\n<\/pre>\n<p>\u4ed6\u306e\u30b5\u30fc\u30d0\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n### 2\u3064\u3081\u306e\u30d8\u30c3\u30c0\u3057\u304b\u4ed8\u4e0e\u3055\u308c\u306a\u3044  ###\r\nroot@akat:\/home\/shimizu# curl --dump-header - http:\/\/49.212.204.46\/only-one-if\r\nHTTP\/1.1 204 No Content\r\nServer: nginx\/1.2.1\r\nDate: Sun, 07 Sep 2014 10:42:03 GMT\r\nConnection: keep-alive\r\nX-Second: 2\r\n<\/pre>\n<h2>proxy-pass-uri<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/proxy-pass-uri {\r\n    proxy_pass http:\/\/www.google.co.jp\/;\r\n\r\n    set $true 1;\r\n\r\n    if ($true) {\r\n        # nothing\r\n    }\r\n}\r\n<\/pre>\n<p>\u4ed6\u306e\u30b5\u30fc\u30d0\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n### if\u3092\u901a\u904e\u5f8c\u3001proxy_pass\u304b\u3089redirect\u3059\u308b ###\r\nroot@akat:\/home\/shimizu# curl --dump-header - http:\/\/49.212.204.46\/proxy-pass-uri\r\nHTTP\/1.1 404 Not Found\r\nServer: nginx\/1.2.1\r\nDate: Sun, 07 Sep 2014 11:13:28 GMT\r\nContent-Type: text\/html; charset=UTF-8\r\nContent-Length: 1439\r\nConnection: keep-alive\r\nX-Content-Type-Options: nosniff\r\nX-XSS-Protection: 1; mode=block\r\nAlternate-Protocol: 80:quic\r\n\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html lang=en&gt;\r\n  &lt;meta charset=utf-8&gt;\r\n  &lt;meta name=viewport content=&quot;initial-scale=1, minimum-scale=1, width=device-width&quot;&gt;\r\n  &lt;title&gt;Error 404 (Not Found)!!1&lt;\/title&gt;\r\n  &lt;style&gt;\r\n    *{margin:0;padding:0}html,code{font:15px\/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* &gt; body{background:url(\/\/www.google.com\/images\/errors\/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(\/\/www.google.com\/images\/errors\/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(\/\/www.google.com\/images\/errors\/logo_sm_2_hr.png) no-repeat 0% 0%\/100% 100%;-moz-border-image:url(\/\/www.google.com\/images\/errors\/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(\/\/www.google.com\/images\/errors\/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}\r\n  &lt;\/style&gt;\r\n  &lt;a href=\/\/www.google.com\/&gt;&lt;span id=logo aria-label=Google&gt;&lt;\/span&gt;&lt;\/a&gt;\r\n  &lt;p&gt;&lt;b&gt;404.&lt;\/b&gt; &lt;ins&gt;That\u2019s an error.&lt;\/ins&gt;\r\n  &lt;p&gt;The requested URL &lt;code&gt;\/proxy-pass-uri&lt;\/code&gt; was not found on this server.  &lt;ins&gt;That\u2019s all we know.&lt;\/ins&gt;\r\n<\/pre>\n<h2>if-try-files<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/if-try-files {\r\n     try_files  \/file  @fallback;\r\n\r\n     set $true 1;\r\n\r\n     if ($true) {\r\n         # nothing\r\n     }\r\n}\r\n<\/pre>\n<p>\u4ed6\u306e\u30b5\u30fc\u30d0\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n### try_files\u306f\u52d5\u4f5c\u3057\u306a\u3044 ###\r\nroot@akat:\/home\/shimizu# curl --dump-header - http:\/\/49.212.204.46\/if-try-files\r\nHTTP\/1.1 404 Not Found\r\nServer: nginx\/1.2.1\r\nDate: Sun, 07 Sep 2014 11:19:02 GMT\r\nContent-Type: text\/html\r\nContent-Length: 168\r\nConnection: keep-alive\r\n\r\n&lt;html&gt;\r\n&lt;head&gt;&lt;title&gt;404 Not Found&lt;\/title&gt;&lt;\/head&gt;\r\n&lt;body bgcolor=&quot;white&quot;&gt;\r\n&lt;center&gt;&lt;h1&gt;404 Not Found&lt;\/h1&gt;&lt;\/center&gt;\r\n&lt;hr&gt;&lt;center&gt;nginx\/1.2.1&lt;\/center&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h2>crash<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/crash {\r\n\r\n    set $true 1;\r\n\r\n    if ($true) {\r\n        # fastcgi_pass here\r\n        fastcgi_pass  127.0.0.1:9000;\r\n    }\r\n\r\n    if ($true) {\r\n        # no handler here\r\n    }\r\n}\r\n<\/pre>\n<p>\u4ed6\u306e\u30b5\u30fc\u30d0\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n### SIGSEGV(\u30bb\u30b0\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u9055\u53cd) fastcgi_pass\u30fbproxy_pass\u306a\u3069\u306f\u5229\u7528\u3057\u306a\u3044\u307b\u3046\u304c\u3088\u3044 ###\r\nroot@akat:\/home\/shimizu# curl --dump-header - http:\/\/49.212.204.46\/crash\r\ncurl: (52) Empty reply from server\r\n<\/pre>\n<h1>Why this happens and still not fixed<\/h1>\n<p>\u901a\u5e38\u306e\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u306f\u9806\u756a\u306b\u51e6\u7406\u3055\u308c\u308b\u304c\u3001if\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u306frewrite module\u306e\u4e00\u90e8\u3067\u3042\u308a\u3001\u65e9\u3044\u6bb5\u968e\u3067\u8a55\u4fa1\u3055\u308c\u308b\u3002<br \/>\nif\u306e\u4e2d\u306bnon-rewrite\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u304c\u3042\u308b\u3068\u3001\u4e0a\u306e\u3088\u3046\u306a\u52d5\u4f5c\u3068\u306a\u308b<br \/>\nif\u306e\u4e2d\u306enon-rewrite\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u7121\u52b9\u306b\u3059\u308b\u3053\u3068\u304c\u3001\u305f\u3060\u3057\u3044fix\u3067\u3042\u308b\u304c\u3001\u591a\u304f\u306e\u8a2d\u5b9a\u306b\u5f71\u97ff\u304c\u3042\u308b\u305f\u3081\u5bfe\u5fdc\u3067\u304d\u3066\u3044\u306a\u3044<\/p>\n<h1>If you still want to use if<\/h1>\n<p>\u305d\u308c\u3067\u3082if\u3092\u5229\u7528\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u52d5\u4f5c\u3092\u7406\u89e3\u3057\u3001\u3061\u3083\u3093\u3068\u30c6\u30b9\u30c8\u3057\u3066\u5229\u7528\u3059\u308b\u3053\u3068<\/p>\n<h1>\u53c2\u8003URL<\/h1>\n<p>\u516c\u5f0f\u30b5\u30a4\u30c8-IfIsEvil-<br \/>\nhttp:\/\/wiki.nginx.org\/IfIsEvil<br \/>\nnginx\u306eif\u306b\u8981\u6ce8\u610f<br \/>\nhttp:\/\/www.techscore.com\/blog\/2012\/10\/31\/nginx%E3%81%AEif%E3%81%AB%E8%A6%81%E6%B3%A8%E6%84%8F\/<br \/>\nHow nginx &#8220;location if&#8221; works<br \/>\nhttp:\/\/agentzh.blogspot.jp\/2011\/03\/how-nginx-location-if-works.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u516c\u5f0f\u30b5\u30a4\u30c8\u3088\u308a IfIsEvil\u3092\u8a33\u3057\u3066\u307f\u305f http:\/\/wiki.nginx.org\/IfIsEvil Introduction if\u306flocation\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3067\u4e88\u671f\u305b\u306c\u52d5\u4f5c\u3092\u3059\u308b\u3053\u3068\u304c\u3042\u308b\u3001SIGSEGV(\u30bb\u30b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/911"}],"collection":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=911"}],"version-history":[{"count":4,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions"}],"predecessor-version":[{"id":916,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/911\/revisions\/916"}],"wp:attachment":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}