nginx.conf详解

| nginx.conf配置:
nginx.conf是里面最重要的一个配置,因为我们经常会用到它,并且要熟练掌握它的配置。nginx.conf里面分为这么几个配置块,分别是events、http、server、upstream、location,当然还有最初的关于user的配置关于worker_processes的配置等。
#定义Nginx运行的用户和用户组
user nginx nginx;//默认为user nobody;
#nginx进程数,一般建议设置为CPU总核数
worker_processes 8;
#全局错误日志定义类型 [ debug |info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;#默认是这样的的,但其实可以info的写成info_log,
#进程文件
pid /var/run/nginx.pid; 进程文件显示nginx运行的PID
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

#事件模块
event{
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本
内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)`
worker_connection 65535;
}
#http服务器
http{
include mime.types; #文件扩展名与文件类型映射表
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;#默认文件类型
server_names_hash_bucket_size 512;#服务器名字的hash表大小
client_header_buffer_size 32k;#上传文件大小限制
large_client_header_buffers 4 32k;#设定请求缓存
client_max_body_size 50m;#设定请求缓存
sendfile on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on;#防止网络阻塞
keepalive_timeout 60;#长连接超时时间,单位是秒
tcp_nodelay on;#防止网络阻塞

    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    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 256k;
    fastcgi_intercept_errors on;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;#增加响应头”Vary: Accept-Encoding”
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";

    limit_conn_zone $binary_remote_addr zone=perip:10m;
    limit_conn_zone $server_name zone=perserver:10m;
    server_tokens off;
    access_log off;
    upstream blog.ha97.com {
        #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
        server 192.168.80.121:80 weight=3;
        server 192.168.80.122:80 weight=2;
        server 192.168.80.123:80 weight=3;
    }

server
{
listen 888;#监听888端口
server_name phpmyadmin;#服务器名字为phpmyadmin
index index.html index.htm index.php;#解析index.html index.html index.php
root /www/server/phpmyadmin;#服务根目录
#error_page 404 /404.html;#错误页面
include enable-php.conf;#加载允许php解析的文件
location ~ ..(gif|jpg|jpeg|png|bmp|swf)$ #location负责转发,转发规则为
expires 30d;
}
location ~ .
.(js|css)?$
{
expires 12h;
}
location ~ /.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
include /www/server/panel/vhost/nginx/*.conf;
}