Nginx問題及解決方案


提示錯誤信息

nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)

原因分析:nginx/目錄下沒有logs文件夾

 

 

 解決方法:

mkdir logs
chmod 700 logs

正常情況的信息輸出:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

啟動成功后打不開鏈接

1.ping

2telnet 端口(CentOS7:驗證端口  firewall-cmd --query-port=80/tcp)

  下面我們開啟80端口:
  firewall-cmd --add-port=80/tcp --permanent
  #重啟防火牆
  systemctl restart firewalld
  --permanent #永久生效,沒有此參數重啟后失效

 

配置nginx開機自啟動

vim /etc/rc.d/rc.local

 

 

pid問題

問題描述
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid" # pid丟 失
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) # 文件或者文件夾丟失
解決方法
如果文件夾缺失,則創建該文件夾
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 執行重新啟動命令,則會生成nginx.pid文件

nginx日志切割

 第一種
nginx的日志分為訪問日志(access.log)和錯誤日志(error.log),日志過大會影響nginx的性能,也不利於日志的分析,所以需要將日志分割處理。
日志常見的配置參數:
$remote_addr 客戶端的ip地址(代理服務器,顯示代理服務ip)
$remote_user 用於記錄遠程客戶端的用戶名稱(一般為“-”)
$time_local 用於記錄訪問時間和時區
$request 用於記錄請求的url以及請求方法
$status 響應狀態碼,例如:200成功、404頁面找不到等。
$body_bytes_sent 給客戶端發送的文件主體內容字節數
$http_user_agent 用戶所使用的代理(一般為瀏覽器)
$http_x_forwarded_for 可以記錄客戶端IP,通過代理服務器來記錄客戶端的ip地址
$http_referer

可以記錄用戶是從哪個鏈接訪問過來的 

1. 進入/usr/local/nginx/sbin目錄下,並創建shell文件 
cd /usr/local/nginx/sbin 
touch cut_nginx_log.sh
chmod +x cut_nginx_log.sh
2. 編輯shell文件,編寫日志切割命令 
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/nginx/logs/historyLog"
logPath="/usr/local/nginx/logs/"
record_time=$(date -d "yesterday" +"%Y-%m-%d")
#創建備份目錄
mkdir -p ${logs_path}/
#將日志移動到上面的目錄中
mv ${logPath}/access.log ${logs_path}/access、access.${record_time}.log
mv ${logPath}/error.log ${logs_path}/error、error.${record_time}.log
#nginx重讀配置文件
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid) 

第二種

1. 安裝定時任務
yum install crontabs
2. 添加到crontab定時任務中 
crontab -e 0 0 * * * bash /usr/local/nginx/sbin/cut_nginx_log.sh #將於每天凌晨0點0分將 nginx日志重命名為昨天的日期格式,並重新生成今天的新日志
3. 重啟crontab服務 
/sbin/service crond restart //重啟服務 
/sbin/service crond reload //重新載入配置
常用定時任務命令 
  CentOS6上的cron命令: 
service crond start // 啟動服務
service crond stop // 關閉服務
service crond restart // 重啟服務
service crond reload // 重新載入配置
service crond status // 查看狀態
crontab -e // 編輯任務
crontab -l // 查看任務列表 
  CentOS7上的cron命令: 
systemctl start crond.service/crond start // 啟動服務
systemctl stop crond.service/crond stop // 關閉服務
systemctl restart crond.service/crond restart // 重啟服務
systemctl reload crond.service/crond reload // 重新載入配置
systemctl status crond.service/crond status // 查看狀態
  crontab命令介紹
 
星期幾 年(可選) 
取值范圍
0-59
0-23 
1-31 
1-12
1-7
2021/2022/.....
常用表達式示例: 
*/1 * * * * // 每分鍾執行 
59 23 * * * // 每日凌晨(每天晚上23:59)執行
0 1 * * * // 每天1點執行
0 0 * * 1 // 每周一執行

 

運維頁面

使用錯誤碼判斷是否跳轉到運維頁面 
狀態碼
含義
500
服務器遇到了一個未曾預料的狀況,導致了它無法完成對請求的處理。一般來說,這個問
題都會在服務器的程序碼出錯時出現。 
501
服務器不支持當前請求所需要的某個功能。當服務器無法識別請求的方法,並且無法支持
其對任何資源的請求。 
502
作為網關或者代理工作的服務器嘗試執行請求時,從上游服務器接收到無效的響應。 
503
由於臨時的服務器維護或者過載,服務器當前無法處理請求。這個狀況是臨時的,並且將
在一段時間以后恢復。如果能夠預計延遲時間,那么響應中可以包含一個 Retry-After 頭用
以標明這個延遲時間。如果沒有給出這個 Retry-After 信息,那么客戶端應當以處理500響
應的方式處理它。 注意:503狀態碼的存在並不意味着服務器在過載的時候必須使用
它。某些服務器只不過是希望拒絕客戶端的連接。 
504
作為網關或者代理工作的服務器嘗試執行請求時,未能及時從上游服務器(URI標識出的
服務器,例如HTTP、FTP、LDAP)或者輔助服務器(例如DNS)收到響應。 注意:
某些代理服務器在DNS查詢超時時會返回400或者500錯誤
505
服務器不支持,或者拒絕支持在請求中使用的 HTTP 版本。這暗示着服務器不能或不願使
用與客戶端相同的版本。響應中應當包含一個描述了為何版本不被支持以及服務器支持哪
些協議的實體。 
506
由《透明內容協商協議》(RFC 2295)擴展,代表服務器存在內部配置錯誤:被請求的協
商變元資源被配置為在透明內容協商中使用自己,因此在一個協商處理中不是一個合適的
重點。 
207
服務器無法存儲完成請求所必須的內容。這個狀況被認為是臨時的。WebDAV (RFC 4918)
209
服務器達到帶寬限制。這不是一個官方的狀態碼,但是仍被廣泛使用。 
510
獲取資源所需要的策略並沒有沒滿足。(RFC 2774) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM