Engine X (Nginx) is a lightweight Web server, reverse proxy server, and email (IMAP/POP3) proxy server. It was developed in C by Igor Sysoev from Russia while working for Rambler Media.
Igor Sysoev open-source Nginx code and gives it the freest 2-clause BSD-like license. Because Nginx use based on the event-driven architecture is able to handle millions of level of concurrent TCP connections, highly modular design and free license make extension Nginx function of third-party modules emerge in endlessly, brought excellent stability and good design, so it is widely used as a Web server to the website of the heavy traffic.
In the so-called Reverse Proxy mode, a Proxy server receives Internet connection requests, forwards the requests to a server on the internal network, and returns the result obtained from the server to the client requesting Internet connection. In this case, the proxy server acts as a reverse proxy server.
Since there are reverse proxies, there are also forward proxies. A forward proxy is a server that sits between a client and the original server. In order to get content from the original server, the client sends a request to the proxy and specifies the destination. The proxy then forwards the request to the original server and returns the obtained content to the client.
It can be said that the forward proxy represents the client and the reverse proxy represents the server.
Using Nginx has the following advantages:
advantage | instructions |
---|---|
faster | Nginx can respond to requests faster than other Web servers |
High scalability | Nginx has a low-coupling design with many excellent third-party modules |
High stability | Each worker process is relatively independent. When one worker process fails, the master process can quickly “pull up” the new worker child process to provide services |
Low memory consumption | Typically, 10,000 inactive HTTP keep-alive connections consume only 2.5MB of memory in Nginx, which is the foundation of Nginx’s support for high concurrency connections. The single machine supports more than 100,000 concurrent connections, which is not the limit, the limit depends on memory |
Hot deployment | The separation of the ASTER management process from the worker process enables Nginx to provide hot deployment, that is, upgrade Nginx executables with 7×24 hours of uninterrupted service. Of course, it also supports the ability to update configuration items and change log files without stopping the service. |
The most liberal BSD license agreement | This is a powerful incentive for Nginx to grow quickly. The BSD license not only allows users to use Nginx for free, it also allows users to directly use or modify the Nginx source code in their own projects and then distribute it. |
Rich modules | Nginx has numerous official function modules, third-party function modules make Nginx can meet most application scenarios, these function modules can be stacked to achieve more powerful, complex functions, some modules also support Nginx and Perl, Lua and other scripting languages integration work, greatly improve the development efficiency. |
Dependent libraries
Servers now typically run Linux, and before you can compile and install Nginx, you need to install the libraries it relies on.
Here are a few libraries necessary to perform the most basic functions of a Web server.
GCC
GNU Compiler Collection (GCC) is used to compile C programs.
Nginx usually does not provide binary executables directly, so we need to compile their source code.
And we’ll probably be writing the Nginx HTTP module in C++, which will require the G++ compiler.
Install G++ compiler with yum:
yum install -y gcc-c++
Copy the code
PCRE
The Perl Compatible Regular Expressions library (PCRE) is a Perl Compatible Regular Expressions library developed by Philip Hazel. The PCRE library supports Regular Expressions. It evolved from the RegEx, and in fact, Perl regular expressions came from the RegEx written by Henry Spencer.
If we use regular expressions in the configuration file nginx.conf, then we must compile the PCRE library into Nginx when compiling nginx, because nginx’s HTTP module relies on it to parse regular expressions.
Of course, you don’t need to install regular expressions if you’re sure you won’t use them.
The yum installation is as follows:
yum install -y pcre pcre-devel
Copy the code
Pcre-devel is a library for secondary development using PCRE, including header files, which is also required to compile Nginx.
Zlib library
The zlib library is used to compress the contents of HTTP packages in gzip format. If we configure gzip on in nginx.conf and specify gzip for some content-type HTTP responses to reduce network traffic, then, Zlib must be compiled into Nginx at compile time.
The yum installation is as follows
yum install -y zlib zlib-devel
Copy the code
Similarly, zlib is used directly and zlib-devel is needed for secondary development.
OpenSSL development libraries
If our server is not only going to support HTTP, but also needs to transport HTTP over the more secure SSL protocol, then we need to have OpenSSL.
Also, if we want to use hash functions such as MD5 and SHA1, we need to install it.
The yum installation is as follows:
yum install -y openssl openssl-devel
Copy the code
Download the source package
Go to the download page of the official Nginx website and select the latest stable version.
Then use the wget command to download:
[root@host nginx]Wget # http://nginx.org/download/nginx-1.16.0.tar.gz- the 2019-05-23 03:28:52 - http://nginx.org/download/nginx-1.16.0.tar.gz Resolving nginx.org... 62.210.92.35 95.211.80.227, 2001:1 af8:4060: a004:21: : e3 Connecting to nginx.org | 62.210.92.35 | : 80... connected. HTTP request sent, awaiting response... 200 OK Length: 1032345 (1008K) [application/octet-stream] Saving to: "Nginx - 1.16.0. Tar. Gz" 100% [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = >] 1032345 715 k/sin 1.4s
2019-05-23 03:28:53 (715 KB/s) - “nginx-1.16.0.tar.gz” saved [1032345/1032345]
Copy the code
Unzip files:
[root@host nginx]# tar xf nginx - 1.16.0. Tar. Gz
[root@host nginx]# lsNginx - 1.16.0 nginx - 1.16.0. Tar. Gz [root @ host nginx]# CD nginx - 1.16.0[root @ host nginx - 1.16.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
Copy the code
Compile the installation
Compile and install Nginx using the following three commands:
./configure
make
make install
Copy the code
The./configure command displays an error if the PCRE library cannot be found:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
Copy the code
Normal output should look like this, with a Makefile generated:
[root @ host nginx - 1.16.0]# ./configure
checking forOS + Linux 4.10.4-1.el6.elrebo. i686 I686 Checkingfor C compiler ... found
+ using GNU C compiler
+ gcc version: 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for O_PATH ... not found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... not found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... not found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 4 bytes
checking for long long size ... 8 bytes
checking for void * size ... 4 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 4 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 4 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... not found
checking for clock_gettime(CLOCK_MONOTONIC) in librt ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... not found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
Copy the code
View the Nginx version
After the installation is successful, you can view the Nginx version by using the -v parameter.
[root@host sbin]# /usr/local/nginx/sbin/nginx -vNginx version: nginx / 1.16.0Copy the code
Start the
Nginx supports both direct and parameter startup, as illustrated below.
The port to take up
Nginx needs to use port 80. If port 80 is used, the following error will be reported:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already failedin use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already failedin use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already failedin use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already failedin use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already failedin use)
Copy the code
You can use the lsof tool to check port usage. If you don’t have one, you can use the following command to install it:
yum install -y lsof
Copy the code
Check whether port 80 is occupied on the host and kill the occupied process:
[root@host sbin]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 1765 root 53u IPv6 15062 0t0 TCP *:http (LISTEN)
[root@host sbin]# killall -9 java
[root@host sbin]# lsof -i :80
[root@host sbin]#
Copy the code
The default startup
Use whereis to view the installation directory of nginx:
[root @ host nginx - 1.16.0]# whereis nginx
nginx: /usr/local/nginx
Copy the code
If nginx is started without any parameters, the default nginx.conf configuration file is used to start nginx:
/usr/local/nginx/sbin/nginx
Copy the code
After successful startup, when you request the server again, you will see the following page:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
Copy the code
Start with parameters
The -c parameter specifies the startup mode of the configuration file:
./nginx -c mynginx.conf
Copy the code
The -p parameter specifies the Nginx installation directory:
./nginx -p mydir/nginx
Copy the code
The -g parameter temporarily specifies some global configuration items
./nginx -g "pid varnginx/test.pid;"
Copy the code
This command means that the pid file will be written to varnginx/test.pid.
The restriction of the -g parameter is that the specified configuration item cannot conflict with the configuration item in nginx.conf in the default path; otherwise, the startup fails.
As in the example above, configuration items such as pid logs/nginx.pid do not exist in the default nginx.conf.
Another constraint is that when the Nginx service starts in -g mode and executes other command lines, the -g parameter must also be added. Otherwise, configuration items may not match.
Using the -t parameter only tests for errors in the configuration file without starting Nginx. Such as:
./nginx -t
Copy the code
The command output shows whether the configuration is correct.
[root@host sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Copy the code
When testing configuration options, using the -q parameter does not output information below the error level to the screen. Such as:
./nginx -t -q
Copy the code
Stop the service
There are two main ways to stop Nginx services.
One is quick stop, which immediately stops all network requests that the Nginx service is processing and immediately discards the connection stop service.
The other is a gentle stop, in which Nginx is allowed to finish processing the current request, but receives no new requests, and then closes the connection and stops working.
Quick stop of service
/usr/local/nginx/sbin/nginx -s stop
Copy the code
Kill service
Kill -s SIGTERM process ID or kill -s SIGINT process ID has the same effect as the preceding./nginx -s stop command.
[root@host sbin]# ps -ef|grep nginx
root 10568 1 0 04:22 ? 00:00:00 nginx: master process ./nginx
nobody 10569 10568 0 04:22 ? 00:00:00 nginx: worker process
root 10571 5440 0 04:23 pts/1 00:00:00 grep nginx
[root@host sbin]# kill -s SIGINT 10568
[root@host sbin]# ps -ef|grep nginx
root 10574 5440 0 04:24 pts/1 00:00:00 grep nginx
[root@host sbin]#
Copy the code
Gracefully discontinue service
If you want the Nginx service to process all the current requests properly before stopping the service, you can use the -s quit parameter to stop the service.
Such as:
./nginx -s quit
Copy the code
This command is different from stopping the Nginx service quickly.
When the service is stopped quickly, the worker process and master process will immediately jump out of the loop and exit the process after receiving the signal.
Gracefully stopping the service closes the listening port, stops receiving new connections, processes all connections that are currently being processed, and exits the process.
Similar to stopping the service quickly, you can directly send the QUIT signal to the master process to stop the service. The effect is the same as running the -s QUIT command.
Such as:
kill -s SIGQUIT <nginx master pid>
Copy the code
If you want to “gracefully” stop a worker process, you can stop the service by sending a WINCH signal to the process.
Such as:
kill -s SIGWINCH <nginx worker pid>
Copy the code
Send a signal
./nginx -g TERM | INT | QUIT
Copy the code
TERM and INT signals are used for quick stops, and QUIT signals are used for smooth stops.
Nginx reloads the configuration
Enable running Nginx to re-read configuration items and take effect
Using the -s reload parameter causes the running Nginx service to reload the nginx.conf file. Such as:
usrlocal/nginx/sbin/nginx -s reload
Copy the code
Rollback of log files
The -s reopen parameter is used to reopen the log file. In this case, the current log file is renamed or moved to another directory for backup. When reopened, a new log file is generated.
This feature keeps log files from becoming too large. Such as:
./nginx -s reopen
Copy the code
This has the same effect as sending a USR1 signal using the kill command.
kill -s SIGUSR1 <nginx master pid>
Copy the code
conclusion
This article provides a brief overview of Nginx’s functions and benefits, then demonstrates how to install Nginx, and how to start and shut down services.