Nginx配置檔案解析

2021-10-09 13:13:17 字數 3042 閱讀 6272

忙裡偷閒,研究了一下nginx的配置檔案用法。nginx的安裝和在流****上的應用,前面已經寫過,這裡略過,有興趣的朋友可以參考之前的文章:

在ubuntu上安裝nginx with nginx-http-flv-module

nginx + nginx-http-flv-module + ffmpeg實現直播和點播

nginx預設的配置檔案為/usr/local/nginx/conf/nginx.conf,配置檔案主要包括全域性、event、http、server設定,其中,event用來定義nginx工作模式,http提供web功能,server用來設定虛擬主機。server必須位於http塊內部,乙個配置檔案中可以包含多個server。

nginx配置檔案的詳細解析如下:

#設定使用者與組

#user nobody

#啟動子程序數,建議數值為與cpu核心數相同

worker_processes 1

#錯誤日誌檔案,以及日誌級別,[ debug | info | notice | warn | error | crit ]

error_log logs/error.log info

#程序pid檔案

#設定錯誤**對應的錯誤頁面

#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

# proxy the php scripts to apache listening on 127.0.0.1:80

##該塊的意思是,若使用者訪問url以.php結尾,則自動將該請求轉交給127.0.0.1伺服器,

#通過proxy_pass可以實現**功能

#location ~ \.php$

# pass the php scripts to fastcgi server listening on 127.0.0.1:9000

##location ~ \.php$

# deny access to .htaccess files, if apache's document root

# concurs with nginx's one

##拒絕所有人訪問.ht頁面

#location ~ /\.ht }

# another virtual host using mix of ip-, name-, and port-based configuration

# 定義另乙個虛擬主機

接下來,我們就測試一下nginx的虛擬主機**。設定4個網頁,不同的位址請求分別返回不同的網頁給客戶端。由於沒有安裝dns,所以這裡只能以ip和埠來表示。

新建乙個用於測試的conf檔案,命名為test_nginx.conf,內容如下:

以上conf檔案中,實現了4個server,每個server通過相同不同埠來區分,每個server返回的html檔案不同,4個server的html檔案分別放在/usr/local/nginx/html/目錄下的first、second、third和default子目錄下。現在,我們先來建立這四個目錄:

sudo mkdir /usr/local/nginx/html/
建立成功後,為每個目錄新增可讀寫許可權:

sudo chmod 777
然後,向4個目錄中新增index.html及內容,簡單起見,分別新增如下內容:

echo "welcom to the first html!" > /usr/local/nginx/html/first/index.html

echo "welcom to the second html!" > /usr/local/nginx/html/second/index.html

echo "welcom to the third html!" > /usr/local/nginx/html/third/index.html

echo "welcom to the default html!" > /usr/local/nginx/html/default/index.html

執行完成後,確認相應目錄下的檔案被成功建立,接下來,我們就在自己的pc機上訪問以上幾個server,結果如下:

Nginx配置檔案解析

nginx配置檔案解析 nginx配置檔案預設在 usr local nginx conf nginx.conf,配置檔案包括 全域性 event http server設定 event主要設定nginx的工作模式,http提供web功能 server用來設定虛擬主機,server必須位於http內...

nginx配置檔案解析

nginx為微核心加可擴充套件模組。自身模組有core event http mail等核心模組。開發者可以基於這些核心模組開發滿足自身業務需求的模組,主要是http模組。nginx的配置項的資料結構 struct ngx cycle sconf ctx是乙個指標陣列,陣列一共有ngx max mo...

nginx配置檔案解析

nginx 監聽原理 先監聽埠 再配置網域名稱 匹配到就訪問local 否則 沒有匹配到網域名稱就預設訪問第乙個監聽埠的local位址 定義錯誤頁面,如果是500錯誤,則把站點根目錄下的50x.html返回給使用者 location 50x.html nginx 監聽原理 先監聽埠 再配置網域名稱 ...