{"id":131,"date":"2011-03-16T18:53:39","date_gmt":"2011-03-16T23:53:39","guid":{"rendered":"http:\/\/soljerome.com\/blog\/?p=131"},"modified":"2011-03-27T12:19:44","modified_gmt":"2011-03-27T17:19:44","slug":"nginx-caching-with-multiple-vhosts","status":"publish","type":"post","link":"https:\/\/www.soljerome.com\/blog\/2011\/03\/16\/nginx-caching-with-multiple-vhosts\/","title":{"rendered":"Nginx caching with multiple vhosts"},"content":{"rendered":"<p>This post discusses the\u00a0nginx <a href=\"http:\/\/wiki.nginx.org\/HttpProxyModule\" target=\"_blank\">proxy module<\/a>. I recently setup Nginx as a reverse caching proxy for various sites. Every configuration example I came across online failed to mention using the <a href=\"http:\/\/wiki.nginx.org\/HttpProxyModule#proxy_cache_key\" target=\"_blank\">proxy_cache_key<\/a> directive. Therefore, I originally ended up with something like this:<\/p>\n<pre># cat \/etc\/nginx\/sites-available\/siriad\r\n# You may add here your\r\n# server {\r\n#       ...\r\n# }\r\n# statements for each of your virtual hosts to this file\r\n\r\nserver {\r\n        listen   [::]:80;\r\n        server_name siriad.com;\r\n        rewrite ^\/(.*) http:\/\/www.siriad.com\/$1 permanent;\r\n}\r\n\r\nserver {\r\n        listen   [::]:80;\r\n        server_name     www.siriad.com\r\n                        testing.siriad.com;\r\n\r\n        access_log      \/var\/log\/nginx\/siriad.com\/access.log;\r\n        error_log       \/var\/log\/nginx\/siriad.com\/error.log;\r\n\r\n        location \/ {\r\n                proxy_pass              http:\/\/backend;\r\n                proxy_set_header        Host $host;\r\n                proxy_cache             siriad;\r\n                proxy_cache_valid       200 1d;\r\n                proxy_cache_use_stale   error timeout invalid_header updating\r\n                                        http_500 http_502 http_503 http_504;\r\n        }\r\n}<\/pre>\n<p>This led to some odd behavior. When I would load www.siriad.com and subsequently load testing.siriad.com, I would end up with the cached content from www.siriad.com for both requests. The cache was working, but was not distinguishing between the two hosts. I spent some time trying different configurations thinking that this was a problem caused by me since I had trouble finding any information on it.<\/p>\n<p>It turns out, this is exactly the use case for the\u00a0\ufeff\ufeff<a href=\"http:\/\/wiki.nginx.org\/HttpProxyModule#proxy_cache_key\" target=\"_blank\">proxy_cache_key<\/a> directive. By adding the following line, I made sure that the hostname was included in the key used to cache the request so that there were no key collisions during the process.<\/p>\n<pre>                proxy_cache_key         \"$scheme$host$request_uri\";<\/pre>\n<p>I was able to find this information after searching around <a href=\"http:\/\/duckduckgo.com\/\">DDG<\/a> for quite a while. I finally came across <a href=\"http:\/\/forum.nginx.org\/read.php?2,1530,1539\">this<\/a> forum post. The result of the above configuration is a working reverse caching proxy using Nginx for siriad.com as well as testing.siriad.com. I am hoping this post is slightly more searchable than the results I was getting while trying to find the answer to this problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post discusses the\u00a0nginx proxy module. I recently setup Nginx as a reverse caching proxy for various sites. Every configuration example I came across online failed to mention using the proxy_cache_key directive. Therefore, I originally ended up with something like this: # cat \/etc\/nginx\/sites-available\/siriad # You may add here your # server { # &#8230; <a href='https:\/\/www.soljerome.com\/blog\/2011\/03\/16\/nginx-caching-with-multiple-vhosts\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,14],"tags":[],"_links":{"self":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/131"}],"collection":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":15,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/posts\/131\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.soljerome.com\/blog\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}