1. Necessary software dependencies

1. Install pcRE

To support the rewrite functionality, we need to install pcRE if you already have it installed, please skip this step

yum install pcre*

2. Installing OpenSSL requires SSL support. If SSL is not required, skip this step

yum install openssl*

Two, installation:

Execute command:

./configure --prefix=/usr/local/nginx-1.5.1 \ --with-http_ssl_module --with-http_spdy_module \ --with-http_stub_status_module --with-pcre

–with-http_stub_status_module: supports nginx status query

–with-http_ssl_module: HTTPS is supported

–with-http_spdy_module: support Google SPDY, this must have SSL support

— with-pcRE: To support the rewrite rewrite functionality, a PCRE must be formulated

Configure is OK

Three, configuration:

Nginx configuration file

nginx.conf server { listen 80; -- Specify port server_name localhost; ssl on; - enable the domain name: specify the certificate and KEY ssl_certificate/opt/ngx_openresty/nginx/nginx/conf/server. The CRT. ssl_certificate_key /opt/ngx_openresty/nginx/nginx/conf/server.key;Copy the code

Start, close, and reset Nginx

Nginx starts by executing the following command without changing any configuration files:

/ usr/local/nginx 1.5.1 / sbin/nginx

2. Close:

/ usr/local/nginx - 1.5.1 / sbin/nginx - s stop

/usr/local/nginx-1.5.1/sbin/ nginx-s -s /usr/local/nginx-1.5.1/sbin/ nginx-s -s

4. Check the Nginx process

$ps-ef|grep nginx|grep -v grep nginx 5097 1 0 2017 ? 00:00:00 nginx: master process /opt/ngx_openresty/nginx/nginx/sbin/nginx nginx 5098 5097 6 2017 ? 5-18:33:21 nginx: worker process nginx 5099 5097 6 2017 ? 5-19:13:03 nginx: worker process nginx 5100 5097 6 2017 ? 5-18:44:23 nginx: worker process nginx 5101 5097 6 2017 ? 5-18:59:18 Nginx: worker process note: Nginx has Master and worker processesCopy the code

Master is responsible for scheduling and managing worker for the management process. Worker belongs to the service process

5, nginx parameter:

--prefix= point to installation directory --sbin-path point to (execute) program file (nginx) --conf-path= point to configuration file (nginx.conf) --error-log-path= point to error log directory --pid-path= Point to pid file (nginx.pid) --user= specify the non-privileged user when the program is running --group= Specify the non-privileged user group when the program is running --builddir= point to the compile directoryCopy the code

Five, configuration tuning skills

Optimize the number of Nginx processes

$ grep worker_processes nginx.conf nginx.conf:worker_processes 4; The number of worker processes is the same as the number of CPU cores. The number of concurrent processes is the same as the number of CPU cores. The number of CPU cores is the same as the number of CPU cores.#grep processor /proc/cpuinfo|wc -l4: Indicates that one CPU has four coresCopy the code

2. Nginx event processing model optimization

Events Sets the working mode of Nginx and the maximum number of connections. Select poll kqueue epoll rtsig /dev/poll select poll Standard working mode kqueue epoll Efficient working mode: Epoll user Linux platform Kqueue is used for THE BSD systemCopy the code

3. Modify the startup user

1. After Nginx is started by default, the default user is nobody grep user nginx.conf user nobody. Create a new user for the nginx service useradd nginx -s /sbin/nologin -m /sbin/nologin -m There are two ways to do this: 1. Modify the configuration file nginx.conf to default#user nobody; Change the value to user nginx nginxMethod 2: Specify the user and group to compile the Nginx software. /configure --user=nginx --group=nginx --prefix=/application/nginx1.6.3 --with-http_stub_status_module - with - http_ssl - the result of the check module modified ps - ef | grep nginx | grep -v grep nginx 1 0 2017 5097? 00:00:00 nginx: master process /opt/ngx_openresty/nginx/nginx/sbin/nginx nginx 5098 5097 6 2017 ? 5-18:33:21 nginx: worker process nginx 5099 5097 6 2017 ? 5-19:13:03 nginx: worker process nginx 5100 5097 6 2017 ? 5-18:44:23 nginx: worker process nginx 5101 5097 6 2017 ? 5-18:59:18 nginx: worker processCopy the code

4. Hide tabs

Add "server_tokens off;" to HTTP tags. Parameter to hide Nginx tabs; To modify the TAB, use the nginx -s reload command to load the configuration file.Copy the code

5, Nginx total concurrent connections

Default: 1024 events {worker_connections 4096} Total number of concurrent Nginx connections = number of workers * number of worker_connections The number of proxy server connections, client connections, and concurrent connections also depends on the maximum number of open files worker_rlimit_nofileCopy the code