nginx.conf配置及优化相关

nginx.conf配置文件内容

user www www;
worker_processes 8;
worker_rlimit_nofile 100000;

error_log /data/nginx/logs/error.log notice;

pid /var/run/nginx.pid;

events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
#multi_accept on;
worker_connections 52000; #单个后台worker process进程的最大并发链接数
}


http {
include mime.types; #设定mime类型,类型由mime.type文件定义
default_type application/octet-stream;
#charset UTF-8; #设置我们的头文件中的默认的字符集

#设置日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent $request_body "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log off;

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
server_tokens off; #并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
sendfile on;#sendfile可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。
tcp_nopush on; #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
tcp_nodelay on;#告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
#设置超时时间
keepalive_timeout 10; #给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接
client_header_timeout 10; #client_header_timeout 和client_body_timeout 设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些。
client_body_timeout 10; 
reset_timedout_connection on; #告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
send_timeout 10; #指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接

#开启gzip压缩
gzip on;
gzip_disable "msie6";
# gzip_static on;
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

#gzip是告诉nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。
#gzip_disable为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
#gzip_static告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了(想要更详尽的gzip_static的信息,请点击这里)。
#gzip_proxied允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。
#gzip_min_length设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。
#gzip_comp_level设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。

open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
#open_file_cache打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。
#open_file_cache_valid 在open_file_cache中指定检测正确信息的间隔时间。
#open_file_cache_min_uses 定义了open_file_cache中指令参数不活动时间期间里最小的文件数。
#open_file_cache_errors指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。

#负载均衡相关配置

#设定负载均衡的服务器列表
#weigth参数表示权值,权值越高被分配到的几率越大
upstream tdx {
#ip_hash;
server node1:80 max_fails=3 fail_timeout=30s weight=6;
server node2:80 max_fails=3 fail_timeout=30s weight=6; 
}

#虚拟主机配置,开启负载均衡

server {
listen 80;
#server_name wwww.xxx.com;
access_log /data/nginx/logs/tdx_access.log combined;

#默认请求
location / {
root /data/nginx/htdocs/tdx; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称
proxy_pass http://tdx; #请求转向tdx定义的服务器列表
proxy_redirect off; #禁用缓存
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90;
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
#定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/web_app/nginx/html;
}

location ~ .php$ {
root /data/nginx/htdocs/tdx;
proxy_pass http://tdx;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
# proxy_redirect off;
# fastcgi_pass unix:/tmp/php-fcgi.sock;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /data/nginx/htdocs/tdx/$fastcgi_script_name;
# include fastcgi_params;
}
#禁止访问.htxxx文件
location ~ /.ht {
deny all;
}
}

}
nginx.conf

#http段完整配置如下:

 1 user www-data;
 2 pid /var/run/nginx.pid;
 3 worker_processes auto;
 4 worker_rlimit_nofile 100000;
 5 events {
 6  worker_connections 2048;
 7  multi_accept on;
 8  use epoll;
 9 }
10 http {
11   server_tokens off;
12  sendfile on;
13 tcp_nopush on;
14  tcp_nodelay on;
15  access_log off;
16  error_log /var/log/nginx/error.log crit;
17  keepalive_timeout 10;
18  client_header_timeout 10;
19  client_body_timeout 10;
20 reset_timedout_connection on;
21  send_timeout 10;
22  limit_conn_zone $binary_remote_addr zone=addr:5m;
23  limit_conn addr 100;
24  include /etc/nginx/mime.types;
25  default_type text/html;
26 charset UTF-8;
27 gzip on;
28  gzip_disable "msie6";
29 gzip_proxied any;
30 gzip_min_length 1000;
31 gzip_comp_level 6;
32  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
33  open_file_cache max=100000 inactive=20s;
34 open_file_cache_valid 30s;
35 open_file_cache_min_uses 2;
36 open_file_cache_errors on;
37  include /etc/nginx/conf.d/*.conf;
38  include /etc/nginx/sites-enabled/*;
39 }
http段内容

http://www.ttlsa.com/nginx/using-nginx-as-http-loadbalancer/  负载均衡三种模式区别
http://www.ttlsa.com/nginx/nginx-load-balancing-from-theory-to-practice/  负载均衡实践
http://www.ttlsa.com/nginx/nginx-configure-descriptions/  编译安装nginx参数详细
http://www.ttlsa.com/nginx/nginx-battle-ready-optimization-guide/  优化指南

后记

以上是我个人部署及整理的部分nginx资料信息,感谢运维生存时间默北分享的资料。由于最近刚开始研究,内容可能有遗漏之处,欢迎指正。

原文地址:https://www.cnblogs.com/aslongas/p/5162824.html