生产环境nginx常用配置文件

nginx常用配置文件

1.主配置文件

user www www;
worker_processes  2;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;

events {
        use epoll;
        worker_connections  51200;
}

http {
        charset utf-8;
        include       mime.types;
        default_type  application/octet-stream;
        sendfile  on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout  60;
        server_tokens off;
        
 
        limit_conn_log_level error;  #当达到最大限制连接数时,记录日志的等级
        limit_conn_status 503;       #该参数在1.3.15版本引入的。指定当超过限制时,返回的状态码(默认是503),code值只能设置在400到599之间。
        limit_conn_zone $binary_remote_addr zone=one:10m;
        #用于定义一个zone,该zome将会被用于存储会话状态。能够存储的会话数量是由定义的size大小和系统参数memory_max_size的大小决定的。
        #zone是共享内存空间,作用是保存每个key对应的连接数,此处可以按照需求自己定义名字。
        limit_conn_zone $server_name zone=perserver:10m; #
        limit_req_zone $binary_remote_addr zone=allips:10m   rate=10r/s;#对每个连接的速率限制。参数rate的单位是字节/秒,设置为0将关闭限速。 
        #按连接限速而不是按IP限制,因此如果某个客户端同时开启了两个连接,那么客户端的整体速率是这条指令设置值的2倍。

        log_format main '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';
        access_log logs/access.log main;

        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;

        include vhosts/*.conf;
        include upstream/*.conf;
}

2.主机配置文件

server {
        listen  80;
        server_name xxx.xxx.xxx;
        root /data/web/myblog;
        index index.html index.php;
        access_log  /usr/local/nginx/logs/blog/access.log main;
        error_log  /usr/local/nginx/logs/blog/error.log;

        limit_conn  one  10;
        limit_conn perserver 1000;
        limit_req zone=allips  burst=10 nodelay;


        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;
        }

        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
              root   /opt/web/error_page/;
        }
}

3.定制json格式的日志文件,记录客户端IP

  • 修改主配置文件中日志格式
        log_format  main escape=json '{"remote_addr":"$remote_addr",'
                      '"remote_user":"$remote_user",'
                      '"time_local":"$time_local",'
                      '"request":"$request",'
                      '"status":"$status",'
                      '"body_bytes_sent":"$body_bytes_sent",'
                      '"http_referer":"$http_referer",'
                      '"http_user_agent":"$http_user_agent",'
                      '"http_x_forwarded_for":"$http_x_forwarded_for"}';
        access_log logs/access.log main;
posted @ 2020-11-19 17:53  lnsix  阅读(623)  评论(0编辑  收藏  举报