Nginx Server

Nginx Server

README

This is a guide for the installation and configuration of a nginx server for a local development environment (even so this guide can be used to install nginx in a production server with the same functionality).

The variables, commands and overall process that will be presented here are based on a Debian based distribution and with a few modifications you can be able to accomplish the same result in other systems.

Installation

The latest version available at the time in which this guide was written is v1.4.4 and you can get it from this page Nginx Download.

$ cd /opt/
$ wget -c 'http://nginx.org/download/nginx-1.4.4.tar.gz'
$ tar xfz nginx-1.4.4.tar.gz && rm -f nginx-1.4.4.tar.gz
$ mv -i nginx-1.4.4/ nginx && cd nginx/
$ mkdir source && mv -i * source/ && cd source/
$ ./configure --prefix=/opt/nginx
$ make && make install
$ echo 'export PATH="$PATH:/opt/nginx/sbin"' >> ~/.bashrc
$ source ~/.bashrc
$ IFS=':'; for path in $PATH; do echo $path; done

Documentation

This guide gives a basic introduction to nginx and describes some simple tasks that can be done with it. This guide also describes how to start and stop nginx, and reload its configuration, explains the structure of the configuration file and describes how to set up nginx to serve out static content, how to configure nginx as a proxy server, and how to connect it with a FastCGI application.

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. nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes. The number of worker processes is defined in the configuration file and may be fixed for a given configuration or automatically adjusted to the number of available CPU cores. See worker_processes.

The way nginx and its modules work is determined in the configuration file. By default, the main configuration file is named nginx.conf and is placed at /opt/nginx/conf/nginx.conf. In other installations followed by a package manager like apt or yum you will find this file under one of these directoryes:

  • /etc/nginx/nginx.conf
  • /usr/local/nginx/conf/nginx.conf
  • /etc/nginx/nginx.conf
  • /usr/local/etc/nginx/nginx.conf

Services

To start nginx, run the executable file located at /opt/nginx/sbin/nginx, you will need root privileges to do that if you are trying to start the server in a priority port of your system. Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:

$ nginx -s signal
# Where signal may be one of the following:
  stop   - fast shutdown
  quit   - graceful shutdown
  reload - reloading the configuration file
  reopen - reopening the log files

You can start the nginx server basically using this command:

$ sudo /opt/nginx/sbin/nginx
$ curl --head http://127.0.0.1:80/
  HTTP/1.1 200 OK
  Server: nginx/1.4.4
  Date: Sun, 01 Dec 2013 03:58:54 GMT
  Content-Type: text/html
  Content-Length: 612
  Last-Modified: Sun, 01 Dec 2013 00:56:06 GMT
  Connection: keep-alive
  ETag: "529a8926-264"
  Accept-Ranges: bytes

For more information on sending signals to nginx, see controlling nginx.

Configuration

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 { ... }. If a block directive can have other directives inside braces, it is called a context. Common examples of directives: events, http, server, and location.

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, server in http, and location in server.

Note: The rest of a line after the # sign is considered a comment.

Do you have a project idea? Let's make it together!