您的位置:首页 > 运维架构 > Nginx

Nginx详细配置的备注说明

2016-09-09 09:01 429 查看
Nginx.conf代码


#http://www.open-open.com/home/space-361-do-blog-id-5087.html

#user nobody;

#定义了nginx对外提供web服务时的worker进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量

#及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。

worker_processes 2;

#更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。

#设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件,所以把这个值设高,这样nginx就不会有“too many open files”问题了。

worker_rlimit_nofile 10000;

#error_log logs/error.log;

#error_log logs/error.log notice;

error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections 2048; #允许最大连接数

multi_accept on; #告诉nginx收到一个新连接通知后接受尽可能多的连接。

#设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。

#(值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的)

#use epoll;

}

http {

#server_tokens 并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。

#server_tokens off;

include mime.types;

default_type application/octet-stream;

#定义日志格式(格式名字为main)

#$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;

#$remote_user:用来记录客户端用户名称;

#$time_local: 用来记录访问时间与时区;

#$request: 用来记录请求的url与http协议

#$status: 用来记录请求状态;成功是200,

#$body_bytes_s ent :记录发送给客户端文件主体内容大小;

#$http_referer:用来记录从那个页面链接访问过来的;

#$http_user_agent:记录客户毒啊浏览器的相关信息;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;

#sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。

#Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,

#write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,

#sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)。

sendfile on;

#告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送。

tcp_nopush on;

#告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。

tcp_nodelay on;

fastcgi_intercept_errors on;

#开启gzip压缩,对网页文件、css、js、xml等启动gzip压缩,减少数据传输量,提高访问速度。

#include gzip.conf;

#给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让ngnix持续工作的时间更长。

#设置为0,即为持续长久连接

keepalive_timeout 65;

#设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些。

client_header_timeout 10;

client_body_timeout 10;

#告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。

reset_timedout_connection on;

#指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。

#如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。

send_timeout 30;

#定义一个名为allips的limit_req_zone用来存储session,大小是10M内存,

#以$binary_remote_addr为key,限制平均每秒的请求为20个,

#1M能存储16000个状态,rate的值必须为整数,如果限制两秒钟一个请求,可以设置成30r/m

#limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;

#限制每ip每秒不超过20个请求,漏桶数burst为5。(结合limit_req_zone使用)

#brust的意思就是,如果第1秒、2,3,4秒请求为19个,

#第5秒的请求为25个是被允许的。

#但是如果你第1秒就25个请求,第2秒超过20的请求返回503错误。

#nodelay,如果不设置该选项,严格使用平均速率限制请求数,

#第1秒25个请求时,5个请求放到第2秒执行,

#设置nodelay,25个请求将在第1秒执行。

#limit_req zone=allips burst=5 nodelay;

#设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。

#该属性只能定义在http区域。

#limit_conn_zone $binary_remote_addr zone=perip:1m;

#为给定的key设置最大连接数。这里key是addr,我们设置的值是20,也就是说我们允许每一个IP地址最多同时打开有20个连接。

#该属性可定义在http server location区域。

#limit_conn perip 20;

#打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。

open_file_cache max=10000 inactive=20s;

#在open_file_cache中指定检测正确信息的间隔时间。

open_file_cache_valid 30s;

#定义了open_file_cache中指令参数不活动时间期间里最小的文件数。

open_file_cache_min_uses 2;

#指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。

#我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。

open_file_cache_errors on;

#关闭磁盘缓存,减少磁盘IO。

proxy_max_temp_file_size 0;

proxy_connect_timeout 2;

proxy_send_timeout 10;

proxy_read_timeout 10;

proxy_buffer_size 16k;

proxy_buffers 4 64k;

#如果系统很忙的时候可以申请更大的proxy_buffers(官方推荐:proxy_buffers*2)

#proxy_busy_buffers_size 128k;

#proxy_temp_file_write_size 128k;

#缓存proxy_temp_path和proxy_cache_path必须在同一个分区

#proxy_temp_path docs/cache/temp;

#该指令用于设置缓存文件的存放路径,设置缓存区名称为cache_one,

#内存缓存空间大小为200M,自动清除超过1天没有被访问的缓存数据,硬盘缓存空间大小为30GB

#proxy_cache_path docs/cache/path levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=4g;

upstream localhost{

server localhost:8080 weight=10;

server localhost:8081 weight=10;

#server localhost:10000 backup;

#每个设备的状态设置为:

#1.down表示单前的server暂时不参与负载

#2.weight默认为1.weight越大,负载的权重就越大。

#3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误

#4.fail_timeout:max_fails次失败后,暂停的时间。

#5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

}

server {

listen 80;

server_name localhost;

charset utf-8;

access_log logs/localhost.access.log main;

error_page 404 /404.html;

location = /404.html {

root html;

}

# redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

#禁止访问WEB-INF目录文件

location ~ ^/(WEB-INF)/{

#不记录404错误日志

#log_not_found off;

deny all;

}

#静态文件让nginx处理

#location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js|htm|html)$ {

# root html;

# access_log off;

# expires 30d;

#}

location / {

#限制下载速度256KB/秒

#limit_rate 256k;

#DNS解析服务器的IP地址,可以在IE 工具-Internet选项-连接-局域网设置-代理服务器 中设置代理服务器IP地址和端口

resolver 8.8.8.8;

# 默认主页目录在nginx安装目录的html子目录。

root html;

index index.jsp index.htm;

proxy_pass http://localhost;
#3. 没有索引页时,罗列文件和子目录

autoindex on;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#使用http区域定义的cache名称。

#proxy_cache cache_one;

#访问出现错误,500,502等错误,转向另外tomcat

#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

#200 304状态缓存3小时

proxy_cache_valid 200 304 3h;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

# deny all;

#}

#location /nginxStatus{

# sub_status on;

# access_log logs/nginxLog.log;

# auth_basic "nginxStatus";

#auth_basic_user_file conf/htpasswd;

#allow all; #例:allow 10.0.0.1/24

#deny all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

#}

# HTTPS server

#

#server {

# listen 443 ssl;

# server_name localhost;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;

# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;

# location / {

# root html;

# index index.html index.htm;

# }

#}

}

Gzip.conf代码


#gzip模块设置

gzip on; #告诉nginx采用gzip压缩的形式发送数据

#指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。

gzip_disable 'msie6';

#告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),

#从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了

#gzip_static on;

#允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。

gzip_proxied any;

#最小压缩文件大小。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。

gzip_min_length 1k;

#设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。

gzip_comp_level 4;

#设置需要压缩的数据格式

gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

原文:http://aokunsang.iteye.com/blog/2058787
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx