Detailed analysis of nginx configuration file
Nginx installation directory: /usr/local/nginx
Configuration file: nginx.conf file in the /usr/local/nginx/conf directory
nginx optimization method
1, work_process: cpu affinity
2, select epoll model
3, work_connects connection number
4, maximum number of open files per process
5, keepalive timeout; session retention time
6, gzip compression level
7, proxy timeout configuration
8 , expires 3d; browser cache expiration time, 30 days
9, nginx dynamic and static separation
An nginx can be configured with dozens of domain names and dozens of virtual hosts
user www www #Define nginx users and user groups
worker_processes 4; #Start 4 worker processes by default (generally 8-16 for enterprises)
Can be changed to worker_processes.auto; #Automatically start the process according to the request
worker_cpu_affinity 00000001 00000010 000001 00 00001000 #CPU affinity, there are several logical CPUs to configure
pid /usr/local/nginx/nginx.pid #Specify the location of the process id
worker_rlimit_nofile 102400; #Specify the maximum number of file descriptors opened by the process. This is best configured to be equal to the value of ulimit -n
#event module, define which module to use; nginx2.6 and above, you can use the epoll module
events{
use epoll;
worker_connections 102400; #Maximum number of concurrent connections for a single background process
multi_accept on; #Receive as many requests as possible (part of optimization)
}
epoll model: asynchronous non-blocking model, the selection of handle events does not require traversal, and is event-responsive. High efficiency--nginx
select model: traverse all handles, low efficiency--apache
sendfile on; #Specify whether nginx calls the function, ordinary applications must be on
tcp_nopush on; #Prevent network blocking
keepalive_timeout 60; #Keep the timeout in seconds. The amount of time the connection will continue to be maintained without disconnecting the connection. When browsing again, you can request it directly without re-establishing the connection.
tcp_nodelay on; #Improve the real-time responsiveness of data.
#Enable gzip compression, the performance is improved several times
gzip on;
gzip_min_length 1k; #Small and 1k will not compress
gzip_buffers 416k;
gzip_http_version1.1;
gzip_comp_level 4; #This refers to the compression level. The maximum is 9. When the configuration is 4, when the original URL is 180k, it may be 40-50k after compression. This improves performance, and then it is generally configured as 4 or 6.
Buffer
client_max_body_size 10M; #The maximum number of bytes of a single file that the client requests are allowed to be
10M client_body_buffer_size 128k; #The buffer proxy buffers the maximum number of bytes requested by the client
proxy
proxy_connect_timeout 90; #Example of the connection time between nginx and the backend server : one nginx corresponds to multiple tomcats on the backend, and reading more than 90 seconds is the timeout
proxy_send_timeout 90; #Backend server data return time, within the specified time, the backend The server must return all data.
proxy_read_timeout 90; #After the connection is successful, the response time of the backend server (the time to process the request)
这个值一般设置为120秒。超时了nginx会报502错误。
****nginx动静分离
将动态请求与静态请求分开
一种是将 纯粹的把静态文件做成独立的域名,放在独立的服务器上,目前的主流方案。
另一种是 将动态文件和静态文件混合发布,根据不同的后缀名请求不同的文件。
#定义模块:(这里配置服务器,**负载均衡**)
upstream jvm_web1{
server 127.0.0.1:8080 weight=1max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1max_fails=2 fail_timeout=30s;
}
location / {
proxy_pass http://jvm_web1; #将请求转发给后台请求,使用jvm_web1模块
}
location~ 。。。#这里做匹配静态文件的数据,然后下面配置对应的server接收静态文件请求