nginx目錄結構和配置檔案詳解
nginx目錄結構和配置檔案詳解
0x00 Nginx 目錄結構
Nginx 檔案結構比較簡潔,主要包括配置檔案和二進位制可執行程式,通過安裝包形式安裝的 nginx 檔案結構跟各 Linux 發行版目錄規則存放配置檔案和二進位制檔案的位置
目錄結構及檔案功能如下:
[root@localhost ~]# tree /usr/local/nginx
/usr/local/nginx
├── client_body_temp # POST 大檔案暫存目錄
├── conf # Nginx所有配置檔案的目錄
│ ├── fastcgi.conf # fastcgi相關引數的配置檔案
│ ├── fastcgi.conf.default # fastcgi.conf的原始備份檔案
│ ├── fastcgi_params # fastcgi的引數檔案
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types # 媒體型別
│ ├── mime.types.default
│ ├── nginx.conf #這是Nginx預設的主配置檔案,日常使用和修改的檔案
│ ├── nginx.conf.default
│ ├── scgi_params # scgi相關引數檔案
│ ├── scgi_params.default
│ ├── uwsgi_params # uwsgi相關引數檔案
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp # fastcgi臨時資料目錄
├── html # Nginx預設站點目錄
│ ├── 50x.html # 錯誤頁面優雅替代顯示檔案,例如出現502錯誤時會呼叫此頁面
│ └── index.html # 預設的首頁檔案
├── logs # Nginx日誌目錄
│ ├── access.log # 訪問日誌檔案
│ ├── error.log # 錯誤日誌檔案
│ └── nginx.pid # pid檔案,Nginx程序啟動後,會把所有程序的ID號寫到此檔案
├── proxy_temp # 臨時目錄
├── sbin # Nginx 可執行檔案目錄
│ └── nginx # Nginx 二進位制可執行程式
├── scgi_temp # 臨時目錄
└── uwsgi_temp # 臨時目錄
0x01 Nginx配置檔案
Nginx 主配置檔案/usr/local/nginx/conf/nginx.conf 是一個純文字型別的檔案,整個配置檔案是以區塊的形式組織,通常每一個區塊以一對大括號{}來表示開始與結束。提示:若編譯安裝則 nginx.conf 位於編譯時所指定目錄。
-
Main 位於 nginx.conf 配置檔案的最高層;
-
Main 層下可以有 Event、HTTP 層;
-
Http 層下面允許有多個 Server 層,用於對不同的網站做不同的配置;
-
Server 層下面允許有多個 Location,用於對不同的路徑進行不同模組的配置。
全域性配置部分用來配置對整個 server 都有效的引數。主要會設定一些影響 nginx 伺服器整體執行的配置指令,主要包括配置執行 Nginx 伺服器的使用者(組)
-
全域性Main配置
user nginx;
worker_processes 1; #設定值和CPU核心數一致
error_log /var/log/nginx/error.log warn; #日誌位置和日誌級別
pid /var/run/nginx.pid;
events 塊涉及的指令主要影響 Nginx 伺服器與使用者的網路連線,常用的設定包括是否開啟對多 worker process 下的網路連線進行序列化,是否允許同時接收多個網路連線,選取哪種事件驅動模型來處理連線請求,每個 worker process 可以同時支援的最大連線數等。
-
Event配置
events {
worker_connections 1024;
}
http 全域性塊配置的指令包括檔案引入、MIME-TYPE 定義、日誌自定義、連線超時時間、單鏈接請求數上限等。
http配置
http {
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
通常 Server 配置在獨立的/etc/nginx/conf.d/*.conf中,通過引用的方式呼叫。
Server 塊也被叫做“虛擬主機”部分,它描述的是一組根據不同 server_name 指令邏輯分割的資源,這些虛擬伺服器響應 HTTP 請求,因此都包含在 http 部分。最常見的配置是本虛擬機器主機的監聽配置和本虛擬主機的名稱或 IP 配置。一個 server 塊可以配置多個 location 塊。
-
server配置
server {
listen 80;
server_name localhost;
location / {
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;
}
}
-
Nginx全域性配置
user nginx; #程序使用者
worker_processes 1; #工作程序,配合和CPU個數保持一致
error_log /var/log/nginx/error.log warn; #錯誤日誌路徑及級別
pid /var/run/nginx.pid;#Nginx服務啟動的pid
-
Nginx events事件配置
events {
worker_connections 1024;#每個worker程序支援的最大連線數
use epoll;#核心模型,select、poll、epoll
}
-
Nginx公共配置
http {
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"';#日誌格式
access_log /var/log/nginx/access.log main;#訪問日誌
sendfile on;#優化靜態資源
#tcp_nopush on;#nginx不要快取資料,而是一段一段傳送
keepalive_timeout 65;#給客戶端分配連線超時時間,伺服器會在這個時間過後關閉連線。
#gzip on;#壓縮
-
Nginx server配置
Nginx必須使用虛擬機器配置站點,每個虛擬主機使用一個server。
server {
listen 80; #監聽埠,預設80
server_name localhost;#提供服務的域名或主機名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {#控制網站訪問路徑
root /usr/share/nginx/html; #存放網站的路徑
index index.html index.htm; #預設訪問的首頁
}
#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 {#錯誤程式碼重定向到新的location
root html;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {#server段配置
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {#server段配置
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;#SSL證書配置
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
-
Nginx網站配置
在預設虛擬機器 default.conf 基礎上新建虛擬機器。
[root@nginx ~]# vi /etc/nginx/conf.d/mysite.conf
server {
server_name www.cainiaojc.com;
location / {
root /usr/share/nginx/base;
index index.html;
}
}
*本文章僅供技術交流分享,請勿做未授權違法攻擊,雨筍教育不負任何責任。具體請參考《網路安全法》。