3 nginx.conf文件配置詳解
#user nobody;
worker_processes 1; //指定工作進程的個數,默認爲1,可以進行修改,一般爲 CPU數*核數
#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; //一個woker同時最多可以產生多少個鏈接,默認爲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 / { //響應
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;
# }
#}
}
4.日誌管理
4.1 日誌的格式
server的訪問日誌文件爲: logs/access.log
server的錯誤日誌爲:logs/error.log
使用的格式爲“main”格式
除此之外,可以自定義其他格式
4.2 main日誌格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
4.3 爲某一個server使用main格式日誌的配置
首先,打開log_format
然後:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html;
}
access_log logs/zijide.access.log main;
}
4.4 日誌的定時任務日誌切割
4.4.1 書寫shell腳本,logs.sh
LOGPATH=/application/nginx/logs/access.log #日誌文件位置
BASEPATH=/data #分割文件存儲目錄
bak=$BASEPATH/$(date -d yesterday +%Y%m%d%H%M).zcom.access.log #目標文件路徑加文件名稱
mv $LOGPATH $bak #將源文件移動至目標文件
touch $LOGPATH #新建一個文件
kill -USER1 /application/nginx/logs/nginx.pid #通知nginx讀取新文件
4.4.2編輯定時任務,觸發時間
使用命令:crontab -e
*/1 * * * * sh /application/shellScript/logs.sh #爲了測試方便 一分鐘執行一次