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:
stop — fast shutdown
quit — graceful shutdown
reload — reloading the configuration file
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 | user www-data; |
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.
worker_process
Defines how many threads, or simultaneous instances, of Nginx to run.
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 | http { |
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 | server { |
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 | listen 80 default_server; |
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 | listen 127.0.0.1:80; |
Note: localhost is conventionally set as the hostname for 127.0.0.1 in /etc/hosts.
1 | listen 80; |
This will listen on all domains and IP addresses on port 80. listen 80; is equivalent to listen *:80;
1 | listen 12.34.56.77:80; |
server_name *.example.com;
server_name .example.com;
1 |
|
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)