From 8271444903fa62a7cba09d3059ddc18df8f3002f Mon Sep 17 00:00:00 2001
From: shammash <shammash@autistici.org>
Date: Sun, 4 Jun 2017 09:57:46 +0100
Subject: [PATCH] Add blog->site map

We want per-blog traffic stats.

Apache noblogs is configured with a single virtualhost, we cannot use the
virtualhost name (%v) for these stats, because everything will appear as
noblogs.org .

We cannot even use the Host header (%{Host}i) because the virtualhost wildcard
configuration allows for bogus blog names (e.g. cavalle55e.noblogs.org instead
of cavallette.noblogs.org).

With this map nginx can lookup a valid blog name (or default to noblogs.org for
invalid names) and pass it to apache with an internal header (e.g.
X-AI-Noblogs-Site ). Apache will then use this header to produce correct stats.

Signed-off-by: shammash <shammash@autistici.org>
---
 wp-nginx-map.php | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/wp-nginx-map.php b/wp-nginx-map.php
index f01c18f79..ec241f080 100644
--- a/wp-nginx-map.php
+++ b/wp-nginx-map.php
@@ -33,13 +33,11 @@ function backend_to_http_endpoint($backend) {
 }
 
 // Print the blog -> backend map.
-function generate_map() {
+function generate_backend_map($blogs) {
   global $wpdb;
   global $noblogs_master;
   $wpdb_hash = &$wpdb->hash_map;
 
-  $blogs = get_blogs();
-
   printline('map $http_host $backend_noblogs {');
   printline(' default http://' . $noblogs_master . ':82;');
   foreach ($blogs as $blog) {
@@ -53,5 +51,24 @@ function generate_map() {
   printline('}');
 }
 
+// Print the blog -> site map.
+// This is used to send apache the right site name for logging purposes.
+function generate_site_map($blogs) {
+  printline('map $http_host $site_noblogs {');
+  printline(' default noblogs.org;');
+  foreach ($blogs as $blog) {
+    $blog_id = $blog->blog_id;
+    if ($blog_id == 1)
+      continue;
+    printline(' ' . $blog->domain . ' ' . $blog->domain . ';');
+  }
+  printline('}');
+}
+
+function generate_maps() {
+  $all_blogs = get_blogs();
+  generate_backend_map($all_blogs);
+  generate_site_map($all_blogs);
+}
 
-generate_map();
+generate_maps();
-- 
GitLab