Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1669704
  • 博文数量: 636
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3950
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-06 21:58
个人简介

博客是我工作的好帮手,遇到困难就来博客找资料

文章分类

全部博文(636)

文章存档

2024年(5)

2022年(2)

2021年(4)

2020年(40)

2019年(4)

2018年(78)

2017年(213)

2016年(41)

2015年(183)

2014年(66)

我的朋友

分类: 系统运维

2018-05-16 20:24:17


Nginx访问日志(access_log)配置及信息详解

通过访问日志,可以知晓用户的地址,网站的哪些部分最受欢迎,用户的浏览时间,对大多数用户用的的浏览器做出针对性优化。

Nginx访问日志(access_log)介绍

    Nginx会把每个用户访问往咱的日志信息记录到指定的日志文件里,供网站管理员分析用户浏览行为等,此功能由 ngx_http_log_module 模块负责。

访问日志参数

    Nginx访问日志主要有两个参数控制

log_format  #用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)

access_log  #用来指定日至文件的路径及使用的何种日志格式记录日志

lof_format的默认值:

#    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的默认值:


#access_log  logs/access.log  main;

log_format语法格式及参数语法说明如下:

    log_format        ;

    关键字         格式标签   日志格式

 

    关键字:其中关键字error_log不能改变

    格式标签:格式标签是给一套日志格式设置一个独特的名字

    日志格式:给日志设置格式

 

log_format格式变量:

    $remote_addr  #记录访问网站的客户端地址

    $remote_user  #远程客户端用户名

    $time_local  #记录访问时间与时区

    $request  #用户的http请求起始行信息

    $status  #http状态码,记录请求返回的状态码,例如:200301404

    $body_bytes_sent  #服务器发送给客户端的响应body字节数

    $http_referer  #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。

    $http_user_agent  #记录客户端访问信息,例如:浏览器、手机客户端等

    $http_x_forwarded_for  #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置

access_log语法格式及参数语法说明如下:


    access_log        ;

    关键字         日志文件   格式标签

 

    关键字:其中关键字error_log不能改变

    日志文件:可以指定任意存放日志的目录

    格式标签:给日志文件套用指定的日志格式

 

其他语法:

    access_log    off;  #关闭access_log,即不记录访问日志

    access_log path [format [buffer=size [flush=time]] [if=condition]];

    access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition];

    access_log syslog:server=address[,parameter=value] [format [if=condition]];

    说明:

    buffer=size  #为存放访问日志的缓冲区大小

    flush=time  #为缓冲区的日志刷到磁盘的时间

    gzip[=level]  #表示压缩级别

    [if = condition]  #表示其他条件

一般场景这些参数都无需配置,极端优化才有可能会考虑这些参数。

lof_format参数的标签段位置:

http

access_log参数的标签段位置:

http, server, location, if in location, limit_except

参考资料:

Nginx配置访问日志过程介绍

(1)创建log_format语句

vi conf/nginx.conf

#vi编辑nginx主配置文件,添加标签为mainlog_format格式(http标签内,在所有的server标签内可以调用)

文件内容:

worker_processes  1;

error_log logs/error.log error;

events {

    worker_connections  1024;

}

http {

    include status.conf;

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    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;

    server {

        listen       80;

        server_name  localhost;

                rewrite ^/.* permanent;

    }

    include vhost/*.conf;

}

(2)插入access_log语句

vi conf/vhost/

#vi编辑虚拟主机配置文件

文件内容:

server {

        access_log /data/log/www;

        listen 80;

        server_name abc.com

        location / {

                root /data/www/www;

                index index.html index.htm;

        }

        error_log    logs/error_ error;

        access_log    logs/access_ main;

        #新增内容↑

}

(3)重启服务

确认无误便可重启,操作如下:

nginx -t

#结果显示oksuccess没问题便可重启

nginx -s reload

(4)查看访问日志文件

ll logs/access_

-rw-r--r-- 1 root root 2305 Jun 13 18:25 logs/access_




#nginx进程,一般设置为和cpu核数一样
worker_processes 4;                        
#错误日志存放目录 
error_log  /data1/logs/error.log  crit;  
#运行用户,默认即是nginx,可不设置
user nginx       
#进程pid存放位置
pid        /application/nginx/nginx.pid;        

#Specifies the value for maximum file descriptors that can be opened by this process. 
#最大文件打开数(连接),可设置为系统优化后的ulimit -HSn的结果
worker_rlimit_nofile 51200;


cpu亲和力配置,让不同的进程使用不同的cpu

worker_cpu_affinity 0001 0010 0100 1000 0001 00100100 1000;

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

include mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型

#limit模块,可防范一定量的DDOS攻击

#用来存储session会话的状态,如下是为session分配一个名为one的10M的内存存储区,限制了每秒只接受一个ip的一次请求 1r/s

  limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  limit_conn_zone $binary_remote_addr zone=addr:10m;
  include       mime.types;
  default_type  application/octet-stream;

#第三方模块lua防火墙
    lua_need_request_body on;
    #lua_shared_dict limit 50m;
    lua_package_path "/application/nginx/conf/waf/?.lua";
    init_by_lua_file "/application/nginx/conf/waf/init.lua";
    access_by_lua_file "/application/nginx/conf/waf/access.lua";

#设定请求缓存 
  server_names_hash_bucket_size 128;
  client_header_buffer_size 512k;
  large_client_header_buffers 4 512k;
  client_max_body_size 100m;


  #隐藏响应header和错误通知中的版本号
  server_tokens off;

  #开启高效传输模式   
  sendfile on;

-------------------------------------------------------------------------------------------------
  #激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布
  积极的作用是减少网络报文段的数量
  tcp_nopush     on;
  #激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能
  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 128k;

----------------------------------------------

#连接超时时间,单位是秒
  keepalive_timeout 60;

  #开启gzip压缩功能
    gzip on;
 #设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示不管页面多大都进行压缩。建议设置成大于1K。如果小于1K可能会越压越大。
  gzip_min_length  1k;

#压缩缓冲区大小。表示申请4个单位为16K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果
  gzip_buffers     4 16k;

#压缩版本(默认1.1,前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP解压,使用默认即可。
  gzip_http_version 1.0;

#压缩比率。用来指定GZIP压缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理最慢,也比较消耗cpu资源。
  gzip_comp_level 9;

#用来指定压缩的类型,“text/html”类型总是会被压缩
  gzip_types       text/plain application/x-javascript text/css application/xml;
  #vary header支持。该选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用

Squid缓存经过Nginx压缩的数据。

gzip_vary off;
#开启ssi支持,默认是off
  ssi on;
  ssi_silent_errors on;
#设置日志模式
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" $http_x_forwarded_for';

#反向代理负载均衡设定部分

#upstream表示负载服务器池,定义名字为backend_server的服务器池
upstream backend_server {
    server   10.254.244.20:81 weight=1 max_fails=2 fail_timeout=30s;
    server   10.254.242.40:81 weight=1 max_fails=2 fail_timeout=30s;
    server   10.254.245.19:81 weight=1 max_fails=2 fail_timeout=30s;
    server   10.254.243.39:81 weight=1 max_fails=2 fail_timeout=30s;

  #设置由 fail_timeout 定义的时间段内连接该主机的失败次数,以此来断定 fail_timeout 定义的时间段内该主机是否可用。默认情况下这个数值设置为 1。零值的话禁用这个数量的尝试。

设置在指定时间内连接到主机的失败次数,超过该次数该主机被认为不可用。

#这里是在30s内尝试2次失败即认为主机不可用!
  }
###################

#基于域名的虚拟主机
  server
  {

#监听端口
    listen       80;
    server_name  abc.com;    
    index index.html index.htm index.php;      #首页排序
    root  /data0/abc;                                     #站点根目录,即网站程序存放目录 
    error_page 500 502 404 /templates/kumi/phpcms/404.html;   #错误页面

#伪静态   将/list....html的文件转发到index.php

#rewrite ^/list-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /index.php?m=content&c=index&a=lists&catid=$1&types=$2&country=$3&language=$4&age=$5&startDate=$6&typeLetter=$7&type=$8&page=$9 last;

#location 标签,根目录下的.svn目录禁止访问
    location ~ /.svn/ {
     deny all;
    }
            location ~ \.php$   
             {  #符合php扩展名的请求调度到fcgi server  
              fastcgi_pass  127.0.0.1:9000;  #抛给本机的9000端口
              fastcgi_index index.php;    #设定动态首页
              include fcgi.conf;
             }
            allow   219.237.222.30 ;  #允许访问的ip
            allow   219.237.222.31 ;
            allow   219.237.222.32 ;
            allow   219.237.222.33 ;
            allow   219.237.222.34 ;
            allow   219.237.222.35 ;
            allow   219.237.222.61 ;
            allow   219.237.222.28 ;
            deny    all;            #禁止其他ip访问
            }
    location ~ ^/admin.php
         {
            location ~ \.php$
             {
              fastcgi_pass  127.0.0.1:9000;
              fastcgi_index index.php;
              include fcgi.conf;
             }
            allow   219.237.222.30 ;
            allow   219.237.222.31 ;
            allow   219.237.222.32 ;
            allow   219.237.222.33 ;
            allow   219.237.222.34 ;
            allow   219.237.222.35 ;
            allow   219.237.222.61;
            allow   219.237.222.28;
         deny    all;
            }

#将符合js,css文件的等设定expries缓存参数,要求浏览器缓存。

location~ .*\.(js|css)?$ {

       expires      30d; #客户端缓存上述js,css数据30天

    }

##add by 20140321#######nginx防sql注入##########

###start####
if ( $query_string ~* ".*[\;'\<\>].*" ){
    return 444;
    }
if ($query_string  ~* ".*(insert|select|delete|update|count|\*|%|master|truncate|declare|\'|\;|and|or|\(|\)|exec).* ") 
    {  
    return 444; 
    }
if ($request_uri ~* "(cost\()|(concat\()") {
                 return 444;
    }
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
                 return 444;
    }
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
                 return 444;
    }
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
                 return 444;
    }
set $block_file_injections 0;
if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") {
set $block_file_injections 1;
}
if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") {
set $block_file_injections 1;
}
if ($block_file_injections = 1) {
return 448;
}
set $block_common_exploits 0;
if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
set $block_common_exploits 1;
}
if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
set $block_common_exploits 1;
}
if ($query_string ~ "proc/self/environ") {
set $block_common_exploits 1;
}
if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") {
set $block_common_exploits 1;
}
if ($query_string ~ "base64_(en|de)code\(.*\)") {
set $block_common_exploits 1;
}
if ($block_common_exploits = 1) {
return 444;
}
set $block_spam 0;
if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") {
set $block_spam 1;
}
if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") {
set $block_spam 1;
}
if ($block_spam = 1) {
return 444;
}
set $block_user_agents 0;
if ($http_user_agent ~ "Wget") {
 set $block_user_agents 1;
}
# Disable Akeeba Remote Control 2.5 and earlier
if ($http_user_agent ~ "Indy Library") {
set $block_user_agents 1;
}
# Common bandwidth hoggers and hacking tools.
if ($http_user_agent ~ "libwww-perl") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetRight") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GetWeb!") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go!Zilla") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Download Demon") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "Go-Ahead-Got-It") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "TurnitinBot") {
set $block_user_agents 1;
}
if ($http_user_agent ~ "GrabNet") {
set $block_user_agents 1;
}
if ($block_user_agents = 1) {
return 444;
}

###end####
     location ~ ^/list {
         #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         #对不同的HTTP状态码设置不同的缓存时间
         proxy_cache_valid  200 301 302 304 1d;
         #proxy_cache_valid  any 1d;
         #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
         proxy_cache_key $host$uri$is_args$args;
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
         #proxy_ignore_headers Set-Cookie;
         #proxy_hide_header Set-Cookie;
         proxy_pass
         add_header      Nginx-Cache     "$upstream_cache_status  from  km";

          expires      1d;
        }

    access_log  /data1/logs/abc.com.log access;    #nginx访问日志
  }

-----------------------ssl(https)相关------------------------------------

server {
  listen 13820; #监听端口
  server_name localhost;
  charset utf-8; #gbk,utf-8,gb2312,gb18030 可以实现多种编码识别
  ssl on; #开启ssl
  ssl_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/server.crt; #服务的证书
  ssl_certificate_key /ls/app/nginx/conf/mgmtxiangqiankeys/server.key; #服务端key
  ssl_client_certificate /ls/app/nginx/conf/mgmtxiangqiankeys/ca.crt; #客户端证书
  ssl_session_timeout 5m; #session超时时间
  ssl_verify_client on; # 开户客户端证书验证 
  ssl_protocols SSLv2 SSLv3 TLSv1; #允许SSL协议 
  ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #加密算法
  ssl_prefer_server_ciphers on; #启动加密算法
  access_log /lw/logs/nginx/dataadmin.test.com.ssl.access.log access ; #日志格式及日志存放路径
  error_log /lw/logs/nginx/dataadmin.test.com.ssl.error.log; #错误日志存放路径

}

-------------------------------------------------------------------------
}





user nginx nginx ;    Nginx用户及组:用户 组。window下不指定



worker_processes 8;   工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU


error_log  logs/error.log;  


error_log  logs/error.log  notice;  


error_log  logs/error.log  info;           错误日志:存放路径。


 


pid logs/nginx.pid;          pid(进程标识符):存放路径。


 


worker_rlimit_nofile 204800;       指定进程可以打开的最大描述符:数目。


这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致

现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。

这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。

events

{

use epoll;

使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。

补充说明:

与apache相类,nginx针对不同的操作系统,有不同的事件模型

A)标准事件模型

Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll

B)高效事件模型

Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。

Epoll:使用于Linux内核2.6版本及以后的系统。

/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。

Eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。



worker_connections 204800;

没个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。

每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections


keepalive_timeout 60;   keepalive超时时间。


client_header_buffer_size 4k;


客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。

分页大小可以用命令getconf PAGESIZE 取得。

[root@web001 ~]# getconf PAGESIZE

4096


但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。


open_file_cache max=65535 inactive=60s;

这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

open_file_cache_valid 80s;

这个是指多长时间检查一次缓存的有效信息。

open_file_cache_min_uses 1;

open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。

}


##设定http服务器,利用它的反向代理功能提供负载均衡支持

http

{

include mime.types;

设定mime类型,类型由mime.type文件定义

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"';

log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';

日志格式设置。

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

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

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

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


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


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


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


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


通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。

反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。


access_log  logs/host.access.log  main;

access_log  logs/host.access.404.log  log404;

用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径


server_names_hash_bucket_size 128;


#保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。

参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.

 
client_header_buffer_size 4k;

客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。


large_client_header_buffers 8 128k;

客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果
header过大,它会使用large_client_header_buffers来读取。


open_file_cache max=102400 inactive=20s;

这个指令指定缓存是否启用。

例: open_file_cache max=1000 inactive=20s; 

open_file_cache_valid 30s; 

open_file_cache_min_uses 2; 

open_file_cache_errors on;

open_file_cache_errors

语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.


open_file_cache_min_uses


语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.
open_file_cache_valid

语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.
 
client_max_body_size 300m;     设定通过nginx上传文件的大小


 sendfile on;    

sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。


tcp_nopush on; 此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用

proxy_connect_timeout 90; 

后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout 180;

连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)

proxy_send_timeout 180;


后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

proxy_buffer_size 256k;

设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小

proxy_buffers 4 256k;

设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k

proxy_busy_buffers_size 256k;

proxy_temp_file_write_size 256k;

设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长

proxy_temp_path /data0/proxy_temp_dir;

proxy_temp_path和proxy_cache_path指定的路径必须在同一分区

proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB

keepalive_timeout 120;

keepalive超时时间。

tcp_nodelay on;

client_body_buffer_size 512k;

如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。

无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误

proxy_intercept_errors on;

表示使nginx阻止HTTP应答代码为400或者更高的应答。

upstream bakend {

server 127.0.0.1:8027;
server 127.0.0.1:8028;
server 127.0.0.1:8029;
hash $request_uri;

}


nginx的upstream目前支持4种方式的分配


1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。


2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}


3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}


4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配
upstream backend {
server server1;
server server2;
fair;
}


5、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {

server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;

}

tips:

upstream bakend{#定义负载均衡设备的Ip及设备状态}
{
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}

在需要使用负载均衡的server中增加

proxy_pass


每个设备的状态设置为:
1.down表示单前的server暂时不参与负载
2.weight为weight越大,负载的权重就越大。
3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path设置记录文件的目录 可以设置最多3层目录

location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
 
##配置虚拟机


server

{

listen 80;

配置监听端口

server_name image.***.com;

配置访问域名


location ~* \.(mp3|exe)$ {

对以“mp3或exe”结尾的地址进行负载均衡

proxy_pass

设置被代理服务器的端口或套接字,以及URL

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

以上三行,目的是将代理服务器收到的用户的信息传到真实服务器上


}



location /face {


if ($http_user_agent ~* "xnp") {


rewrite ^(.*)$ redirect;


}


proxy_pass


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


error_page 404 502 = @fetch;


}


location @fetch {


access_log /data/logs/face.log log404;


rewrite ^(.*)$ redirect;


}


location /image {


if ($http_user_agent ~* "xnp") {


rewrite ^(.*)$ redirect;


}


proxy_pass


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


error_page 404 502 = @fetch;


}


location @fetch {


access_log /data/logs/image.log log404;


rewrite ^(.*)$ redirect;


}


}






##其他举例


server


{


listen 80;


server_name *.***.com *.***.cn;


location ~* \.(mp3|exe)$ {


proxy_pass


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


}


location / {


if ($http_user_agent ~* "xnp") {


rewrite ^(.*)$ ***img.com/help/noimg.gif redirect;


}


proxy_pass


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


#error_page 404 ***img.com/help/noimg.gif;


error_page 404 502 = @fetch;


}


location @fetch {


access_log /data/logs/baijiaqi.log log404;


rewrite ^(.*)$ ***img.com/help/noimg.gif redirect;


}


}


server


{


listen 80;


server_name *.***img.com;


 


location ~* \.(mp3|exe)$ {


proxy_pass


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


}


location / {


if ($http_user_agent ~* "xnp") {


rewrite ^(.*)$ ***img.com/help/noimg.gif;


}


proxy_pass


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


#error_page 404 ***img.com/help/noimg.gif;


error_page 404 = @fetch;


}


#access_log off;


location @fetch {


access_log /data/logs/baijiaqi.log log404;


rewrite ^(.*)$ ***img.com/help/noimg.gif redirect;


}


}


server


{


listen 8080;


server_name ngx-ha.***img.com;


location / {


stub_status on;


access_log off;


}


}


server {


listen 80;


server_name imgsrc1.***.net;


root html;


}






server {


listen 80;


server_name ***.com w.***.com;


# access_log /usr/local/nginx/logs/access_log main;


location / {


rewrite ^(.*)$ ***.com/ ;


}


}


server {


listen 80;


server_name *******.com w.*******.com;


# access_log /usr/local/nginx/logs/access_log main;


location / {


rewrite ^(.*)$ *******.com/;


}


}


server {


listen 80;


server_name ******.com;


# access_log /usr/local/nginx/logs/access_log main;


location / {


rewrite ^(.*)$ ******.com/;


}


}


location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}


#设定查看Nginx状态的地址


 


location ~ /\.ht {
deny all;
}


#禁止访问.htxxx文件


}


 


注释:变量


Ngx_http_core_module模块支持内置变量,他们的名字和apache的内置变量是一致的。


首先是说明客户请求title中的行,例如$http_user_agent,$http_cookie等等。


此外还有其它的一些变量


$args此变量与请求行中的参数相等


$content_length等于请求行的“Content_Length”的值。


$content_type等同与请求头部的”Content_Type”的值


$document_root等同于当前请求的root指令指定的值


$document_uri与$uri一样


$host与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样


$limit_rate允许限制的连接速率


$request_method等同于request的method,通常是“GET”或“POST”


$remote_addr客户端ip


$remote_port客户端port


$remote_user等同于用户名,由ngx_http_auth_basic_module认证


$request_filename当前请求的文件的路径名,由root或alias和URI request组合而成


$request_body_file


$request_uri含有参数的完整的初始URI


$query_string与$args一样


$sheeme http模式(http,https)尽在要求是评估例如


Rewrite ^(.+)$ $sheme://example.com$; Redirect;


$server_protocol等同于request的协议,使用“HTTP/或“HTTP/


$server_addr request到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。


$server_name请求到达的服务器名


$server_port请求到达的服务器的端口号


$uri等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index

阅读(14300) | 评论(0) | 转发(0) |
0

上一篇:Pandas快速入门

下一篇:nginx配置文件详解

给主人留下些什么吧!~~