Cowman's Gitbook

Gitbook 使用 Nginx 做為 Proxy

因為Gitbook預設是以自帶的 http port 4000 啟動網頁伺服器,同時會啟動 http port 35729 的 loadreload 伺服器,所以這邊將會使用 Nginx Proxy 將「網址/gitbook」導至「http://localhost:4000」。

  • 修改 Gitbook 網頁伺服器預設埠

    預設執行 Gitbook 指令為

    git serve
    

    若要更改預設的埠 (預設: 4000)

    git serve -p [欲使用的埠]
    

    關於 gitbook 這個指令的介紹如下

    Usage: gitbook [options] [command]
    
    Commands:
    
      build [options] [source_dir] Build a gitbook from a directory
      serve [options] [source_dir] Build then serve a gitbook from a directory
      install [options] [source_dir] Install plugins for a book
      pdf [options] [source_dir] Build a gitbook as a PDF
      epub [options] [source_dir] Build a gitbook as a ePub book
      mobi [options] [source_dir] Build a gitbook as a Mobi book
      init [source_dir]      Create files and folders based on contents of SUMMARY.md
      publish [source_dir]   Publish content to the associated gitbook.io book
      git:remote [source_dir] [book_id] Adds a git remote to a book repository
    
    Options:
    
      -h, --help     output usage information
      -V, --version  output the version number
    

    這邊列出 gitbook serve 的參數說明

    Usage: serve [options] [source_dir]
    
    Options:
    
      -h, --help                output usage information
      -v, --verbose             Activate verbose mode, useful for debugging errors
      -o, --output <directory>  Path to output directory, defaults to ./_book
      -f, --format <name>       Change generation format, defaults to site, availables are: site, page, ebook, json
      --config <config file>    Configuration file to use, defaults to book.js or book.json
      -p, --port <port>         Port for server to listen on
      --no-watch                Disable restart with file watching
    
  • 修改 Gitbook loadreload 伺服器預設埠

    編輯檔案 /usr/bin/gitbook

    <略>
      // init livereload server
      var lrOptions = {port: 35729};
      // 將 35729 改成要使用的埠
      var lrServer = tinylr(lrOptions);
      var lrPath = undefined;
      lrServer.listen(lrOptions.port, function(err) {
        if (err) { return console.log(err); }
        console.log('Live reload server started on port: ' + lrOptions.port);
      });
    
  • 設定 Nginx Proxy

    編輯檔案 /etc/nginx/sites-enable/default

          location /gitbook {
                  rewrite ^/gitbook(/.*)$ $1 break;
                  proxy_pass        http://localhost:4000/gitbook;
                  proxy_set_header  Host $host;
                  proxy_buffering   off;
          }
    
  • 修改 Gitbook loadreload client js

    編輯檔案 /usr/lib/node_modules/gitbook/node_modules/gitbook-plugin-livereload/book/plugin.js

    if (firstScriptTag) {
      newEl.async = 1;
      newEl.src = '//' + window.location.hostname + ':35729/livereload.js';
      // 將 35729 更改為使用的埠
      firstScriptTag.parentNode.insertBefore(newEl, firstScriptTag);
    }
    
  • 修改 Gitbook 只透過 HTTP 進行

    因為測試的站點有支援 HTTPS,會導致 HTTP、HTTPS 間切換的問題,所以針對 Gitbook 僅提供 HTTP 的訪問方式。 編輯檔案 /usr/lib/node_modules/gitbook/theme/templates/layout.html,加入 Javascript 語法。

    <!DOCTYPE HTML>
    <html lang="en-US" {% block htmlTag %}{% endblock %}>
      <script>
        if (window.location.protocol != "http:")
          window.location.href = "http:" + window.location.href.substring(window.location.protocol.length);
      </script>
    
  • 假設要修改 Gitbook loadreload ws 的設定 編輯檔案 /usr/lib/node_modules/gitbook/node_modules/tiny-lr-fork/lib/public/livereload.js

    this._uri = "ws" + (this.options.https ? "s" : "") + "://" + this.options.host + ":" + this.options.port + "/livereload";