1. 程式人生 > 實用技巧 >linux系統nginx配置檔案

linux系統nginx配置檔案

Markdown圖床**

圖床的好處:自己把makedown的文件裡面的照片

上傳到圖床裡,別人可以通過在圖床網站輸入圖片的連結地址找到相關圖片

兩個上傳圖片的網站

1.SM:網站IP

這個網站有限制,每天只能上傳30張圖片

支援批量上傳

2.路過圖床:網站IP

這個網站沒限制,同樣支援批量上傳

第二段視訊名詞和鬆耦合架構

剛開始曾老師展示了一段程式碼,通過修改程式碼中的部分內容展示了不同的頁面,複製了nginx的程式碼模擬nginx頁面展示在瀏覽器上告訴了nginx對運維的好處,結合工具方便修改。

名詞

1.pv:頁面獨立瀏覽量,網站的請求量

統計pv:cat /var/log/nginx/www.drizeng.com_access.log |wc -l

2.uv:獨立裝置,一個IP無論傳送多少請求只算一個uv

3.IP:獨立IP

注:pv不是網站真正訪問量,使用者在網站重新整理一次頁面或者點進去一個服務或網站模組就會產生一次新的pv。uv才是網站的真正訪問量,只要是相同的IP無論點開多少模組都只產生一個uv

模擬題

# 假設公司有一座大廈,大廈有100人,每個人有一臺電腦和一部手機,上網都是通過nat轉換出口,每個人點選網站2 次, 請問對應的pv,uv,ip分別是多少?(點選一次,傳送1000個請求)
PV:400000
UV:200
IP:1

SOA鬆耦合架構

京東是多個網站組合,拆分模組

解耦

#一個電商公司,他的網站頁面功能會有很多
註冊
登入
首頁
詳情頁
購物車
價格標籤
留言
客服
支付中心
物流
倉儲資訊
訂單相信
圖片

第三段視訊安裝nginx

剛開始講述了apache和nginx的先後產生的順序。先產生的是apache,nginx相對晚一些,然後介紹了apache和nginx使用的模型。

apache:select模型

nginx:epoll模型

兩種模型的區別:IP

nginx相對於apache的區別是:
輕量級,同樣起web 服務,比apache 佔用更少的記憶體及資源
抗併發,nginx 處理請求是非同步非阻塞的,而apache 則是阻塞型的,在高併發下nginx 能保持低資源低消耗高效能
高度模組化的設計,編寫模組相對簡單
社群活躍,各種高效能模組出品迅速
apache 相對於nginx 的優點:
rewrite ,比nginx 的rewrite 強大
模組超多,基本想到的都可以找到
少bug ,nginx 的bug 相對較多

在就是理由,一般來說,需要效能的web 服務,用nginx 。如果不需要效能只求穩定,那就apache
吧。後者的各種功能模組實現得比前者,例如ssl 的模組就比前者好,可配置項多。這裡要注意一點,epoll(freebsd 上是 kqueue
)網路IO 模型是nginx 處理效能高的根本理由,但並不是所有的情況下都是epoll

大獲全勝的,如果本身提供靜態服務的就只有寥寥幾個檔案,apache 的select 模型或許比epoll 更高效能。
當然,這只是根據網路IO 模型的原理作的一個假設,真正的應用還是需要實測了再說的。

**nginx應用場景

靜態伺服器**

以下服務沒有辦法直接連線資料庫

nginx

apache

IIS

lighttpd

tengine

openresty-nginx

只需要看,請求是否呼叫資料庫

不呼叫:靜態請求

動態伺服器

tomcat

resin

php

weblogic

jboss

物流

倉儲資訊

訂單相信

圖片呼叫:動態請求

:動態請求和靜態請求不能看是不是頁面在動,而是看是不是條用資料庫,不呼叫資料庫的是靜態請求,呼叫資料庫的是動態請求

nginx安裝

官方的IP

安裝過程:

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# cat /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
module_hotfixes=true

5.安裝nginx,如果系統的源有nginx,然後配置了官方源,下載時會優先從官方源下載

[root@web01 ~]# yum install -y nginx

安裝完之後的頁面

6.檢測nginx是否安裝成功

# 啟動並加入開機自啟
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

# 檢查埠
[root@web01 ~]# netstat -lntup|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8123/nginx: master
# 檢查埠
[root@web01 ~]# ps -ef|grep [n]ginx


nginx啟動方式**

檢查nginx版本:nginx -v

檢查nginx安裝的外掛:nginx -V(大寫)

檢測配置檔案是否有錯誤:nginx -t

systemctl方式:

