nginx在centos7下的安裝
阿新 • • 發佈:2020-07-27
系統依賴線上安裝
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
獲取nginx安裝包(http://nginx.org/en/download.html)
wget http://nginx.org/download/nginx-1.16.1.tar.gz tar zxvf nginx-1.16.1.tar.gz tar cd nginx-1.16.1/ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_modulemake && make install
需要新增的模組:
--with-http_ssl_module(ssl模組)
--with-http_sub_module(內容替換)等模組
配置nginx系統服務
vim /usr/lib/systemd/system/nginx.service
[Unit] Description=Nginx After=network.target remote-fs.target nss-lookup.target [Service] Type=forking WorkingDirectory=/usr/local/nginx ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
相關命令
# 更新系統服務 systemctl daemon-reload # 新增開機自啟動 systemctl enable nginx # 啟動nginx systemctl start nginx # 關閉nginx systemctl stop nginx # 停止開機自啟動 systemctl disable nginx # 檢視狀態 systemctl status nginx # 重啟服務 systemctl restart nginx # 重新載入配置檔案(注意:配置檔案出錯時,重新整理不會成功,需要關注log檔案輸出)/usr/local/nginx/sbin/nginx -s reload
配置nginx主配置檔案中增加配置引用,使用配置引入的好處是,避免多個配置混雜。也便於配置切換。
Vi /usr/local/nginx/conf/nginx.conf
# 修改使用者,否則日誌無法寫入硬碟 user root # 修改處理執行緒數,提升效能,最好與伺服器CPU數量一致 worker_processes 2; http { …… …… server { listen 80; …… } #注意新增位置 include conf.d/*.conf; }
在nginx的conf目錄下建立conf.d目錄
conf/conf.d/test-nginx.conf
# 分流配置,根據轉發地址修改 upstream asnp_proxy { # 如果失敗兩次,則60秒後重試 # server 192.168.32.32:5888 max_fails=2 fail_timeout=60s; # server 192.168.32.32:6888 max_fails=2 fail_timeout=60s; server 192.168.100.72:8080; } #日誌格式設定,無需修改 #$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址; #$remote_user:用來記錄客戶端使用者名稱稱; #$time_local: 用來記錄訪問時間與時區; #$request: 用來記錄請求的url與http協議; #$status: 用來記錄請求狀態;成功是200,錯誤是500 #$body_bytes_sent :記錄傳送給客戶端主體內容大小; #$http_referer:用來記錄從那個頁面連結訪問過來的; #$http_user_agent:記錄客戶瀏覽器的相關資訊; #$request_time : 整個請求的總時間 #$upstream_response_time:請求過程中,upstream響應時間 #$bytes_sent :客戶端傳送的位元組數 #$request_length:客戶端請求的長度 #$upstream_status:upstream狀態 #$upstream_addr :後臺upstream的地址,即真正提供服務的主機地址 log_format asnp_main '{ "@timestamp": "$time_iso8601", ' '"time": "$time_iso8601", ' '"remote_addr": "$remote_addr", ' '"remote_user": "$remote_user", ' '"host": "$host", ' '"request": "$request", ' '"uri": "$uri", ' '"request_method": "$request_method", ' '"status": "$status", ' '"request_time": "$request_time", ' '"upstream_response_time": "$upstream_response_time", ' '"request_length": "$request_length", ' '"bytes_sent": "$bytes_sent", ' '"upstream_addr": "$upstream_addr", ' '"upstream_status": "$upstream_status", ' '"http_referrer": "$http_referer", ' '"http_x_forwarded_for": "$http_x_forwarded_for", ' '"http_user_agent": "$http_user_agent" ' '}'; # 限流配置,如有限流需要則開啟,開啟後需要在loaction中增加對應配置 # limit_req_zone $uri zone=db_access:20m rate=1r/s; server { # 對外的代理埠,根據需要修改 listen 80; # 代理伺服器名稱,填寫本機IP即可 server_name 192.168.100.72; # 日誌配置,可以按各種頻率輸出,目前是按月,也可以按天 if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") { set $year $1; set $month $2; set $day $3; set $hour $4; set $minutes $5; set $seconds $6; } # 日誌輸出路徑配置 access_log logs/asnp_access-$year-$month.log asnp_main; error_log logs/asnp_error.log; # 開放http訪問 location / { proxy_pass http://asnp_proxy/; # 設定IP proxy_set_header X-Real-IP $remote_addr; #後端的Web伺服器可以通過X-Forwarded-For獲取使用者真實IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 連線後端伺服器超時時間(秒) proxy_connect_timeout 30; # 將域名代理過去 proxy_set_header Host $host:$server_port; } }