Share this page:
Nginx is the second most popular open source web server behind Apache. Nginx is open source, but some add-ons are available as part of a paid subscription. According to W3Techs.com, it is used in approximately 23% of all websites.
Nginx uses an asynchronous event processing model instead of a thread based model. Apache needs one thread per connection, whereas Nginx uses one thread per CPU, and each Nginx thread can handle many connections. Nginx tends to use less memory than Apache, and performs very well under a heavy load.
Before installing Nginx, make sure that your server is up to date.
Now install and start Nginx:
Visit your server's IP address in your browser, and you should see a sample page like this:
The configuration files for Nginx are stored in /etc/nginx. There are some global settings in /etc/nginx/nginx.conf that control the server as a whole, and /etc/nginx/sites-available contains configuration files for individual virtual servers.
Virtual servers are a lot like virtual hosts in Apache; they are a groups of configuration options that describe how the server should handle traffic for different sites.
Settings are grouped into contexts. Some contexts can contain other contexts, creating a hierarchy. The http context contains settings that control the HTTP protocol. Virtual server settings are stored in server contexts, which are contained in the http context. Locations are contexts that store settings for URLs, and they are contained within a server context.
Server contexts are usually stored in a seperate file than the http context, and the include directive is used to import code for virtual servers. This line is used in the http context to include site-specific configuration files:
Nginx.conf contains server-wide options. The first options set Nginx's username and the number of worker processes:
The number of worker processes should be set to the number of CPU cores in the server. There's no benefit to creating additional processes.
The events context contains a setting named worker_connections which determines how many simultaneous connections Nginx will attempt to handle. If you increase this number, make sure that you also increase the maximum number of files that the OS can open with ulimit.
The http context contains settings for handling TCP connections, logging and content compression with gzip.
Extra configuration files can be stored in /etc/nginx/conf.d/. This line of code ensures that as long as they have a .conf extension, they will be included in nginx.conf.