啟動:systemctl start nginx

停止:systemctl stop nginx

重啟:systemctl restart nginx

不停止nginx重新載入配置檔案:systemctl reload nginx

nginx二進位制程式檔案管理方式:

啟動:nginx

還可以寫上絕對路徑/usr/sbin/nginx

停止:nginx -s stop

:兩種方式只能用一種,不能兩種方式摻著使用

編輯nginx的server檔案

# 進入nginx的配置檔案
[root@web01 ~]# cd /etc/nginx/conf.d
# 刪除原有的default.conf檔案
[root@web01 conf.d]# rm -rf default.conf
# 手動編輯配置檔案
[root@web01 conf.d]# vim wzh.conf
[root@web01 conf.d]# cat wzh.conf
server {
	listen 80;
	server_name www.wzh.com;
	root /code/wzh
        index index.html;
}
# 建立站點目錄
[root@web01 conf.d]# mkdir /code/wzh -p
# 編輯index.html檔案在站點目錄
[root@web01 conf.d]# cd /code/wzh
[root@web01 wzh]# echo 123 >index.html
# 域名解析
windows+r 開啟執行框
輸入drivers 然後找到etc點開裡面有hosts檔案
使用管理員身份開啟在最後輸入要解析的ip和網址
# 重新載入nginx
[root@web01 wzh]# systemctl reload nginx



nginx配置檔案詳解

