Skip to content
Snippets Groups Projects
Select Git revision
  • noblogs default
  • noblogs-5.7.1
  • upstream
  • noblogs-5.7
  • noblogs-5.6new
  • upstream5.5.1
  • noblogs28dic
  • upstream28dic
  • noblogs-5.5.1
  • noblogs-5.4.2
  • noblogs-5.4_seconda
  • noblogs-5.4
  • noblogs-7c
  • wp5.2.3p3
  • mergedbconf
  • noblogs-5.7.1
  • noblogs.5.7.0p1
  • noblogs-5.7.0
  • noblogs-5.6p3
  • noblogs5.6p2
  • noblogs-5.6p1
  • noblogs-5.6
  • noblogs-5.4.2p1
  • noblogs-5.4.2
  • noblogs-5.4.1
  • noblogs-5.4
  • noblogs-p5.4
  • noblogs-5.3.2p2
  • noblogs-5.3.2p1
  • noblogs-5.3.2
  • noblogs-5.3
  • noblogs-5.2.3p4
  • noblogs-5.2.3p3
  • noblogs-5.2.3p2
  • noblogs-5.2.3p1
35 results

wp-nginx-map.php

Blame
  • wp-nginx-map.php 2.10 KiB
    <?php
    
    // wp-nginx-map.php
    //
    // Stampa una mappa delle assegnazioni blog -> backend, per NGINX.
    // IP interno del master.
    $master_url = file_get_contents('/etc/noblogs/master');
    $noblogs_master = parse_url($master_url)['host'];
    
    // Load wordpress api.
    define('WP_CACHE',false);
    require_once('/opt/noblogs/www/wp-load.php');
    
    // Port that the noblogs apache2 instance is listening on (as a string).
    define('NOBLOGS_PORT','92');
    
    // Return all blogs.
    function get_blogs() {
      global $wpdb;
      $sql = "SELECT blog_id, domain FROM $wpdb->blogs WHERE deleted = 0 AND archived = '0' ORDER BY domain ASC";
      $result = $wpdb->get_results($sql);
      return ($result);
    }
    
    function printline($s) {
      echo $s . "\n";
    }
    
    function backend_to_http_endpoint($backend) {
      if (substr($backend, 0, 8) != 'backend_') {
        error_log('diamine, di questo backend non so che farmene: ' . $backend);
        return $noblogs_master . ':' . NOBLOGS_PORT;
      }
      $id = substr($backend, 8);
      return '172.16.1.' . $id . ':' . NOBLOGS_PORT;
    }
    
    // Print the blog -> backend map.
    function generate_backend_map($blogs) {
      global $wpdb;
      global $noblogs_master;
      $wpdb_hash = &$wpdb->hash_map;
    
      printline('map $http_host $backend_noblogs {');
      printline(' default http://' . $noblogs_master . ':' . NOBLOGS_PORT . ';');
      foreach ($blogs as $blog) {
        $blog_id = $blog->blog_id;
        if ($blog_id == 1)
          continue;
        $backend_id = $wpdb_hash->lookup($blog_id);
        $backend_http = backend_to_http_endpoint($backend_id);
        printline(' ' . $blog->domain . ' http://' . $backend_http . ';');
      }
      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_maps();