c# nginx-setup Setting up and understanding basic functionalities of nginx
Initially, nginx was a piece of software that runs on server machine and can respond to request from browser and when number of requests increased the number of nginx server increased then to handle number of requests navigation to number of nginx server the Nginx acted as a load balancer which distribute incoming traffic to multiple server to avoid redundancy which is reverse proxied and distributed the load to different nginx servers using load balancing methods like least connections and round robin algorithm
Nginx = Web Server and Proxy Server.
Load BalancerCaching: Fetching data from remote servers or database on each request results in slower response times. Instead do fetching of data just once, store the response and send whenever requested.One entrypoint: Proxy is publicly accessible only and acts as shield for many other servers which enhance securityEncrypted communication: Proxy server should get encrypted message and pass on encrypted traffic to other server by configuring proxy for SSL encryptionCompression: Configured to compress large video and image files to reduce bandwidth usage and improve load times and break files into chunks (video streaming)web app firewallinternal ddos protectionapi gateway
Nginx has nginx.conf file located in /etc/nginx configure using directives and blocks as web server or proxy server.
All http request should be sent to https endpoint where we have https server configured which serves the file and have ssl certificate configured
Directives: Location -> Defines how server should process the specific types of request
In Kubernetes, the nginx is used as ingress controller. The request from Cloud load balancer is forwarded to nginx ingress controller and then reaches to the cluster

- Install npm
- Install express
- Create server.js and write the routes for '/' and 'images' and add port number to serve
- Create index.html
When we run the app and hit on localhost:8080 browser directly request the server No security.
We are creating container environment:
- Install node
- Create app folder and set as working directory
- Copy files like server.js, index.html, images, package.json from local to Docker image
- Install dependencies and run app
- Start docker in your local
- Verify by running
docker info - Run
docker run hello world-> This will pull hello world image from docker registry - Build the image of our app by running:
docker build -t blogapp:1.0 .
- Check if image is built by:
docker images | grep blogapp
- Bind port from local port to docker container port
docker run -p 3000:3000 blogapp:1.0 - The app will run on localhost:3000
- Add 3 services app1, app2, app3 and define the ports
- Pass the environment variable from docker-compose.yaml to application in server.js to check which container is running
- const replicaApp = process.env.APP_NAME
Output: app1-1 | Request served by App1 app2-1 | Request served by App2 app3-1 | Request served by App3
We dont have to access each replica separately one by one so we need only 1 entry point which then forwards request to different containers.
- Install nginx on macOS:
brew install nginx - Check version:
nginx -v - To find location of nginx installed:
whereis nginx - To find the configuration file contents:
cat etc/nginx/nginx.conf
- worker_processes and master_process
Directives- Key value pairs are called directives.- master_processes starts automatically as the nginx run. This handles all the worker_processes which actually handle the incoming requests. We can define how many worker_processes should run and each worker processes can handle how many connections via
worker_connections worker_processeshandles request usingsingle threaded event loop. Here single thread handles multiple task through asynchronous operation. We haveevent queuewhere all the operations like incoming request/IO operations are placed.- When event loop receives request it places it starts the task like querying db and it does not wait instead it moves to next task and initial task continues in background and when its done its added to queue. Event loop continuously checks the event queue for completed task and executes them in order. So in similar way
worker_processeshandles using event loop. - Number of worker processes can influence how well the nginx can handle traffic. More processes more better but worker processes should be configured as per cpu cores. Where 1 workerprocess = 1 core
- events - general connection processes
- http - http traffic. How client request is handled?
- mail - mail traffic
- stream - tcp udp
Contexts placed outside of these are considered to be in main context
http- how to handle http request from domain or ip?- How to listen for connections
- which domain or subdomain the configuration applies to
- how to route requests
server block -> defines an HTTP server that listens for incoming requests
- Set nginx to listen on port 8080
listen 8080 - Set which ip or domain should of the request. Where is the client sending the response to?
server_name localhost(ip or domain)
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://nodejs_cluster;
}
}
-
upstream: Group of backend servers like app1,app2,app3 running on different ips or ports. The upstream block help with load balancing -
include mime.types- file types to be sent as response like index.html, images etc -
load balancing algorithm: Which algo to use to route requests to server. types: Round robin, least connections, ip hash