{"id":613,"date":"2010-02-17T09:00:09","date_gmt":"2010-02-17T17:00:09","guid":{"rendered":"http:\/\/onehub.com\/blog\/?p=613"},"modified":"2019-01-11T15:04:15","modified_gmt":"2019-01-11T23:04:15","slug":"tasty-cookies-with-nginx","status":"publish","type":"post","link":"https:\/\/www.onehub.com\/blog\/2010\/02\/17\/tasty-cookies-with-nginx\/","title":{"rendered":"Tasty Cookies with nginx"},"content":{"rendered":"<p>In a <a href=\"https:\/\/www.onehub.com\/blog\/2009\/03\/06\/rails-maintenance-pages-done-right\/\">previous post<\/a> I talked about the best way to handle putting an application into maintenance mode. In addition to sending the appropriate status code (503) for machines, <a href=\"http:\/\/nginx.org\/\">nginx<\/a> is configured to serve a helpful page to inform users of the maintenance window. Today, I would like to build on this configuration to make it possible for a few users to test the application while it is in maintenance mode for the rest of the world. There are a few different ways this could be handled, but HTTP cookies are a simple and flexible solution.<\/p>\n<p>Recall these directives which instruct nginx to check for our maintenance page and set the status code to 503:<\/p>\n<pre><pre class=\"brush: bash; light: true; title: ; notranslate\" title=\"\">\nif (-f $document_root\/system\/maintenance.html) {\n  return 503;\n}\n<\/pre>\n<p>Let&#8217;s modify this with a variable to track state. Now, we can add an additional check for anything we would like to turn $maint off. <a href=\"http:\/\/wiki.nginx.org\/NginxHttpCoreModule#variables\">$http_cookie<\/a> holds all the cookies for the request:<\/p>\n<pre><pre class=\"brush: bash; light: true; title: ; notranslate\" title=\"\">\nset $maint off;\n\nif (-f $document_root\/system\/maintenance.html) {\n  set $maint on;\n}\n\nif ($http_cookie ~* &quot;topsekrit&quot; ) {\n  set $maint off;\n}\n\nif ($maint = on) {\n  return 503;\n}\n<\/pre>\n<p>You can now set a cookie with a value of &#8220;topsekrit&#8221; and nginx will let you circumvent the maintenance page to test your new code before you release it to the world.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post I talked about the best way to handle putting an application into maintenance mode. In addition to sending the appropriate status code (503) for machines, nginx is configured to serve a helpful page to inform users [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/posts\/613"}],"collection":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/comments?post=613"}],"version-history":[{"count":0,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/posts\/613\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/media?parent=613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/categories?post=613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onehub.com\/blog\/wp-json\/wp\/v2\/tags?post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}