Nginx
What Is Nginx?
Nginx is a free, open-source web server developed by Russian developer Igor Sysoev.
It was proposed as an alternative web server to Apache. Apache had a weakness when handling many simultaneous connections. Problems could occur when there were around 10,000 clients. Nginx was developed as web server software that can withstand high load.
What Is a Web Server?
A web server receives requests from outside, returns static content from the web server, and when dynamic content is requested from an application server, returns the application server’s processing result to the client.
Nginx is one example of this kind of web server.
Differences Between Apache and Nginx
Apache is often mentioned together with Nginx. Apache is a representative web server. Nginx was originally created to solve the problem where Apache and ordinary web servers respond poorly under a large number of simultaneous accesses.
Main Differences
Apache characteristics
- It has rich functionality as a web server and is easy to use.
- Because it provides many features, server cost is high.
Nginx characteristics
- It has reverse proxy functionality, acting as an intermediary for web servers and responding to requests, and is specialized for processing simultaneous requests.
- It supports non-blocking I/O and is specialized for large data transfers and many simultaneous connections.
- Because it is simple, server cost is low.
- One advantage is that it can hide the existence of the web server, and when there are multiple web servers, it can distribute processing. In other words, it can function like a load balancer. This gives it strong characteristics for many simultaneous requests.
How Apache and Nginx Run
Apache uses a multi-process, process-centered architecture.
- Because each request is assigned to a process, many processes start at the same time when a large number of requests arrive, which has the disadvantage of creating very large overhead.
Nginx uses a single-threaded, event-driven architecture.
- Like Node.js, it uses an event loop model, where a single thread runs a loop and processes events accumulated in a queue.
- By adopting the event loop model, it can process a large number of requests with only a small number of processes.
- This makes it possible to solve the C10K problem.
Installing Nginx
Install on Mac
Install using the brew command.
% brew install nginx
Run the following command and access http://localhost:8080.
% nginx
Stop it with the following command.
% nginx -s stop
The configuration file exists at the following path.
/usr/local/etc/nginx/nginx.conf
Install on CentOS
Create /etc/yum.repos.d/nginx.repo.
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
Install with the yum command.
$ sudo yum install nginx
Nginx Commands
Start
nginx
Stop
CentOS 6, Amazon Linux
nginx -s stop
CentOS 7, Amazon Linux 2
systemctl stop nginx
Restart
CentOS 6, Amazon Linux
/etc/init.d/nginx restart
CentOS 7, Amazon Linux 2
systemctl restart nginx
Register for Startup
CentOS 7, Amazon Linux 2
systemctl enable nginx # register
systemctl disable nginx # remove
Reload Configuration
CentOS 6, Amazon Linux
nginx reload
service nginx reload
CentOS 7, Amazon Linux 2
systemctl reload nginx
Check Configuration File
nginx -t
Configuration File Locations
Main configuration file
cd /etc/nginx/
Per-server configuration files
cd /etc/nginx/conf.d/