基于CentOS7的系統:
一、下載下傳并上傳包到linux上
1、使用
wget
指令下載下傳(推薦)。確定系統已經安裝了wget,如果沒有安裝,執行 yum install wget 安裝。
# 下載下傳
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
# 解壓
tar -zxvf nginx-1.12.0.tar.gz
# 重命名
mv nginx-1.12.0 nginx
2、可以從官網下載下傳,然後使用rz指令上傳壓縮包
如果rz使用不了,執行指令yum install lrzsz 安裝rzsz後,再執行rz
二、在 nginx.conf 中加上對于日志的json格式化
vim /usr/local/nginx/conf/nginx.conf
#在http下加上:
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":1,'
'"host":"$server_addr",'
'"client":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"domain":"$host",'
'"url":"$uri",'
'"status":"$status"}';
access_log /opt/elk-data/logs/access.log json;

三、測試配置是否可以正常運作,并解決相應問題
/usr/local/nginx/sbin/nginx -t
# 顯示如下結果就是正常運作
Password:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
出現以下問題則對應解決:
1. nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
原因:目前使用者對該位置沒有寫入權限
解決辦法:
l 使用指令:sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 以root權限啟動
l 使用指令:sudo chmod -R a+rw /usr/local/nginx 給所有使用者賦權限(個人學習,不考慮安全問題)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 啟動Nginx
注:以非root權限啟動時,會出現 nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied) 錯誤
原因:Linux隻有root使用者可以使用1024一下的端口
解決辦法:
l 已root權限啟動
l 将 /usr/local/nginx/conf/nginx.conf 檔案中的80端口改為1024以上
server {
# listen 80
listen 8080
……
}
2. nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
在 /usr/local/nginx/logs/ 下建立一個error.log 的檔案就可以了
cd /usr/local/nginx/logs/
mkdir error.log
3. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
參照這個:https://blog.csdn.net/ownfire/article/details/7966645
4. nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
解決方法:找到你的nginx.conf的檔案夾目錄,然後運作類似如下指令
nginx -c etc/nginx/nginx.conf
再運作nginx -s reload,就可以了。
5、yum install gcc gcc-c++ //執行指令後出現的問題,一直循環執行其中的三句,如下圖所示:
解決辦法就是先ctrl+z一下,接着輸入rm –f /var/run/yum.pid
注意:一定要有網
6、unbuntu下安裝安裝pcre-8.37 configure: error: You need a C++ compiler for C++ support
解決辦法連結是:http://www.ithao123.cncontent-9518048.html
7、安裝必要的庫(nginx 中gzip子產品需要 zlib 庫,rewrite子產品需要 pcre 庫,ssl 功能需要openssl庫)
參考這個連結: https://blog.csdn.net/qq_40673345/article/details/103695894
8、curl: (7) Failed connect to 192.168.194.6:80; Connection refused
[[email protected] ~]# curl 192.168.194.6:80
curl: (7) Failed connect to 192.168.194.6:80; Connection refused
檢查 nginx.conf 的配置
server 的listen 改成0.0.0.0:80,用0.0.0.0來代替自己的ip
9、啟動 nginx 後 access.log 收集不到日志
重新開機一下 nginx 就可以了
/usr/local/nginx/sbin/nginx -s reload