nginx配置

来源:互联网 发布:linux curse 是什么 编辑:程序博客网 时间:2024/06/06 20:43
#使用的用户和组
user  www www;
#nginx进程数,按照cpu数目来指定,一般为它的倍数。
worker_processes  16;
#为每个进程分配cpu
worker_cpu_affinity  0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
#当一个nginx进程打开的最多文件描述符数目
worker_rlimit_nofile 65536;
error_log  /usr/local/nginx/logs/error.log  crit;
pid        logs/nginx.pid;

events {
        #使用epoll的I/O模型,优点是高效。  
        use epoll;
        #每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。
        worker_connections  65536;
}

http {
    #为打开文件指定缓存,inactive是指经过多长时间文件没被请求后删除缓存。
    open_file_cache max=65536  inactive=20s;
    #open_file_cache指令中的inactive参数时间内文件的最少使用次数
    open_file_cache_min_uses 1;
    #多长时间检查一次缓存的有效信息。
    open_file_cache_valid 30s;
    client_max_body_size 10m;

    include       mime.types;
    default_type  application/octet-stream;
    #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;
    #下面6行自行添加
    server_names_hash_bucket_size 128;
    #body和header据内容大小适当调大,防止get和post时出现错误错误
    client_header_buffer_size 128k;
    client_body_buffer_size 128k;
    large_client_header_buffers 4 32k;
    #以下配合使用,提升下载性能,非下载等应用均打开
    sendfile    on;
    tcp_nopush  on;
    tcp_nodelay on;
#   keepalive_timeout  60;
#   send_timeout        120;
    #隐藏nginx版本
    server_tokens       off;
    
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
 #以下适当调大,并且大小一致,防止php-cgi异常终止出现502错误
        fastcgi_buffer_size 256k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
  
        gzip  on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
server {
        listen 80;
        server_name www.test.com;
        index index.html index.htm index.php;
        root  /data/www/;
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                {
                expires 30d;
                }
                location ~ .*\.(js|css)?$
                {
                expires 1h;
                }
    location ~ .*\.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
   #支持pathinfo
   location / {
        index index.php;
       if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php/$1 last;
        break;
    }
}
location ~ .+\.php($|/) {
    set $script $uri;
    set $path_info "/";
    if ($uri ~ "^(.+\.php)(/.+)") {
        set $script $1;
        set $path_info $2;
    }
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php?IF_REWRITE=1;
    include /usr/local/nginx/conf/fastcgi_params;
    fastcgi_param PATH_INFO $path_info;
    fastcgi_param SCRIPT_FILENAME $document_root/$script;
    fastcgi_param SCRIPT_NAME $script;
}
   # location /webstatus {
    #    stub_status on;
    #    access_log off;
    #    allow x.x.x.x;
    #    deny all;
   # }
    #前端如果有LB、Squid就不能用$remote_addr 取客户端的地址
    log_format  access_www  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
        access_log  /usr/local/nginx/logs/www_access.log  access_www;
      }  

日志格式中:
$remote_addr 记录IP地址;
$remote_user 记录远程客户端用户名称;
$time_local  记录访问时间与时区;
$request     记录请求URL与HTTP协议;
$status      记录请求状态;
$body_bytes_sent 记录发送给客户端的文件主体内容大小;
$http_referer    记录是从哪个页面链接访问过来;
$http_user_agent 记录客户端浏览器的相关信息;

为防止日志太大影响服务器效率,为方便对日志进行分析,需用脚本定时切割日志.
0 0
原创粉丝点击