Nginx ~ Configuration - rohit120582sharma/Documentation GitHub Wiki

Nginx consists of modules which are controlled by directives specified in the configuration file. Directives are divided into simple directives and block directives.

A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;). A block directive has the same structure as a simple directive, but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({ and }). If a block directive can have other directives inside braces, it is called a context.

Directives placed in the configuration file outside of any contexts are considered to be in the main context. The events and http directives reside in the main context, whereas server in http, and location in server.

There can only be one events context in the whole configuration file. It has worker_connections directive which controls the maximum amount of concurrent worker connection that nginx would handle (multiplied by the number of worker processes or CPU cores). That is, how many people can be served simultaneously by nginx. The default value of 1024 is fine for most cases.

# some simple main directives
user nobody;

error_log logs/error.log notice;

worker_processes 1;


Configure Nginx as a Web Server

http context is the most important part of nginx configuration if you are going to use it as a web server.

The configuration defines a set of virtual servers that control the processing of requests for particular domains or IP addresses.

Each virtual server for HTTP traffic defines special configuration instances called locations that control processing of specific sets of URIs.

It is possible to add multiple server directives into the http context to define multiple virtual servers.

Serving static content

# Configuration of connection processing
events {
}

http {
	# HTTP virtual server configuration
	server {
		listen			80 default_server;
		server_name		example.com www.example.com;
	}
	server {
		listen			8080;
		server_name		127.0.0.1:8080;
		root			/data/www;

		# Configuration for processing URIs starting with '/'
		location / {
			index index.htm index.html;
		}

		# Configuration for processing URIs starting with '/images/'
		location /images/ {
			include /images;
		}
	}
}

Setting up a simple Proxy Server

http {
	server {
		listen			8080;
		server_name		www.example.com;

		location / {
			proxy_pass http://localhost:3000;
		}
	}
}


⚠️ **GitHub.com Fallback** ⚠️