Overview

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP proxy server.

For a long time, it has been running on many heavily loaded Russian sites, and recently, Netflix, Wordpress.com.

Ref: nginx

nginx has one master process and several worker processes. The main purpose of the master process is to read and evaluate configuration, and maintain worker processes. Worker processes do actual processing of requests.

Basics

start nginx

nginx

stop nginx

nginx -s signal

signal:

  1. stop — fast shutdown

  2. quit — graceful shutdown

  3. reload — reloading the configuration file

  4. reopen — reopening the log files

List of running nginx process

ps -ax | grep nginx

config file: nginx.conf

nginx.conf

By default, the configuration file is placed in /etc/nginx. (If not, check /usr/local/nginx/conf, or /usr/local/etc/nginx.)

first part

1
2
3
4
5
6
7
8
user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}
  1. user

    Defines which Linux system user will own and run the Nginx server. Most Debian-based distributions use www-data

    In case you run two simultaneous web servers, or need another program’s user to have control over Nginx, you can play with it.

  2. worker_process

    Defines how many threads, or simultaneous instances, of Nginx to run.

  3. pid

    Defines where Nginx will write its master process ID, or PID.

    On my machine, the file /run/nginx.pid store a single number 724.

http part

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

Note: The HTTP block of the nginx.conf file contains the statement include /etc/nginx/sites-enabled/*;. This allows for server block configurations to be loaded in from separate files found in the sites-enabled sub-directory.

Usually these are symlinks to files stored in /etc/nginx/sites-available/. By using symlinks you can quickly enable or disable a virtual server while preserving its configuration file.

config file for sites

sites-available

/etc/nginx/sites-available/default is a template provided for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}

Do not modify this file. Generally, you’ll want to make a separate file with its own server block for each virtual domain on your server. So do:

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

listen directive

1
2
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

The argument default_server means this virtual host will answer requests on port 80 that don’t match any other virtual host’s listen statement.

The second statement listens over IPv6 and behaves in the same way.

1
2
listen     127.0.0.1:80;
listen localhost:80;

Note: localhost is conventionally set as the hostname for 127.0.0.1 in /etc/hosts.

1
2
listen     80;
listen *:80;

This will listen on all domains and IP addresses on port 80. listen 80; is equivalent to listen *:80;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	listen     12.34.56.77:80;
listen 12.34.56.78:80;
listen 12.34.56.79:80;
listen 12.34.56.79:8080;
```

This will listen on multiple IP/port.

## server_name directive

Common:

server_name example.com;

Or

server_name example.com www.example.com;

Now this:

server_name   *.example.com;
server_name   .example.com;
1
2
3
4
5
6
7
8
9
10
11
12
13

will listen to all subdomains of example.com, including www.example.com, foo.example.com, etc. __Both equivalent__.

server_name "";

If you set server_name to the empty quote set (”“), Nginx will process __all requests that either__ do not have a hostname, __or__ that have an unspecified hostname, such as requests for the IP address itself.

## location directive

__location__ setting lets you configure how Nginx will respond to requests for resources within the server.

eg.

location / { } 
location /images/ { } 
location /blog/ { } 
location /planet/ { } 
location /planet/blog/ { }

The location can set actual path in the file system (that is under /etc/nginx/ folder). Or we can also set __proxy_pass__ directive. 

Ref 1: [nginx Beginner’s Guide](http://nginx.org/en/docs/beginners_guide.html)


Ref 2: [Basic Nginx Configuration by Linode](https://www.linode.com/docs/websites/nginx/basic-nginx-configuration#location-root-and-index)