############ 核心模組 ############ # 
nginx啟動使用者
user nginx;
# worker程序數
worker_processes auto;#auto自動獲取
# 錯誤日誌的路徑 和 級別 
error_log /var/log/nginx/error.log warn; 
# pid檔案的路徑
pid /var/run/nginx.pid; 
############ 事件驅動模組 ############
events { 
# 每一個worker程序允許連線數量
worker_connections 1024; }
############ HTTP模組 ############ 
http { # 包含指定檔案的內容,該檔案是nginx瀏覽器允許訪問的檔案型別
include /etc/nginx/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"';
log_format zidingyi 'zls - $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; access_log /var/log/nginx/zls_access.log zidingyi;
# 高效傳輸檔案
sendfile on; 
#tcp_nopush on; 
# 長連線的超時時間
keepalive_timeout 65; 
# 開啟gzip壓縮 #gzip on; 
# 包含所有下面路徑下conf結尾的檔案
include /etc/nginx/conf.d/*.conf; }

1.總結nginx配置檔案詳解
- 核心層
- 事件層
- http層
- server 層
- location

2.總結nginx的虛擬主機配置
- 基於IP
- 基於埠
- 基於域名

3.總結nginx的日誌管理
- 日誌設定(格式)
- 日誌呼叫
- 日誌路徑

核心層

#定義Nginx執行的使用者和使用者組
user nginx;

#nginx程序數,建議設定為等於CPU總核心數,幾核的CPU就把數字改成幾 。
worker_processes 1;
 
#全域性錯誤日誌定義型別,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log warn;

#程序pid檔案
pid /var/run/zls.pid

事件層

events { 
# 每一個worker程序允許連線數量
worker_connections 1024; }
events
{
    #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
    #是Linux 2.6以上版本核心中的高效能網路I/O模型,linux建議epoll,如果跑在FreeBSD上面,就用kqueue模型。
    #補充說明:
    #與apache相類,nginx針對不同的作業系統,有不同的事件模型
    #A)標準事件模型
    #Select、epoll屬於標準事件模型,如果當前系統不存在更有效的方法,nginx會選擇select或epoll
    #B)高效事件模型
    #Kqueue:使用於FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用雙處理器的MacOS X系統使用kqueue可能會造成核心崩潰。
    #Epoll:使用於Linux核心2.6版本及以後的系統。
    #/dev/poll:使用於Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
    #Eventport:使用於Solaris 10。 為了防止出現核心崩潰的問題, 有必要安裝安全補丁。
    use epoll;

    #單個程序最大連線數(最大連線數=連線數*程序數)
    #根據硬體調整,和前面工作程序配合起來用,儘量大,但是別把cpu跑到100%就行。每個程序允許的最多連線數,理論上每臺nginx伺服器的最大連線數為。
    worker_connections 65535;

    #keepalive超時時間。
    keepalive_timeout 60;

    #客戶端請求頭部的緩衝區大小。這個可以根據你的系統分頁大小來設定,一般一個請求頭的大小不會超過1k,不過由於一般系統分頁都要大於1k,所以這裡設定為分頁大小。
    #分頁大小可以用命令getconf PAGESIZE 取得。
    #[root@web001 ~]# getconf PAGESIZE
    #4096
    #但也有client_header_buffer_size超過4k的情況,但是client_header_buffer_size該值必須設定為“系統分頁大小”的整倍數。
    client_header_buffer_size 4k;

    #這個將為開啟檔案指定快取,預設是沒有啟用的,max指定快取數量,建議和開啟檔案數一致,inactive是指經過多長時間檔案沒被請求後刪除快取。
    open_file_cache max=65535 inactive=60s;

    #這個是指多長時間檢查一次快取的有效資訊。
    #語法:open_file_cache_valid time 預設值:open_file_cache_valid 60 使用欄位:http, server, location 這個指令指定了何時需要檢查open_file_cache中快取專案的有效資訊.
    open_file_cache_valid 80s;

    #open_file_cache指令中的inactive引數時間內檔案的最少使用次數,如果超過這個數字,檔案描述符一直是在快取中開啟的,如上例,如果有一個檔案在inactive時間內一次沒被使用,它將被移除。
    #語法:open_file_cache_min_uses number 預設值:open_file_cache_min_uses 1 使用欄位:http, server, location  這個指令指定了在open_file_cache指令無效的引數中一定的時間範圍內可以使用的最小檔案數,如果使用更大的值,檔案描述符在cache中總是開啟狀態.
    open_file_cache_min_uses 1;
    
    #語法:open_file_cache_errors on | off 預設值:open_file_cache_errors off 使用欄位:http, server, location 這個指令指定是否在搜尋一個檔案是記錄cache錯誤.
    open_file_cache_errors on;
}
 

HTTP層

http { # 包含指定檔案的內容,該檔案是nginx瀏覽器允許訪問的檔案型別
include /etc/nginx/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"';
log_format zidingyi 'zls - $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; access_log /var/log/nginx/zls_access.log zidingyi;
# 高效傳輸檔案
sendfile on; 
#tcp_nopush on; 
# 長連線的超時時間
keepalive_timeout 65; 
# 開啟gzip壓縮 #gzip on; 
# 包含所有下面路徑下conf結尾的檔案
include /etc/nginx/conf.d/*.conf;

server和location配置(虛擬主機配置)

server {
listen 80 default_server; server_name _; server_name 10.0.0.7; server_name www.test.com test.com; root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / { }error_page 404 /404.html;
location = /40x.html { }error_page 500 502 503 504 /50x.html;
location = /50x.html { } }
nginx 日誌
nginx日誌切割
redis
mysql
nginx
tomcat
...
很久很久以前,運維都需要寫日誌切割的指令碼
server { #監聽的埠
listen 80; #域名,IP server_name www.test.com; # 匹配/
location / { #站點目錄 root /opt/test; #html頁面
index index.html index.htm; } }
nginx的location指令配置語法如下:

語法:location [ = | ~ | ~* | ^~ ] uri { ... }
語境:server,location
1
2
3
說明:

=:表示精準匹配
~:表示正則匹配(區分大小寫)
~*:表示正則匹配(不區分大小寫)
^~:表示普通匹配完成後不使用正則匹配(可以看完下面匹配順序之後再來理解)
uri之前不包含=、~、~*、^~時為普通匹配
location指令可以巢狀,所以可以出現在server指令內部和location指令內部。


nginx為了找到匹配請求的location,搜尋匹配規則如下:

判斷是否精準匹配,如果匹配,直接返回結果並結束搜尋匹配過程。
判斷是否普通匹配,如果匹配,看是否包含^~字首,包含則返回,否則記錄匹配結果,(如果匹配到過個location時返回或記錄最長匹配的那個)
判斷是否正則匹配,按配置檔案裡的正則表示式的順序,由上到下開始匹配,一旦匹配成功,直接返回結果,並結束搜尋匹配過程。
如果正則匹配沒有匹配到結果,則返回步驟2記錄的匹配結果。
注:

多個普通匹配的location時,和location的順序無關,總是匹配所有的location,然後取匹配最長的location作為結果
多個正則匹配的location時,和順序有關,從上到下依次匹配,一旦匹配成功便結束,直接返回結果。
 server {
        #監聽80埠
        listen       80;
        #定義使用 訪問的網址
        server_name  www,wzh.com;
        #設定字元編碼
        #charset koi8-r;
        #設定本虛擬主機的訪問日誌
        access_log  logs/host.access.log  main;
        #預設請求,優先順序最低的配置
        location / {
            #定義伺服器的預設網站根目錄位置 
            root   /opt/wzh;
           
            #定義首頁索引檔案的名稱
            index  index.html index.htm;
        }
        
        #配置Nginx快取
        location ~.*\.(jpg|png|gif)$ {
          expires 30d; #快取存放30天,然後自動清除
        }
        location ~.*\.(css|js)? $ {
          expires 1h; #快取存放1小時
        }

        #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;
        }

linux虛擬主機配置講解

nginx下,一個server標籤就是一個虛擬主機。
1、基於域名的虛擬主機,通過域名來區分虛擬主機——應用:外部網站
2、基於埠的虛擬主機,通過埠來區分虛擬主機——應用:公司內部網站,外部網站的管理後臺
3、基於ip的虛擬主機,幾乎不用。

環境:**

開發環境

10臺 或 5臺

測試環境(10 臺 或 5臺)

效能測試(10臺)

功能測試(5臺)

預釋出環境(10臺)玩遊戲,不刪檔內測,刪檔內測,beta

生產環境(10臺)

基於域名(外部網站常用)

# 編輯配置檔案
[root@web01 ~]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim wzh.conf
server {
	listen 80;
	charset "utf-8";
	server_name www.wzh.com www.hahaha.com;
	location / {
	root /code/wzh;
        index index.html;
}
}
#檢測語法
[root@web01 conf.d]# nginx -t
#重新載入nginx
[root@web01 conf.d]# nginx -s reload
# 解析域名
windows+r鍵開啟執行框輸入drivers然後找到裡面的etc目錄開啟找到hosts檔案使用管理員身份開啟把ip和域名做個解析:10.0.0.7 www.wzh.com 空格www.hahaha.com
# 創建出配置檔案所需要的條件
[root@web01 conf.d]# mkdir /code/wzh -p
# 在這個目錄下建立index.html的檔案或者用echo xxx >index.html的方式
[root@web01 conf.d]# echo 這是王張行的nginx頁面 >/code/wzh/index.html
#開啟瀏覽器用www.wzh.com訪問

# 再echo一點內容用另一個域名訪問
[root@web01 wzh]# echo 這個是用www.hahaha的域名訪問的 >>index.html
# 再用www.hahaha.com訪問下

基於埠(內部測試用)

# 首先編輯兩個配置檔案以.conf結尾的
# 進入nginx的配置檔案目錄
[root@web01 ~]# cd /etc/nginx/conf.d/
#編輯檔案把埠整成81
[root@web01 conf.d]# vim www.www.conf 
[root@web01 conf.d]# cat www.www.conf 
server {
	listen 81;
	charset "utf-8";
	server_name 10.0.0.7;
	location / {
	root /opt/www;
	index index.html;
}
}
#再編輯一個埠不同ip相同的配置檔案
[root@web01 conf.d]# vim www.zzz.conf 
[root@web01 conf.d]# cat www.zzz.conf 
server {
        listen 85;
        charset "utf-8";
        server_name 10.0.0.7;
        location / {
        root /opt/zzz;
        index index.html;
}
}
# nginx檢查語法,沒毛病的話就重新載入一下
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# nginx -s reload
# 創建出需要的條件
[root@web01 conf.d]# mkdir /opt/{www,zzz}
# 在不同的站點目錄裡面寫上點東西
[root@web01 conf.d]# echo 這是81埠的 >/opt/www/index.html
[root@web01 conf.d]# echo 這是85埠的 >/opt/zzz/index.html
# 用瀏覽器訪問


基於IP(一般不用)

#當80埠被佔用的時候 兩種方式,一種新增網絡卡,另一種新增VIP
#新增網絡卡就不說了,說下VIP吧
# 首先新增設定兩個VIP
[root@web01 ~]# ifconfig eth0:0 10.0.0.60/24

[root@web01 ~]# ifconfig eth0:1 10.0.0.20/24
# ping一下看通不通
windows+r開啟執行框輸入cmd然後ping 10.0.0.20和10.0.0.60
# 編輯配置檔案
[root@web01 conf.d]# vim hhh.conf 
[root@web01 conf.d]# cat hhh.conf 
server {
	  listen 80;
        server_name 10.0.0.20 10.0.0.60;
        charset "utf-8";
        location / {
        root /opt/hhh;
        index index.html;	
}
}
# 檢查語法沒毛病就重新載入
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# nginx -s reload
# 創建出站點目錄並寫內容
[root@web01 conf.d]# mkdir /opt/hhh 
[root@web01 conf.d]# echo 123 >/opt/hhh/index.html
# 開啟瀏覽器訪問

# 再編輯點內容
[root@web01 conf.d]# echo 60 >/opt/hhh/index.html
# 再用60的IP訪問下