nginx.conf 檔案配置詳解
阿新 • • 發佈:2019-02-11
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 #為了測試方便 一分鐘執行一次