zoukankan      html  css  js  c++  java
  • 【Nginx系列】Nginx虚拟主机的配置核日志管理

    Nginx配置段

    #user  nobody;
    worker_processes  1;// 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {// 一般是配置nginx连接的特性  如1个word能同时允许多少连接
        worker_connections  1024;// 这是指 一个子进程最大允许连1024个连接
    }
    
    
    http {//这是配置http服务器的主要段
        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;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {// 这是虚拟主机段
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {//定位,把特殊的路径或文件再次定位 ,如image目录单独处理
                root   html;
                index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ .php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #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;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    

    一、基于域名的配置

     server {
            listen 80;  #监听端口
            server_name sang.com; #监听域名
    
            location / {
                    root sang.com;   #根目录定位
                    index index.html;
            }
        }
    

    二、基于端口的配置

     server {
            listen 8080;
            server_name sang.com;
    
            location / {
                    root /var/sang/html;
                    index index.html;
            }
        }
    

    三、基于IP的配置  

     server {
            listen 80;
            server_name 192.168.1.200;
    
            location / {
                    root html/ip;#ip目录
                    index index.html;
            }
        }
    

    四、日志管理  

    我们观察nginxserver,可以看到如下类似信息

     #access_log  logs/host.access.log  main;

    这说明 server, 它的访问日志的文件是  logs/host.access.log ,

    使用的格式”main”格式.

    除了main格式,你可以自定义其他格式.

    main格式是什么?

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

        #                  '$status $body_bytes_sent "$http_referer" '

        #                  '"$http_user_agent" "$http_x_forwarded_for"';

    main格式是我们定义好一种日志的格式,并起个名字,便于引用.

    以上面的例子, main类型的日志,记录的 remote_addr.... http_x_forwarded_for等选项.

    1: 日志格式 是指记录哪些选项

    默认的日志格式: main

         log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                                '$status $body_bytes_sent "$http_referer" '

                                '"$http_user_agent" "$http_x_forwarded_for"';

    如默认的main日志格式,记录这么几项

    远程IP- 远程用户/用户时间 请求方法(GET/POST) 请求体body长度 referer来源信息

    http-user-agent用户代理/蜘蛛 ,被转发的请求的原始IP

    http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP

    2: 声明一个独特的log_format并命名

        log_format  mylog '$remote_addr- "$request" '

                         '$status $body_bytes_sent "$http_referer" '

                            '"$http_user_agent" "$http_x_forwarded_for"';

    在下面的server/location,我们就可以引用 mylog

    server段中,这样来声明

    Nginx允许针对不同的server做不同的Log ,(有的web服务器不支持,lighttp)

    access_log logs/access_8080.log mylog;   

    声明log   log位置          log格式;

    3、实际应用:

    shell+定时任务+nginx信号管理,完成日志按日期存储

    分析思路:

    凌晨00:00:01,把昨天的日志重命名,放在相应的目录下

    USR1信息号控制nginx重新生成新的日志文件

    具体脚本:

    #!/bin/bash
    
    
    base_path='/usr/local/nginx/logs'
    
    
    log_path=$(date -d yesterday +"%Y%m")
    
    
    day=$(date -d yesterday +"%d")
    
    
    mkdir -p $base_path/$log_path
    
    
    mv $base_path/access.log $base_path/$log_path/access_$day.log
    
    
    #echo $base_path/$log_path/access_$day.log
    
    
    kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

    定时任务

    Crontab 编辑定时任务

    01 00 * * * /xxx/path/b.sh  每天01(建议在02-04点之间,系统负载小)

  • 相关阅读:
    递归函数
    python学习笔记
    套接字学习笔记
    Tomcat学习笔记3
    Tomcat学习笔记2
    《Python学习手册 第五版》 -第29章 类代码编写细节
    《Python学习手册 第五版》 -第28章 一个更加实际的示例
    《Python学习手册 第五版》 -第27章 类代码编写基础
    《Python学习手册 第五版》 -第26章 OOP:宏伟蓝图
    《Python学习手册 第五版》 -第25章 高级模块话题
  • 原文地址:https://www.cnblogs.com/dream-to-pku/p/8687690.html
Copyright © 2011-2022 走看看