Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
M ABD AZIZ ALFIAN edited this page Mar 9, 2020 · 21 revisions

Welcome to the cluster-static-server wiki!

Table Of Contents

Get Started

  1. Clone or Download this repo.
  2. Extract it.
  3. Go to the extracted directory.
  4. Run $ npm install.
  5. Done

How to run the server

  • To start master server $ npm run master or $ node server-master.js.
  • To start node server $ npm run node or $ node server-node.js.

Note :

  • You are able to start master and node server in same machine server with different port. But, actualy Master and Node server should be on different machine server so you will get the best performance.

Back to top

How This Work

This cluster static server is working under HTTP for communicate with all nodes server.

  • MASTER
    Master server is for proxy to all nodes and management file through API.

  • NODE
    Node server is for storage file and serving the static file for public.

  • Static File
    Your public file is located at

    Node 1
    http://localhost:3000/ns1/2020/01/09/lgsfdkjhasdufgsdf.png
    
    Node 2
    http://localhost:3000/ns2/2020/01/09/jhkuwuerkfndskhsk.png
    

    Actualy your nodes is serving your file to public but the url file is proxied by master server.
    See at /ns1/ and /ns2/ above.

Back to top

Example Case

Assume you have already 3 machine server.

  • Master is on 10.0.0.11:3000
  • Node 1 is on 10.0.0.12:3001 >> give it prefix name with /ns1
  • Node 2 is on 10.0.0.13:3002 >> give it prefix name with /ns2

Because static server is for public use, so you have to set domain name for Master server.

10.0.0.11 >> static.yourdomain.com
10.0.0.12 >> no need to have a domain
10.0.0.13 >> no need to have a domain

Configuration Master

See example configuration based case above.

// all this config is required
const config_master = {

    // x_token header authentication for master API
    x_token: '12345678',

    // x_token header authentication for node API
    node_x_token: '87654321',

    // set origin for master server
    // Origin is the full base url of master server.
    // You can change this origin with domain name like http://yourdomain.com
    origin: 'http://static.yourdomain.com', // without trailing slash

    // set this master server port
    masterPort: 3000,

    // node server list
    nodeServer: [ // you can add more node server
        { upstream:'http://10.0.0.12:3001', prefix:'/ns1', http2: false },
        { upstream:'http://10.0.0.13:3002', prefix:'/ns2', http2: false }
    ],

    // Limit upload size
    limitSize: (50 * 1024 * 1024), // 50Mb

    // Block upload to spesific node destination. 
    blockNode: [], // Ex. ['/ns2','/ns3'] means the file will not upload to /n2 and /ns3.

    // Print the log
    logger: true
    
}

module.exports = config_master;

Configuration Node

Remember that Node 1 prefix name is /ns1 and Node 2 prefix name is /ns2. So you only have to set nodePrefixName in config-node.js. And check for the nodePort.

Example for Node 1:

    // set node prefix name for this node server
    nodePrefixName: '/ns1', // without trailing slash but leading slash is required

    // set node port for this node server
    nodePort: 3001,

Example for Node 2:

    // set node prefix name for this node server
    nodePrefixName: '/ns2', // without trailing slash but leading slash is required

    // set node port for this node server
    nodePort: 3002,

Back to top

Example API

We include the postman.json file in this source for an example how to use API. Just import it to your postman application and then you can learn from it.

Note :

  • Upload and Delete must be using API from master server because API for node server is for development only.

Back to top

Troubleshoot

  • Can't send request to node server

    1. Before you add your node server upstream into config-master.js in master server, make sure you have already test http://YOUR_IP_NODE:PORT/status is accessed for public.
    2. Better to use IP address for all node server, because using domain sometimes slower caused by DNS resolve time.
  • Can't pass request header in NGINX
    If you can't pass the request header in NGINX, you need to add this line below in your nginx.conf

underscores_in_headers on;

Back to top

Limitation

This cluster-static-server project was design for flexible to use or to fork. Also I want to keep this project always minimalis, so there is bit limitations in this project :

  • No Complex Authentication
    I did not implement any complex authentication in this project because I want you to be more flexible to choose any kind of authentication method for your business application. Actualy for simple authentication by header is already implemented in this project. Just improve it by yourself so it can be suitable to your business application.
  • No Update File
    Naturaly a static server will serve "as it" file to your browser with long cache expiring time. So update file feature could be useless for static server. But you can create your own application to manage your file with this cluster-static-server API.

Back to top

Need Support ?

This project is ready for Production, I've use this cluster-static-server in big cloud microservices infrastructure with kubernetes.

I'm as the author of this project will help you for:

  1. Adding secure authentication with Basic Auth, Session or JWT?
  2. Configure Reverse Proxy with NGINX?
  3. Solving your server problem?

Just buy me coffee
Bitcoin Wallet: 1MtPkZmYEBtQz3odhubB1XpJXyHpV15dYj

Then let me know via Whatsapp : +628192222254

Back to top