CentOS7 原始碼安裝Nginx及Nginx基本管理設定
CentOS7 安裝 參考文件
CentOS7最小安裝後初始化安裝工具
1:yum install net-tools 參考文件
2:原始碼安裝wget 參考文件或者執行 yum -y install wget
CentOS7 原始碼安裝包步驟
一:Nginx 安裝步驟
1-1:原始碼下載地址http://nginx.org/en/download.html
執行命令:wgetwget http://nginx.org/download/nginx-1.21.3.tar.gz
1-2:解壓下載的檔案
執行命令:tar -zxvf {壓縮檔名}.tar.gz
1-3:配置configure 檢測安裝及依賴資訊
檢視命令文件執行:./configure --help
先安裝編譯器
執行:sudo yum -y install gcc
再次執行:./configure 進行配置 如下圖:
檢測到有依賴包沒有裝,根據提示去安裝 pcre
執行命令:sudo yum -y install pcre-devel
再次執行:./configure 如下圖:
根據提示繼續安裝依賴包:
執行命令:sudo yum -y install zlib-devel
一直重複執行./configure 直到檢測到沒有依賴項就說明檢測通過了,如下圖:
檢測並指定安裝路徑命令:./configure --prefix=/usr/local/nginx
、
1-4:執行make指令:原始碼生成可執行檔案,中途不能出現error
1-5:安裝 make install
執行安裝命令:sudo make install
測試:
1:進入:/usr/local/nginx 執行:./sbin/nginx
2:在瀏覽器中輸入:http://localhost
3:安裝一個文字瀏覽器測試 yum -y install elinks
Nginx相關目錄檔案
nginx path prefix:/usr/local/nginx (Nginx安裝目錄)
nginx binary file:/usr/local/nginx/sbin/nginx(Nginx啟動目錄)
nginx moduless path:/usr/local/nginx/modules(Nginx相關模組目錄)
nginx configuration prefix:/usr/local/nginx/conf(Nginx配置檔案目錄)
nginx configuration file:/usr/local/nginx/conf/nginx.con(nginx啟動配置檔案)
nginx pid file:/usr/local/nginx/logs/nginx.pid(nginx程序號)
nginx error log file:/usr/local/nginx/logs/error.log:(nginx錯誤日誌檔案)
nginx http access log file:/usr/local/nginx/logs/access.log(訪問日誌檔案)
先關閉防火牆 (測試需要在區域網中訪問)
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
Nginx新增到環境變數
使用軟連線將nginx
連結到/usr/local/sbin
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin ||/usr/local/sbin/ | grep "nginx"
顯示當前環境變數PATH
echo $PATH
編輯.bash_profile
檔案
vim ~/.bash_profile
在.bash_profile
檔案末尾加入以下內容
export PATH=$PATH:/usr/local/nginx/sbin
引用.bash_profile檔案
source ~/.bash_profile
使用nginx命令
# 啟動nginx
nginx
# 停止nginx
nginx -s quit
nginx部分配置
# 啟動該程式的預設程式 #user nobody; # 一個主程序和多個工作程序。這裡定義的是主程序數量 worker_processes 4; # 全域性錯誤日誌的位置及日誌格式 #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; } # 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; # 全域性日誌路徑 # $remote_addr與$http_x_forwarded_for用以記錄客戶端ip地址 # $remote_user:記錄客戶端名稱 # $time_local:記錄訪問時間與時區 # $request:記錄訪問狀態 200成功 # $body_bytes_sent:記錄傳送給客戶端檔案主體內容大小 # $http_referer:記錄從哪個頁面連結訪問過來的 # $http_user_agent:記錄客戶端瀏覽器相關資訊 # sendfie指令指定nginx是否呼叫sendfile函式(zero copy 方式)來輸出檔案 sendfile on; # 允許或禁止使用socke的TCP_CORK的選項僅在使用sendfile的時候使用 #tcp_nopush on; # 長連線超時時間 #keepalive_timeout 0; keepalive_timeout 65; # 開啟壓縮 #gzip on; # 預設網站 配置虛擬主機 server { # 虛擬主機使用的埠 listen 80; # 主機域名 server_name localhost; # 支援的字符集 charset utf-8; #訪問日誌路徑 #access_log logs/host.access.log main; # 定義web根路徑 location / { # 根目錄路徑 root html; # 索引頁面 index index.html index.htm; } # 訪問控制目錄 location /a { # 允許訪問 allow 127.0.0.0; # 不允許 deny all; # return 404; return http://www.baidu.com; } #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; } } }View Code
如果修改了配置檔案先進入nginx目錄中:cd /usr/local/nginx ,在執行:./sbin/nginx -g ./conf/nginx.conf 檢測配置檔案是否正確
修改配置重啟nginx 方式一:
先執行:killall nginx
在執行:cd /usr/local/nginx 執行:./sbin/nginx
修改配置重啟nginx 方式一:
killall -s HUP nginx (重新載入配置檔案)
nginx訪問目錄配置
注意:訪問控制目錄中的location /a{} 這裡的location值得就是 web根路徑中的root路徑
# 定義web根路徑 location / { # 根目錄路徑 root html; # 索引頁面 index index.html index.htm; } # 訪問控制目錄 location /a { # 允許訪問 allow 127.0.0.0; # 不允許 deny all; # return 404; return http://www.baidu.com; }
Nginx日誌管理
Nginx訪問日誌主要有兩個引數控制
1:log_format 用來定義日誌格式,可以定義多種日誌格式,取不同名字即可
2:access_log 用來指定日誌檔案路徑及使用何種日誌格式記錄日誌
log_format格式變數:
# access_log logs/access.log main; # 全域性日誌路徑
# $remote_addr與$http_x_forwarded_for用以記錄客戶端ip地址
` # $remote_user:記錄客戶端名稱
# $time_local:記錄訪問時間與時區
# $request:記錄訪問狀態 200成功
# $body_bytes_sent:記錄傳送給客戶端檔案主體內容大小
# $http_referer:記錄從哪個頁面連結訪問過來的
# $http_user_agent:記錄客戶端瀏覽器相關資訊
自定義格式為json格式:
# 自定義日誌格式 # log_format default_fmt '[$time_local] $remote_addr "$request"' # 自定義json日誌格式 log_format default_fmt_json '{"@timestamp":"$time_local",' '"client_ip":"$remote_addr",' '"request":"$request",' '"status":"$status",' '"bytes":"$body_bytes_sent",' '"x_forwarded":"$http_x_forwarded_for",' '"referer":"$http_referer"' '}';
兩種格式測試效果如下:
Nginx圖片防盜鏈
# 防盜鏈(只針對b目錄) # location /b { # 專案中以下檔案字尾 location ~* \.(png|gif|bmp|jpg|jpeg)$ { valid_referers none blocked *.ticp.net; if ($invalid_referer){ return 403; } }
Nginx虛擬主機
一個web伺服器軟體預設情況下只能釋出一個web,因為一個web分享出去需要三個條件(IP、Port、Domain name)
虛擬主機就是把一臺物理伺服器劃分成多個虛擬伺服器,每個虛擬主機都可以有獨立的域名和獨立的目錄。
1:基於IP的虛擬主機
準備:
1:在nginx根目錄下準備兩個網站如:
/usr/local/nginx/html/web1/index.html
/usr/local/nginx/html/web1/index.html
2:新增一個子IP,使用邏輯網絡卡新增一個 自網絡卡的方式
新增網絡卡:ifconfig ens33:1 192.168.0.131/24 up
刪除網絡卡:ifconfig ens33:1 down
基於IP虛擬主機nginx配置資訊 (/usr/local/nginx/conf/nginx.conf)
user centos; worker_processes 4; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format default_fmt_json '{"@timestamp":"$time_local",' '"client_ip":"$remote_addr",' '"request":"$request",' '"status":"$status",' '"bytes":"$body_bytes_sent",' '"x_forwarded":"$http_x_forwarded_for",' '"referer":"$http_referer"' '}'; sendfile on; keepalive_timeout 65; server { listen 192.168.0.130:80; server_name localhost; charset utf-8; access_log logs/host.access.log default_fmt_json; location / { root html/web1; index index.html index.htm; } } server { listen 192.168.0.131:80; server_name localhost; charset utf-8; access_log logs/host.access.log default_fmt_json; location / { root html/web2; index index.html index.htm; } } }
重啟nginx 測試 (先:killall nginx 在:../sbin/nginx )
2:基於埠的虛擬主機(只需要修改配置即可)
user centos; worker_processes 4; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format default_fmt_json '{"@timestamp":"$time_local",' '"client_ip":"$remote_addr",' '"request":"$request",' '"status":"$status",' '"bytes":"$body_bytes_sent",' '"x_forwarded":"$http_x_forwarded_for",' '"referer":"$http_referer"' '}'; sendfile on; keepalive_timeout 65; server { listen 80; #server_name localhost; charset utf-8; access_log logs/host.access.log default_fmt_json; location / { root html/web1; index index.html index.htm; } } server { listen 8080; #server_name localhost; charset utf-8; access_log logs/host.access.log default_fmt_json; location / { root html/web2; index index.html index.htm; } } }View Code
測試
3:基於域名的虛擬主機配置檔案
Nginx反向代理
代理伺服器,客戶在傳送請求的時候,不會直接傳送給目的主機,而是先發送給代理伺服器,代理伺服器接受客戶機請求之後,再向主機發出,並接收目的主機返回的資料,存放在代理伺服器硬碟中,在傳送給客戶機
應用場景:
堡壘機場景
內網伺服器釋出場景
快取場景
代理伺服器原理:
代理配置檔案
user centos; worker_processes 4; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format default_fmt_json '{"@timestamp":"$time_local",' '"client_ip":"$remote_addr",' '"request":"$request",' '"status":"$status",' '"bytes":"$body_bytes_sent",' '"x_forwarded":"$http_x_forwarded_for",' '"referer":"$http_referer"' '}'; sendfile on; keepalive_timeout 65; server { listen 80; #server_name localhost; charset utf-8; access_log logs/host.access.log default_fmt_json; location / { proxy_pass http://wendj.ticp.net; } } }View Code
Nginx限速
Nginx官方版本限制IP的連結和併發分別有兩個模組
limit_req_zone:用來限制單位時間內的請求數,即速率限制。
limit_req_conn:用來限制同一時間連線數,即併發限制。
Nginx中URL重寫
rewrite模組(ngx_http_rewrite_module)
Rewrite功能是Nginx伺服器提供的一個重要功能。幾乎所有的web產品必備技能,用於實現URL重寫。URL重寫是非常常用的功能,比如它可以在我們改變網站結構後,不需要客戶端修改原來的書籤,也不需要其它網站吸怪對我網站的友情連結,還可以在一定程度上提高網站安全性。
Nginx伺服器Rewrite功能是依賴於PCRE(PerlCompatibleRegularExpression)
Nginx優化
Nginx是主程序+工作程序模型
worker_processes 1:工作程序數量,按CPU的總核心調整
worker_cpu_affinity 0001 0100 1000:CPU的親和力
worker_connections 1024:一個工作程序的併發數
檢視CPU核數指令:cat /proc/cpuinfo |grep "flags"|wc -l
統計nginx連線數:netstat -antpl|grep nginx|grep ESTABLISHED|wc -l
Nginx長連線
http協議屬於TCP協議
優化目標:減少三次握手和四次揮手次數
keepalive_timeout 5:長連線時間
keepalive_requests 8192:每個長連線接受最大請求數
Nginx資料壓縮
gzip on; # 開啟壓縮
gzip_proxied any; # 任何時候都壓縮
gzip_min_length 1k; # 啟用壓縮最小檔案,小於設定的不會被壓縮
gzip_buffers 4 8k; # 壓縮的快取區大小
gzip_comp_level 6; # 壓縮級別1-9數字越大壓縮的越好,也越佔CPU時間
gzip_types text/plain text/css application/x-javascript application/javascript application/xml; # 壓縮檔案型別
Nginx客戶端快取
location ~* \.(png|gif|jpg|jpeg)${
expires 1h;
}