Nginx 配置文件解讀及日誌切割

配置文件

#設置worker進程的用戶,指的是Linux中的用戶,會涉及到NGINX操作目錄或文件的一些權限,默認就是nobody
#user  nobody;
#worker進程的數量
worker_processes  1;

#錯誤日誌 debug info notice warn error crit 從左到右級別越來越高
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#NGINX的進程號
#pid        logs/nginx.pid;


events {
    #默認使用epoll
    use epoll
    #每個worker進程允許的客戶端最大連接數,根據自己機器的硬件配置進行修改
    worker_connections  1024;
}

#網絡傳輸的相關模塊
http {
    #可以導入外部的指令塊配置,提高可讀性,避免單個配置文件過大,可以導致自定義文件,比如配置多個server時,自定義一個server的config,然後導入,也是可以的
    include       mime.types;
    #默認的type類型
    default_type  application/octet-stream;

    #  $後跟變量
    #log_format  main  '$remote_addr(客戶端IP) 
    #- $remote_user(遠程客戶端用戶名) 
    #[$time_local](時間和時區) 
    #"$request" '(請求的URL以及method)
    # '$status(響應的狀態碼) 
    #$body_bytes_sent(響應客戶端內容字節數) 
    #"$http_referer" '(記錄用戶從哪個鏈接跳轉過來的)
    #'"$http_user_agent"(用戶所使用的代理,一般都來自瀏覽器) 
    #"$http_x_forwarded_for"'(通過代理服務器來記錄客戶端的IP);

    #記錄用戶的每一次請求,每一次請求都能在這個日誌裏面查找
    #access_log  logs/access.log  main;
    
    #使用高效文件傳輸,提升傳輸性能,啓用後才能使用tcp_nopush,是指當下數據積累到一定大小後才發送,提高效率
    sendfile        on;
    #tcp_nopush     on;

    #設置客戶端與服務端請求的超時時間,保證客戶端多次請求的時候不會創建新的建立,節約資源損耗
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #啓用壓縮,html/js/css壓縮後傳輸會更快
    #gzip  on;

    #在http指令快中設置多個虛擬主機
    server {
        #監聽端口
        listen       80;
        #IP 域名
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #請求路由映射,匹配攔截
        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;
    #    }
    #}


}

使用命令課查看當前worker進程的用戶信息,自己可以配置爲root用戶權限

[root@VM_0_14_centos conf]# ps -ef|grep nginx
root     12066  7239  0 15:14 pts/0    00:00:00 grep nginx
root     17525     1  0 11:41 ?        00:00:00 nginx: master process ./nginx
//我們可以發現worker進程操作的用戶默認是nobody
nobody   17526 17525  0 11:41 ?        00:00:00 nginx: worker process

NGINX的 pid 打開失敗以及失效的解決方案

1:如果文件夾沒了,mkdir 創建文件夾

2:重新指定nginx.conf

./nginx -c /usr/local/nginx/conf/nginx.conf 

3:使用默認的pid

nginx日誌切割

現有的日誌文件都會存在access.log文件中,但是隨着時間的推移,這個文件會越來越大,不便於運維人員查看,所以我們可以通過把這個大的日誌文件切割爲不同的小文件,切割規則可以按天作爲單位,如果每天都有幾百個G或者幾個T的日誌文件的話,則可以按需求以每半天或者每小時去切割日誌。

1:創建一個shell可執行文件:cut_my_log.sh,內容如下

#!/bin/bash
LOG_PATH="/var/log/nginx/"
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d-%H:%H)
PID=/var/run/nginx/nginx.pid
mv ${LOG_PATH}/access.log ${LOG_PATH}/access.log.${RECORD_TIME}.log
mv ${LOG_PATH}/error.log ${LOG_PATH}/error.log.${RECORD_TIME}.log
#向nginx主進程發送信號,由於重新打開日誌文件
kill -USR1 'cat $PID'

2:爲cut_my_log.sh添加可執行的權限

chmod +x cut_my_log.sh

3:測試日誌切割後的結果

./cut_my_log.sh

使用定時任務

1:安裝定時任務

yum install corntabs

2:corntab -e 編輯並且添加一行新的任務

*/1 * * * * /usr/local/nginx/sbin/cut_my_log.sh

3:重啓定時任務

service cornd restart

常用命令

service cornd start 啓動任務

service cornd stop 關閉任務

service cornd restart 重啓服務

service cornd reload 重新載入配置

cornd -e 編輯任務

cornd -l 查看任務列表

 

定時任務備份數據庫,參考慕課網老師的備份數據庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章