{"id":666,"date":"2014-07-14T00:33:56","date_gmt":"2014-07-13T15:33:56","guid":{"rendered":"http:\/\/tech.akat.info\/?p=666"},"modified":"2014-12-31T21:13:55","modified_gmt":"2014-12-31T12:13:55","slug":"shimizu-2","status":"publish","type":"post","link":"https:\/\/tech.akat.info\/?p=666","title":{"rendered":"nginx + cookie \u8a8d\u8a3c\u306e\u4ed5\u7d44\u307f\u3092\u4f5c\u6210"},"content":{"rendered":"<h1>\u6982\u8981<\/h1>\n<p>\u300cnginx\u3067\u8a8d\u8a3c\u7528proxy\u30b5\u30fc\u30d0\u3092\u4f5c\u6210\u300d\u3092\u3082\u3068\u306b\u8a2d\u5b9a<br \/>\n\u5168\u3066\u306e\u30a2\u30af\u30bb\u30b9\u3067\u308f\u3056\u308f\u3056PHP\u3092\u547c\u3073\u51fa\u3059\u306e\u306f\u975e\u52b9\u7387\u306a\u306e\u3067\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u5224\u5b9a\u90e8\u5206\u306fNginx\u306e\u307f\u3067\u89e3\u6c7a\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u9ad8\u901f\u5316\u53ef\u80fd<br \/>\nhttp:\/\/blog.asial.co.jp\/1046<\/p>\n<h1>GOAL<\/h1>\n<p>cookie\u306bauthid\u3068\u3044\u3046\u30ad\u30fc\u3067\u3001\u30e9\u30f3\u30c0\u30e0\u5024\u3092\u4fdd\u5b58<br \/>\nmemcached\u306b\u30e9\u30f3\u30c0\u30e0\u5024\u3092\u30ad\u30fc\u306b\u3057\u3066\u3001\u8a8d\u8a3c\u72b6\u614b(1)\u3092\u30bb\u30c3\u30c8<br \/>\ncookie\u3068memcached\u306e\u5024\u304b\u3089\u3001\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3059\u308b<br \/>\n\u4eca\u56de\u306f\u30e9\u30f3\u30c0\u30e0\u5024\u3092&#8221;abcde&#8221;\u3068\u3059\u308b<br \/>\n\u3067\u304d\u308c\u3070\u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3\u306e\u30e2\u30b8\u30e5\u30fc\u30ebeval\u306f\u5229\u7528\u3057\u306a\u3044\u65b9\u5411\u3067\u691c\u8a0e\u3059\u308b<\/p>\n<h1>\u5404\u30d0\u30fc\u30b8\u30e7\u30f3<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/home\/shimizu# uname -a\r\nLinux hostname 3.2.0-4-amd64 #1 SMP Debian 3.2.54-2 x86_64 GNU\/Linux\r\nroot@hostname:\/home\/shimizu# memcached -h\r\nmemcached 1.4.13\r\nroot@hostname:\/home\/shimizu# nginx -v\r\nnginx version: nginx\/1.2.1\r\nTLS SNI support enabled\r\nconfigure arguments: --prefix=\/etc\/nginx --conf-path=\/etc\/nginx\/nginx.conf --error-log-path=\/var\/log\/nginx\/error.log --http-client-body-temp-path=\/var\/lib\/nginx\/body --http-fastcgi-temp-path=\/var\/lib\/nginx\/fastcgi --http-log-path=\/var\/log\/nginx\/access.log --http-proxy-temp-path=\/var\/lib\/nginx\/proxy --http-scgi-temp-path=\/var\/lib\/nginx\/scgi --http-uwsgi-temp-path=\/var\/lib\/nginx\/uwsgi --lock-path=\/var\/lock\/nginx.lock --pid-path=\/var\/run\/nginx.pid --with-pcre-jit --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=\/usr\/include\/openssl --with-md5=\/usr\/include\/openssl --with-mail --with-mail_ssl_module --add-module=\/tmp\/buildd\/nginx-1.2.1\/debian\/modules\/nginx-auth-pam --add-module=\/tmp\/buildd\/nginx-1.2.1\/debian\/modules\/nginx-echo --add-module=\/tmp\/buildd\/nginx-1.2.1\/debian\/modules\/nginx-upstream-fair --add-module=\/tmp\/buildd\/nginx-1.2.1\/debian\/modules\/nginx-dav-ext-module\r\n<\/pre>\n<h1>memcached<\/h1>\n<h2>memcached\u306bkey\u3092\u8a2d\u5b9a\u3059\u308b<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/var\/nginx# telnet localhost 11211\r\nTrying ::1...\r\nTrying 127.0.0.1...\r\nConnected to localhost.\r\nEscape character is '^]'.\r\nset abcde 0 0 1\r\n1\r\nSTORED\r\nget abcde\r\nVALUE abcde 0 1\r\n1\r\nEND\r\nquit\r\n<\/pre>\n<h2>key\u306e\u4e2d\u8eab\u3092GUI\u3067\u78ba\u8a8d\u3059\u308b<\/h2>\n<p>root@hostname:\/var\/www\/htdocs# wget -d http:\/\/livebookmark.net\/memcachephp\/memcachephp.zip<br \/>\nDEBUG output created by Wget 1.13.4 on linux-gnu.<\/p>\n<p>URI encoding = `UTF-8&#8242;<br \/>\n&#8211;2014-07-11 22:52:38&#8211;  http:\/\/livebookmark.net\/memcachephp\/memcachephp.zip<br \/>\nlivebookmark.net (livebookmark.net) \u3092DNS\u306b\u554f\u3044\u3042\u308f\u305b\u3066\u3044\u307e\u3059&#8230; 108.61.16.67<br \/>\nCaching livebookmark.net => 108.61.16.67<br \/>\n&#8230;(\u4e2d\u7565)<br \/>\nroot@hostname:\/var\/www\/htdocs# unzip memcachephp.zip<br \/>\nArchive:  memcachephp.zip<br \/>\n  inflating: memcache.php<br \/>\nroot@hostname:\/var\/www\/htdocs# chown www-data:www-data memcache.php<br \/>\nroot@hostname:\/var\/www\/htdocs# vi memcache.php<br \/>\n==================<br \/>\n$MEMCACHE_SERVERS[] = &#8216;localhost:11211&#8217;; \/\/ add more as an array<br \/>\n\/\/$MEMCACHE_SERVERS[] = &#8216;mymemcache-server2:11211&#8217;; \/\/ add more as an array<br \/>\n==================<\/p>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-11_230850.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-11_230850.png\" alt=\"2014-07-11_230850\" width=\"1103\" height=\"550\" class=\"alignnone size-full wp-image-669\" \/><\/a><\/p>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-11_230931.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-11_230931.png\" alt=\"2014-07-11_230931\" width=\"1274\" height=\"320\" class=\"alignnone size-full wp-image-667\" \/><\/a><\/p>\n<p>\u203bphp\u304b\u3089memcached\u306b\u8a2d\u5b9a\u3057\u305f\u5834\u5408\u3001\u6709\u52b9\u671f\u9650\u306fphp.ini\u306esession.gc_maxlifetime\u304c\u4f7f\u7528\u3055\u308c\u308b<\/p>\n<h2>cookie\u3092\u8a2d\u5b9a\u3059\u308b<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/var\/www\/htdocs# vi setcookie.php\r\n=====\r\n&lt;?php\r\nsetcookie(&quot;authid&quot;, &quot;abcde&quot;);\r\necho &quot;get cookie!&quot;\r\n?&gt;\r\n=====\r\n<\/pre>\n<p>setcookie.php\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b<\/p>\n<h2>cookie\u3092\u78ba\u8a8d\u3059\u308b<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nroot@hostname:\/var\/www\/htdocs# vi getcookie.php\r\n=====\r\n&lt;?php\r\nvar_dump($_COOKIE);\r\n?&gt;\r\n=====\r\n<\/pre>\n<p>\u30d6\u30e9\u30a6\u30b6\u304b\u3089getcookie.php\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\narray(2) {\r\n  &#x5B;&quot;__atuvc&quot;]=&gt;\r\n  string(4) &quot;6|28&quot;\r\n  &#x5B;&quot;authid&quot;]=&gt;\r\n  string(5) &quot;abcde&quot;\r\n}\r\n<\/pre>\n<h2>\u30d6\u30e9\u30a6\u30b6\u306e\u8a2d\u5b9a\u753b\u9762\u304b\u3089<\/h2>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-11_233444.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-11_233444.png\" alt=\"2014-07-11_233444\" width=\"703\" height=\"374\" class=\"alignnone size-full wp-image-672\" \/><\/a><\/p>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-12_000433.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-12_000433.png\" alt=\"2014-07-12_000433\" width=\"536\" height=\"379\" class=\"alignnone size-full wp-image-674\" \/><\/a><\/p>\n<h2>nginx + memcached \u305d\u306e1<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nupstream memcache {\r\n        server 127.0.0.1:11211;\r\n}\r\n\r\nserver {\r\n        root \/var\/www\/htdocs;\r\n        index index.html index.htm;\r\n        server_name x.x.x.x;\r\n\r\n        location \/ {\r\n                set $memcached_key abcde;\r\n                memcached_pass memcache;\r\n                default_type &quot;text\/html; charset=utf-8&quot;;\r\n                error_page 404 = @notkey;\r\n        }\r\n\r\n        location @notkey {\r\n                proxy_pass http:\/\/www.google.co.jp;\r\n        }\r\n}\r\n<\/pre>\n<p>memcached_pass\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u306f\u3001\u30ad\u30fc\u691c\u7d22\u3092\u5b9f\u65bd\u3059\u308b\u969b\u306b$memcached_key\u5909\u6570\u3092\u5229\u7528\u3059\u308b<br \/>\n\u5bfe\u5fdc\u3059\u308bkey\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306f\u3001error_page 404\u3068\u306a\u308a\u3001\u4e0a\u8a18\u5834\u5408\u306fhttp:\/\/www.google.co.jp\u306b\u8ee2\u9001\u3055\u308c\u308b<br \/>\n\u5bfe\u5fdc\u3059\u308bkey\u304c\u5b58\u5728\u3057\u305f\u5834\u5408\u306fvalue\u3092\u8fd4\u3059<br \/>\n\u901a\u5e38\u306fvalue\u306b\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u4fdd\u5b58\u3057\u3066\u5229\u7528\u3059\u308b<\/p>\n<h2>nginx + memcached \u305d\u306e2<\/h2>\n<pre class=\"brush: plain; title: location\u5185\u3092\u5909\u66f4; notranslate\" title=\"location\u5185\u3092\u5909\u66f4\">\r\n        location \/ {\r\n\r\n                if ($cookie_authid) {\r\n                        set $memcached_key $cookie_authid;\r\n                        memcached_pass memcache;\r\n                        default_type &quot;text\/html; charset=utf-8&quot;;\r\n                        error_page 404 = @notkey;\r\n                }\r\n        }\r\n<\/pre>\n<p>HTTP\u30e2\u30b8\u30e5\u30fc\u30eb\u3067$cookie_name\u306f\u3001name\u3068\u3044\u3046\u540d\u524d\u306ecookie\u306e\u5024\u3068\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b<br \/>\n\u3064\u307e\u308a\u3001$cookie_authid\u306fauthid\u306e\u5024\u304c\u5165\u308b<br \/>\n&#8230;\u305f\u3060\u3057memcached_pass\u306b\u306fvalue\u3092\u8fd4\u3059\u3057\u304b\u3067\u304d\u305a\u3001eval\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u5fc5\u9808\u306e\u3088\u3046\u306a\u306e\u3067\u4ed6\u306e\u65b9\u6cd5\u3092\u6a21\u7d22\u3057\u3066\u307f\u308b<\/p>\n<h2>ngx_http_map_module\u306e\u5229\u7528<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmap $cookie_authid $cookie_flag {\r\n        abcde 1;\r\n        default 0;\r\n}\r\n\r\nserver {\r\n        root \/var\/www\/htdocs;\r\n        index index.html index.htm;\r\n        server_name 49.212.204.46;\r\n\r\n        location \/ {\r\n                if ( $cookie_flag = 0 ){\r\n                proxy_pass http:\/\/www.google.co.jp;\r\n                }\r\n\r\n                if ( $cookie_flag = 1 ){\r\n                proxy_pass http:\/\/www.yahoo.co.jp;\r\n                }\r\n\r\n}\r\n<\/pre>\n<p>cookie\u306eauthid\u306e\u5024\u304cabcde\u306e\u3068\u304d\u306f\u3001$cookie_flag\u30921\u306b\u3001\u305d\u308c\u4ee5\u5916\u306f0\u306b\u3059\u308b<br \/>\ncookie\u306e\u5024\u306b\u5fdc\u3058\u3066\u52d5\u4f5c\u3092\u5909\u66f4\u3067\u304d\u308b\u304c\u3001\u3001cookie\u306e\u5024\u3092\u5909\u66f4\u3059\u308b\u305f\u3073\u306b\u8a2d\u5b9a\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3001\u3001\u4ed6\u306e\u65b9\u6cd5\u3092\u6a21\u7d22\u3059\u308b<\/p>\n<h2>ngx_http_userid_module\u306e\u5229\u7528<\/h2>\n<p>cookie\u3092\u64cd\u4f5c\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u3001apache\u3067\u3044\u3046\u3068\u3053\u308d\u306emod_uid<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n        userid          on;\r\n        userid_name     authid_userid;\r\n        userid_domain   49.212.204.46;\r\n        userid_path     \/;\r\n        userid_expires  365d;\r\n        }\r\n<\/pre>\n<p>\/\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001cookie\u304c\u8a2d\u5b9a\u3055\u308c\u308b<br \/>\n<a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-14_115751.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-14_115751.png\" alt=\"2014-07-14_115751\" width=\"547\" height=\"333\" class=\"alignnone size-full wp-image-689\" \/><\/a><br \/>\nuser\u3054\u3068\u306bcookie\u3092\u8a2d\u5b9a\u3057\u3001logformat\u3067$uid_got\u3092\u5229\u7528\u3059\u308b\u3068cookie\u306e\u5024\u304c\u53d6\u5f97\u3067\u304d\u308b\u305f\u3081\u3001\u30e6\u30fc\u30b6\u306e\u52d5\u304d\u3092\u78ba\u8a8d\u3067\u304d\u308b\u304c\u3001\u3001\u4eca\u56de\u306e\u8981\u4ef6\u3068\u306f\u5408\u308f\u306a\u3044\u305f\u3081\u4ed6\u306e\u65b9\u6cd5\u3092\u6a21\u7d22\u3059\u308b<\/p>\n<h2>lua-nginx-module\u306e\u5229\u7528<\/h2>\n<p>Note that the ngx_eval module can be approximated by using rewrite_by_lua<br \/>\nnginx-extras\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3001\u3053\u306e\u624b\u6cd5\u3067\u5b9f\u73fe\u3057\u305f\uff01<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlocation \/ {\r\n        # \u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\r\n        if (-f $request_filename) {\r\n        rewrite_by_lua '\r\n                local res = ngx.location.capture(&quot;\/auth&quot;)\r\n                if res.status == ngx.HTTP_OK then\r\n                        ngx.exit(ngx.OK)\r\n                else\r\n                        return ngx.redirect(&quot;http:\/\/www.google.co.jp\/&quot;)\r\n                end\r\n        ';\r\n        break;\r\n        }\r\n        # \u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3057\u306a\u304b\u3063\u305f\u5834\u5408\r\n        if  (!-e $request_filename) {\r\n                rewrite ^(.*)$ http:\/\/www.yahoo.co.jp permanent;\r\n        }\r\n}\r\n\r\nlocation \/auth {\r\n     internal;\r\n     set $memcached_key $cookie_authid;\r\n     memcached_pass 127.0.0.1:11211;\r\n}\r\n<\/pre>\n<ul>\n<li>rewrite_by_lua\u3067ngx.exit(ngx.OK)\u3092\u5229\u7528\u3059\u308b\u3068\u3001nginx\u306fcontent\u30cf\u30f3\u30c9\u30e9\u306e\u51e6\u7406\u306b\u79fb\u308b<\/li>\n<\/ul>\n<h3>\u304a\u307e\u3051-S3\u3068\u306e\u9023\u643a<\/h3>\n<p><a href=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-21_000216.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tech.akat.info\/wp-content\/uploads\/2014\/07\/2014-07-21_000216.png\" alt=\"2014-07-21_000216\" width=\"716\" height=\"593\" class=\"alignnone size-full wp-image-722\" \/><\/a><\/p>\n<h1>\u53c2\u8003<\/h1>\n<p>http:\/\/livebookmark.net\/journal\/2008\/05\/21\/memcachephp-stats-like-apcphp\/<br \/>\nhttp:\/\/qiita.com\/akiko-pusu\/items\/aae6be5fe4b77f588a80<br \/>\nhttp:\/\/wiki.nginx.org\/HttpLuaModule<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 \u300cnginx\u3067\u8a8d\u8a3c\u7528proxy\u30b5\u30fc\u30d0\u3092\u4f5c\u6210\u300d\u3092\u3082\u3068\u306b\u8a2d\u5b9a \u5168\u3066\u306e\u30a2\u30af\u30bb\u30b9\u3067\u308f\u3056\u308f\u3056PHP\u3092\u547c\u3073\u51fa\u3059\u306e\u306f\u975e\u52b9\u7387\u306a\u306e\u3067\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u306e\u5224\u5b9a\u90e8\u5206\u306fNginx\u306e\u307f\u3067\u89e3\u6c7a\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u9ad8\u901f\u5316\u53ef\u80fd http:\/\/blog.asi [&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":[28,18],"tags":[16],"_links":{"self":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/666"}],"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=666"}],"version-history":[{"count":30,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":1366,"href":"https:\/\/tech.akat.info\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions\/1366"}],"wp:attachment":[{"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.akat.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}