1.下載
2.部署
2.1安裝前提
在linux下安裝需要安裝一下組件
1.?gcc?&&?g++ yum?install?gcc-c++ 2.?pcre yum?install?-y?pcre?pcre-devel 3.?zlib yum?install?-y?zlib?zlib-devel 4.?openssl yum?install?-y?openssl?openssl-devel
2.2 安裝
1.?解壓nginx文件 tar?-zxvf?nginx-1.17.5.tar.gz 2.?安裝 ##?創建一個nginx安裝目錄 mkdir?nginx cd?nginx-1.12.2 ##?指定文件安裝路徑 ./configure?--prefix=/home/lege/nginx make make?install #安裝完成后內容會安裝到指定的路徑?/home/lege/nginx下,否則會在默認目錄/usr/local/nginx
2.3?啟動nginx
##?修改配置文件 cd?/home/lege/nginx/conf vim?nginx.conf ##?設置端口為8080,也可設置成其他 listen????8080; ##?進入到啟動目錄 cd?/home/lege/nginx/sbin ##?檢查配置文件是否有問題 ./nginx?-t ##沒有問題的結果如下所示: [soa@testsoa04?sbin]$?./nginx?-t nginx:?the?configuration?file?/home/lege/nginx/conf/nginx.conf?syntax?is?ok nginx:?configuration?file?/home/lege/nginx/conf/nginx.conf?test?is?successful [soa@testsoa04?sbin]$? ? ##?查詢配置參數 ./nginx?-v ##?對于已安裝的nginx需要修改配置參數 ./configure?--prefix=/home/lege/nginx?...配置參數 make make?install? 然后重新啟動nginx即可 ##?啟動 ./nginx ##?停止 ./nginx?-s?stop ##?重啟 ./nginx?-s?reload ##?輸入網址驗證是否啟動成功 http://ip:port/
3. nginx日志相關配置
日志定義的格式: 語法格式:??access_log?path?[format?[buffer=size]?[gzip[=level]]?[flush=time]?[if=condition]]; ???????????access_log?off; 默認值??:?access_log?logs/access.log?combined; 作用域??:?http,?server,?location,?if?in?location,?limit_except ? 1.?定義日志格式 語法格式:??log_format?name?[escape=default|json]?string?...; 默認值??:??log_format?combined?"..."; 作用域??:??http ? 常見的日志變量 $remote_addr,?$http_x_forwarded_for?記錄客戶端ip地址 $remote_user記錄客戶端用戶名稱 $request記錄請求的url和http協議(get,post,del,等) $status記錄請求狀態 $body_bytes_sent發送給客戶端的字節數,不包括響應頭的大小;?該變量與apache模塊mod_log_config里的“%b”參數兼容。 $bytes_sent發送給客戶端的總字節數。 $connection連接的序列號。 $connection_requests?當前通過一個連接獲得的請求數量。 $msec?日志寫入時間。單位為秒,精度是毫秒。 $pipe如果請求是通過http流水線(pipelined)發送,pipe值為“p”,否則為“.”。 $http_referer?記錄從哪個頁面鏈接訪問過來的 $http_user_agent記錄客戶端瀏覽器相關信息 $request_length請求的長度(包括請求行,請求頭和請求正文)。 $request_time?請求處理時間,單位為秒,精度毫秒;?從讀入客戶端的第一個字節開始,直到把最后一個字符發送給客戶端后進行日志寫入為止。 $time_iso8601?iso8601標準格式下的本地時間。 $time_local通用日志格式下的本地時間。 ? 示例: log_format?main?'$remote_addr?-?$remote_user?[$time_local]?"$request"?' ???????????'$status?$body_bytes_sent?"$http_referer"?"$request_time"?' ???????????'"$http_user_agent"?"$http_x_forwarded_for"
對于日志相關的清理可以使用linux的定時任務去處理,示例如下:
clear.sh腳本如下:
#!/bin/bash #logs_path為日志存放路徑 logs_path=/home/lege/data/nginx/logs yesterday=$(date?-d?"yesterday"?+%y-%m-%d) keeptime=$(date?-d?"-3?days"?+%y-%m-%d) #切分日志文件 mv?${logs_path}/access.log?${logs_path}/access_${yesterday}.log mv?${logs_path}/error.log?${logs_path}/error_${yesterday}.log #通過nginx信號量控制重讀日志,/web/nginx/為nginx安裝目錄 kill?-usr1?$(cat?/home/lege/data/nginx/logs/nginx.pid) #刪除3天前的日志文件 rm?-f?${logs_path}/access_{keeptime}.log rm?-f?${logs_path}/error_{keeptime}.log echo?0 ? crontab?-e?添加如下: 0?0?*?*?*?/bin/sh?/home/lege/data/nginx/logs/clear.sh ? crontab?-l?查看是否添加成功
4. nginx使用之充當文件下載器
http?{ ??server_tokens?off; ??client_header_buffer_size?8k; ??client_max_body_size?130m; ??proxy_buffer_size??64k; ??proxy_buffers??8?64k; ??log_format?access?'$remote_addr?$host?$remote_user?[$time_local]?$status?$request_length?$body_bytes_sent?$request_time?0?0?0?-?"-"?"$request"?"$http_referer"?"$http_user_agent"?$http_cookie?$bytes_sent'; ??access_log?logs/access.log?access; ??keepalive_requests?16; ??keepalive_timeout?5; ??server?{ ????listen?8080; ????server_name?localhost; ????charset?utf-8; ????location?/?{ ??????default_type?'application/octet-stream'; ??????add_header?content-disposition?"attachment"; ??????##?配置可以下載的文件路徑,下面是在windows下測試使用的路徑,linux也可換成對應的路徑 ??????root??d://tools//nginx-1.17.4//conf; ?????} ???} }
配置完成后重啟nginx,然后進入瀏覽器輸入下的文件名即可下載到對應的文件。ps:不能下載目錄只能下載文件。
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END