- To build docker image: docker build -t nodeapp .
- To create/run a docker container using image: docker run -d -p 1111:9999 -e APPID=1111 nodeapp
- To start Nginx Server: nginx
- To reload Nginx Server: nginx -s reload
- To stop Nginx Server: nginx -s stop
- cd/loadBalancer
- npm install
- change layer7_http.conf to nginx.conf in /usr/local/etc/nginx/
- docker build -t nodeapp .
- docker run -d -p 1111:9999 -e APPID=1111 nodeapp (first container/server)
- docker run -d -p 2222:9999 -e APPID=2222 nodeapp (second container/server)
- docker run -d -p 3333:9999 -e APPID=3333 nodeapp (third container/server)
- docker run -d -p 4444:9999 -e APPID=4444 nodeapp (fourth container/server)
- Go to
http://127.0.0.1:8008/
- Try refreshing the page (will be round robin)
- Go to
http://127.0.0.1:8008/
- Try refreshing the page, the behavior will be very weird, sometimes it changes
- But it is round robin in a TCP connection, as long as TCP connection is still there it wouldn't create a new TCP connection
- In the command line, try:
- telnet 127.0.0.1 80
- GET /
- Enter
- Do the commands for a couple of times, you should be able to see it using layer 4 LB in round robin
- cd/websocket
- npm install
- node index.js 1111 & node index.js 2222 & node index.js 3333 & node index.js 4444
- Open a new terminal
- nginx -c PATH to tcp.cfg (i.e. nginx -c /Users/Peter/webSocket/tcp.cfg)
- Go to browser development mode -> console
- let ws = new WebSocket("ws://localhost");
- ws.onmessage = e => console.log(e.data)
- ws.send("some data");
- Should always go to the same server until you create a new connection
- nginx -c PATH to ws.cfg (i.e. nginx -c /Users/Peter/webSocket/ws.cfg)
- Go to http://localhost
- Type anything into the input field and hit ENTER
- Repeat the steps, then you should see your message going to the same server
- Try refreshing the page, repeat step 7. You should see a different server (by default it's going to "ws://localhost/wsapp/")
- You can try changing the index.html from "ws://localhost/wsapp/" to "ws://localhost/wschat/"
- You should notice that /wsapp will switch between 1111 and 2222 servers, and /wschat will switch between 3333 and 4444 servers
- Layer 4 Proxying blindly tunnels everything to the Backend
- ws://localhost/ -> websocket app
- ws://localhost/blahblah -> websocket app
- path doesn't matter (path is only for layer 7)
- Layer 7 proxying for websocket
- http://localhost/ -> open main html page
- ws://localhost/wsapp -> websocket app
- ws://localhost/wschat -> another websocket app for chatting
- can't do these in layer 4 since port 80 is blindly tunnels
- tcp.fcg -> Layer 4 configs
- ws.cfg -> Layer 7 configs
- You can create a config file and pass it as a path to Nginx, like tcp.cfg
- nginx -c FILEPATH
- To run node app as a background service
- nohup node index.js &
- To start multiple servers with different port: node index.js 1111 & node index.js 2222