更新nginx官方yum源html
vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key
yum安裝nginx軟件nginx
yum -y install nginx
啓動nginx服務,並設置開機自啓,打開瀏覽器檢查nginx是否安裝成功web
systemctl start nginx systemctl enable nginx
利用腳本實現切割 (定時任務)算法
#!/bin/bash mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%F).log systemctl restart nginx #重啓nginx是爲了讓其從新產生access.log文件
利用專用的切割程序-logrotateshell
vim /etc/logrotate.conf # rotate log files weekly weekly #--- 定義默認日誌切割的週期 # keep 4 weeks worth of backlogs rotate 4 #--- 定義只保留幾個切割後的文件 # create new (empty) log files after rotating old ones create #--- 建立出一個相同的源文件 # use date as a suffix of the rotated file dateext #--- 定義角標(擴展名稱信息) # uncomment this if you want your log files compressed #compress #--- 是否對切割後的文件進行壓縮處理 # RPM packages drop log rotation information into this directory include /etc/logrotate.d #--- 加載包含/etc/logrotate.d/目錄中文件配置 # no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { #--- 單獨對某個文件進行切割配置 monthly create 0664 root utmp minsize 1M #--- 最小大小爲1M,小於1M不進行切割 rotate 1 }
vim /etc/nginx/nginx.conf #--- 主配置文件 #第一個部分: 配置文件主區域配置 user www; #--- 定義worker進程管理的用戶 (此時改成www 因此要確保系統上有此用戶) 補充: nginx的進程 master process: 主進程 #---管理服務是否可以正常運行 boss worker process: 工做進程 #---處理用戶的訪問請求 員工 worker_processes 2; #---定義有幾個worker進程 == CPU核數 / 核數的2倍 error_log /var/log/nginx/error.log warn; #--- 定義錯誤日誌路徑信息 pid /var/run/nginx.pid; #--- 定義pid文件路徑信息 #第二個部分: 配置文件事件區域 events { worker_connections 1024; #--- 一個worker進程能夠同時接收1024訪問請求 } #第三個部分: 配置http區域 http { include /etc/nginx/mime.types; #--- 加載一個配置文件 default_type application/octet-stream; #--- 指定默認識別文件類型 log_format jiage '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #--- 定義日誌的格式 access_log /var/log/nginx/access.log jiage;# 這裏的jiage 引用上面jiage 的日誌格式 #--- 指定日誌路徑 sendfile on; ??? #tcp_nopush on; ??? keepalive_timeout 65; #--- 超時時間 #gzip on; include /etc/nginx/conf.d/*.conf; #--- 加載一個配置文件 } cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak grep -Ev '^$|#' /etc/nginx/conf.d/default.conf.bak > /etc/nginx/conf.d/default.conf vim /etc/nginx/nginx.d/default #--- 擴展配置(虛擬主機配置文件) #第四個部分: server區域信息(配置一個網站 www/bbs/blog -- 一個虛擬主機) server { listen 80; #--- 指定監聽的端口 server_name www.jiage.com; #--- 指定網站域名 root /usr/share/nginx/html; #--- 定義站點目錄的位置 index index.html index.htm; #--- 定義首頁文件 error_page 500 502 503 504 /50x.html; #--- 優雅顯示頁面信息 location = /50x.html { root /usr/share/nginx/html; } }
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 /var/log/nginx/access.log main; 調用日誌格式 $remote_addr 顯示用戶訪問源IP地址信息 $remote_user 顯示認證的用戶名信息 [$time_local] 顯示訪問網站時間 "$request" 請求報文的請求行信息 $status 用戶訪問網站狀態碼信息 $body_bytes_sent 顯示響應的數據尺寸信息 $http_referer 記錄調用網站資源的鏈接地址信息(防止用戶盜鏈) 老男孩nginx---access.log---莫文傑(荒原飲露---老男孩圖片連接)---http_referer(連接) $http_user_agent 記錄用戶使用什麼客戶端軟件進行訪問頁面的 (谷歌 火狐 IE 安卓 iphone) $http_x_forwarded_for 負載均衡相關,記錄真實客戶端的ip地址
錯誤日誌: /var/log/nginx/error.log --- Core functionality Syntax: error_log file [level]; 指定錯誤日誌路徑以及錯誤日誌記錄的級別 Default: error_log logs/error.log error; Context: main, http, mail, stream, server, location error_log /var/log/nginx/error.log warn; 錯誤級別: debug :調試級別, 服務運行的狀態信息和錯誤信息詳細顯示 信息越多 info :信息級別, 只顯示重要的運行信息和錯誤信息 notice :通知級別: 更加劇要的信息進行通知說明 warn :警告級別: 可能出現了一些錯誤信息,但不影響服務運行 error :錯誤級別: 服務運行已經出現了錯誤,須要進行糾正 推薦選擇 crit :嚴重級別: 必須進行修改調整 alert :嚴重警告級別: 即警告,並且必須進行錯誤修改 emerg :災難級別: 服務已經不能正常運行 信息越少 PS: 日誌文件信息須要作切割處理防止容量過大分析麻煩
location進行uri匹配apache
vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location / { root /html/www; index jiage.html; error_page 404 /404.jpg } }
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: — Context: server, location location = / { --- 精確匹配 優先級01 最高 [ configuration A ] } location / { --- 默認匹配 優先級04 最低 [ configuration B ] } location /documents/ { --- 按照目錄進行匹配 優先級03 [ configuration C ] } location ^~ /images/ { --- 優先匹配/不識別uri信息中符號信息 優先級02 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { --- 不區分大小寫進行匹配 優先級03 [ configuration E ] }
例子:vim
server{ listen 80; server_name test.location.com; location = / { return 404; } location / { return 403; } location /documents/ { return 500; } location ^~ /images/ { return 502; } location ~* \.(gif|jpg|jpeg)$ { return 504; } }
vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location /jiage { root /usr/share/nginx/html; index jiage.html; } }
編寫主頁文件以html結尾centos
vim /usr/share/nginx/html/jiage.html <!DOCTYPE html> <html> <meta charset="utf-8"> <head> <title>這個是標題</title> </head> <body> <h1>這是一個一個簡單的HTML,h1 <p>Hello </p> </h1> </h5> </body> </html>
重啓nginx服務(平滑重啓)瀏覽器
mkdir -p /usr/share/nginx/html/jiage/jiage.html systemctl reload nginx
nginx 命令參數緩存
-t : test configuration and exit
檢查測試配置文件語法
-s : send signal to a master process: stop, quit, reopen, reload
控制服務中止或者從新啓動
編寫DNS配置信息 /etc/hosts
進行訪問測試
瀏覽器中:http://www.jiage.com/jiage
文本界面: curl www.jiage.com/jiage
部署搭建網站常見錯誤:
網站服務配置文件編寫不正確
404 錯誤
解決方法一: 修改nginx配置文件---location
解決方法二: 在站點目錄中建立相應目錄或文件數據信息
403 錯誤
解決方法一: 不要禁止訪問
解決方法二: 由於沒有首頁文件
DNS信息配置不正確
[root@web02 conf.d]# vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location / { root /html/www; index index.html; } } [root@web02 conf.d]# vim /etc/nginx/conf.d/bbs.conf server { listen 80; server_name bbs.jiage.com; location / { root /html/bbs; index index.html; } } [root@web02 conf.d]# vim /etc/nginx/conf.d/blog.conf server { listen 80; server_name blog.jiage.com; location / { root /htmlblog; index index.html; } } [root@web02 conf.d]#systemctl reload nginx
[root@web02 conf.d]# mkdir -p /html/{www,bbs,blog} [root@web02 conf.d]# for name in {www,bbs,blog}; do echo "10.0.0.8 $name.jiage.com" > /html/$name/index.html;done [root@web02 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html ;done 10.0.0.8 www.jiage.com 10.0.0.8 bbs.jiage.com 10.0.0.8 blog.jiage.com
在客戶端上編寫hosts解析文件
10.0.0.8 www.jiage.com bbs.jiage.com blog.jiage.com
進行訪問測試
注意:利用瀏覽器訪問是要清除緩存,或者使用無痕模式訪問
ps:服務配置文件中涉及到地址修改,必須重啓nginx服務,不能平滑重啓
例: 10.0.0.0/24 www.jiage.com/AV 不能訪問
172.16.1.0/24 www.jiage.com/AV 能夠訪問
nginx訪問模塊: ngx_http_access_module 舉例配置: location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all; } 指令用法 Syntax: deny address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except
[root@web02 conf.d]# vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location / { root /html/www; index index.html; } location /AV { deny 10.0.0.0/24; allow 172.16.1.0/24; root /html/www; index index.html; } }
ps: location 外面的信息,全局配置信息
location裏面的信息,局部配置信息
nginx認證模塊: ngx_http_auth_basic_module 舉例配置: location / { auth_basic "closed site"; --- 開啓認證功能 auth_basic_user_file conf/htpasswd; --- 加載用戶密碼文件 }
server { listen 80; server_name www.jiage.com; location / { root /html/www; index index.html; auth_basic "jiage"; #密碼忘了以後的提醒信息 auth_basic_user_file password/htpasswd; }
建立密碼文件(文本中密碼信息必須是密文的)
使用命令: htpasswd 建立一個有密文信息的密碼文件 該命令系統默認是沒有安裝的
[root@web02 conf.d]# provides htpaswd httpd-tools-2.4.6-89.el7.centos.x86_64 #能夠看到htpasswd的安裝包爲這個 htpasswd命令參數說明: -c Create a new file. ***** 建立一個密碼文件 -n Don't update file; display results on stdout. 不會更新文件; 顯示文件內容信息 -b Use the password from the command line rather than prompting for it. ***** 免交互方式輸入用戶密碼信息 -i Read password from stdin without verification (for script usage). 讀取密碼採用標準輸入方式,並不作檢查 ??? -m Force MD5 encryption of the password (default). md5的加密算法 -B Force bcrypt encryption of the password (very secure). 使用bcrypt對密碼進行加密 -C Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 31). 使用bcrypt algorithm對密碼進行加密 -d Force CRYPT encryption of the password (8 chars max, insecure). 密碼加密方式 -s Force SHA encryption of the password (insecure). 加密方式 -p Do not encrypt the password (plaintext, insecure). 不進行加密 -D Delete the specified user. 刪除指定用戶 -v Verify password for the specified user.
[root@web02 conf.d]# htpasswd -bc /html/www/password/htpasswd jiage 123456 [root@web02 conf.d]# chmod 600/html/www/password/htpasswd ps:根據配置文件建立加密文件 用戶名: jiage; 密碼:123456
[root@web02 password]# curl www.jiage.com -u jiage:123456 10.0.0.8 www.jiage.com
nginx模塊功能: ngx_http_autoindex_module Syntax: autoindex on | off; Default: autoindex off; Context: http, server, location server { listen 80; server_name www.jiage.com; location / { root /html/www; #index index.html; auth_basic "jiage"; #密碼忘了以後的提醒信息 auth_basic_user_file password/htpasswd; autoindex on; #開啓nginx站點索引功能 charset utf-8; # 修改目錄結構中出現的中文亂碼問題 } ps:文件共享服務器不須要有主頁文件,或者修改配置文件中主頁文件的名字與站點目錄下的不一致。
例子:訪問www.jiage.com 直接輸入jia.com 就直接訪問大www服務器
編寫配置文件
server_name www.jiage.com jia.com;
做用:
1. 編寫網站訪問測試
狀態模塊: ngx_http_stub_status_module location = /basic_status { stub_status; }
編寫配置文件
[root@web01 conf.d]# vim state.conf server { listen 80; server_name state.jiage.com stub_status; }
重啓nginx服務,並在客戶端寫好域名解析
ps: 頁面顯示信息介紹
Active connections: 激活的鏈接數信息 4000用戶 3500 accepts: 接收的鏈接數彙總(綜合) TCP handled: 處理的鏈接數彙總(綜合) TCP requests: 總計的請求數量 HTTP協議請求 Reading: nginx服務讀取請求報文的數量 100人點餐 Writing: nginx服務響應報文信息數量 100人響應 Waiting: nginx隊列機制,要處理(讀取或者響應保存進行保存) 監控
利用rewrite模塊實現跳轉功能: http_rewrite_module
Syntax: rewrite regex replacement [flag]; rewite 匹配的正則信息 替換成什麼信息
Default: —
Context: server, location, if
出現無限跳轉如何解決:
第一種方法: 利用不一樣server區塊配置打破循環
server{ server_name jiage.com; rewrite ^/(.*) http://www.jiage.com/$1 permanent; }
第二種方法: 利用if判斷實現打破循環
if ($host ~* "^jiage.com$"){ rewrite ^/(.*) http://www.jiage.com/$1 permanent; }
例子: www.jiage.com/abc/index.html ^/ 表示的是www.jia.com (.*) 表示的是abc/index.html $host 表示的是用戶訪問的URL信息