您的位置:首页 > 运维架构 > Nginx

[ELK] Nginx 监控

2017-08-03 12:49 246 查看
一、安装nginx 1.11.10
#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# cd/usr/local/src
#tar -zxvf nginx-1.11.10.tar.gz
# cdnginx-1.11.10
#./configure --prefix=/usr/local/nginx
# make&& make install
 
#/usr/local/nginx/sbin/nginx-t     #测试
 
 
二、修改nginx日志格式
# vi/usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    log_format json '{"@timestamp":"$time_iso8601",'        #修改为json格式
               '"@version":"1",'
               '"client":"$remote_addr",'
               '"url":"$uri",'
               '"status":"$status",'
               '"domain":"$host",'
               '"host":"$server_addr",'
               '"size":$body_bytes_sent,'
               '"responsetime":$request_time,'
               '"referer":"$http_referer",'
               '"ua":"$http_user_agent"'
               '}';
    #                  '$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;
        access_log /var/log/nginx/access_json.log json;            #log日志定义放在server{}中
 
        location / {
            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;
    #    }
    #}
 
}
 
 
 
三、启动nginx
#/usr/local/nginx/sbin/nginx

 
四、编写logstash conf文件
input {
 
    file {                                                                              # file插件
        path =>"/var/log/nginx/access_json.log"          #日志
        type =>"nginx"                                                       #定义类型,在输出中可以用做if判断
        start_position =>"beginning"                              #string,
one of ["beginning", "end"]。默认为end
        codec => json                                                          #格式为json
    }
 
}
 
output {
   elasticsearch {                                               
          hosts =>["192.168.1.75:9200"]                        #定义输出ip:port
          index =>"nginx-%{+YYYY.MM.dd}"                  #定义索引,这个在head/kopf/kibana可以方便查询
    }
 
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息