Nginx服务器配置文件完全解析

  user www www; // 使用的用户和组     worker_processes 8; // 指定的工作衍生进程数(一般等于cpu总核数或总核数的2倍)     error_log logs/nginx_error.log crit; // 指定错误日志存放的路径,错误日志记录级别分别选项为:debug,info,notice,warn,error,crit      #error_log logs/error.log;     #error_log logs/error.log notice;     #error_log logs/error.log info;        pid     nginx.pid; // 指定pid文件存放的路径     #pid    logs/nginx.pid;        worker_rlimit_nofile 65535; // 一个nginx进程打开的最多文件描述符数目,理论值是最多打开的文件数(系统ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以在这里建议和ulimit 值保持一致        events {         use  epoll; // 使用网络I/O模型,Linux系统推荐采用epoll模型,FreeBSD系统推荐采用kqueue模型        worker_connections 51200; // 允许的连接数  }     # 设定http服务器,利用它的反向代理功能提供负载均衡支持  http {         include    mime.types; // 设定mime类型,类型由mime.type文件定义    default_type application/octet-stream;              charset utf-8; // 设置使用的字符集,如果一个网站有多种字符集,请不要随便设置,应该让程序员在HTML代码中通过Meta标签设置         ssi on; // 页面静态化的一个大问题是登陆用户访问如果静态化,大部分页面内容需要缓存但是用户登陆的个人信息是动态的,ssi用来解决页面部分缓存问题         ssi_silent_errors on; // 默认是off,开启后在处理SSI文件出错时不输出错误提示:"[an error occurred while processing the directive]"        ssi_types text/shtml; // 默认是ssi_types text/html,所以如果需要htm和html支持,则不需要设置这句,如果需要shtml支持,则需要设置       #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指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载,如果图片显示不正常把这个改成off    sendfile    on;       #tcp_nopush   on; // 防止网络阻塞       #tcp_nodelay on;         # autoindex off; // 开启目录列表访问,合适下载服务器,默认关闭         server_names_hash_bucket_size 128; // 服务器名字的hash表大小         client_header_buffer_size 32k; // 上传文件大小限制         large_client_header_buffers 4 32k; // 设定请求缓存         client_max_body_size 300m; // 设定请求缓存         # (与php-fpm有关) 优化的上传支持,可以加速对大POST 请求的处理速度,包括文件上传。优化是通过将请求体已写入一个临时文件,然后fastcgi 协议传递文件名而不是请求体到来实现的    client_body_in_file_only clean;       client_body_temp_path /dev/shm 1 2;       # 这个参数设置比较大时,使用firefox或ie提交一个小于512K的图片访问都会正常,注释改指令模式大小是操作系统页面大小的两倍,8K或16K ,一般提交的图片大于512K,提交的内容会写入到临时的文件,不会出现任何问题。当取消了目录访问权限(autoindex off;),如果提交的图片大于512K 都会返回500 Internal Server Error错误    client_body_buffer_size 512k;            proxy_connect_timeout  5; // 后端服务器连接的超时时间_发起握手等候响应超时时间(代理连接超时)            proxy_read_timeout    60; // 连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(后端服务器处理请求的时间)         proxy_send_timeout    5; // 后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据         proxy_buffer_size    16k; // 该指令设置缓冲区大小,从代理后端服务器取得的第一部分的响应内容,会放到这里,小的响应header通常位于这部分响应内容里边.(保存用户头信息的缓冲区大小)         proxy_buffers      4 64k; // 该指令设置缓冲区的大小和数量,从被代理的后端服务器取得的响应内容,会放置到这里. 默认情况下,一个缓冲区的大小等于内存页面大小,可能是4K也可能是8K,这取决于平台         proxy_busy_buffers_size 128k; // 有处在busy状态的buffer size加起来不能超过proxy_busy_buffers_size,控制同时传输到客户端的buffer数量的         proxy_temp_file_write_size 128k; // # 临时文件写入大小         # nginx和cgi之间的超时时间    fastcgi_connect_timeout 90;     fastcgi_send_timeout 90;    fastcgi_read_timeout 90;    fastcgi_buffer_size 64k;    fastcgi_buffers 4 64k;    fastcgi_busy_buffers_size 128k;    fastcgi_temp_file_write_size 128k;       # 开启gzip压缩    gzip on;    gzip_min_length 1k;    gzip_buffers   4 16k;    # 对http/1.1协议的请求才会进行压缩,如果使用了反向代理,那么nginx和后端的upstream server服务器是使用的1.0协议通信    gzip_http_version 1.1;    gzip_comp_level 9;    gzip_types    text/plain application/x-javascript text/css application/xml;    gzip_vary on;         # nginx缓存目录(在location段落下需要配合proxy_store on 开启缓存机制 include proxy.conf 处理的详细规则     if (!-e $request_filename)){proxy_pass http://192.168.10.10;}        proxy_temp_path  /data/wwwroot/proxy_temp_dir;       #proxy_cache_path /data/wwwroot/cache.hxage.com levels=1:2  keys_zone=cache.hxage.com:3000m inactive=1y max_size=80G;    # Nginx 内部重定向规则会被启动,当URL 指向一个目录并且在最后没有包含“/”时,Nginx 内部会自动的做一个301 重定向,这时会有两种情况    # 1、server_name_in_redirect on(默认),URL 重定向为:server_name 中的第一个域名+ 目录名+ /;    # 2、server_name_in_redirect off,URL 重定向为:原URL 中的域名+ 目录名+ /    server_name_in_redirect off;       server_tokens off;       # sub filter       # include sub_filter.conf;       # null hostname       server {       listen 80 default;       return 444;       access_log off;   }  server  {      listen 5566;      server_name localhost;      index index.html index.htm index.shtml index.php;      location ~ ^/status/      {          stub_status on;          access_log off;      }  }  #---------------- Vhost --------------------#  include vhost/*.conf;  }    

# 以下时阿里云主机上的一段nginx.conf配置文件

  user www www;  worker_processes auto;     error_log /alidata/log/nginx/error.log crit;  pid    /alidata/server/nginx/logs/nginx.pid;     #Specifies the value for maximum file descriptors that can be opened by this process.  worker_rlimit_nofile 65535;     events  {   use epoll;   worker_connections 65535;  }        http {      include    mime.types;      default_type application/octet-stream;         #charset gb2312;         server_names_hash_bucket_size 128;      client_header_buffer_size 32k;      large_client_header_buffers 4 32k;      client_max_body_size 8m;         sendfile on;      tcp_nopush   on;         keepalive_timeout 15;         tcp_nodelay on;         fastcgi_connect_timeout 300;      fastcgi_send_timeout 300;      fastcgi_read_timeout 300;      fastcgi_buffer_size 64k;      fastcgi_buffers 4 64k;      fastcgi_busy_buffers_size 128k;      fastcgi_temp_file_write_size 128k;         gzip on;      gzip_min_length 1k;      gzip_buffers   4 16k;      gzip_http_version 1.1;      gzip_comp_level 2;      gzip_types    text/plain application/x-javascript text/css application/xml;      gzip_vary on;      gzip_disable msie6;      #limit_zone crawler $binary_remote_addr 10m;      log_format '$remote_addr - $remote_user [$time_local] "$request" '             '$status $body_bytes_sent "$http_referer" '             '"$http_user_agent" "$http_x_forwarded_for"';      include /alidata/server/nginx/conf/vhosts/*.conf;  }    

参与评论