nginx的工作原理及配置
nginx的工作原理及配置
nginx的模塊與工作原理
nginx由內核和模塊組成。其中,內核的設計非常微小和簡潔,完成的工作也非常簡單,僅僅通過查找配置文件將客戶端請求映射到一個location block(location是nginx配置中的一個指令,用于URL匹配),而在這個location中所配置的每個指令將會啟動不同的模塊去完成相應的工作。
nginx的模塊分類
nginx的模塊從結構上分為核心模塊、基礎模塊和第三方模塊
- HTTP模塊、EVENT模塊和MAIL模塊等屬于核心模塊
- HTTP Access模塊、HTTP FastCGI模塊、HTTP Proxy模塊和HTTP Rewrite模塊屬于基本模塊
- HTTP Upstream模塊、Request Hash模塊、Notice模塊和HTTP Access Key模塊屬于第三方模塊
用戶根據自己的需要開發的模塊都屬于第三方模塊。正是有了如此多模塊的支撐,nginx的功能才會如此強大
nginx模塊從功能上可以分成三類
- Handlers(處理器模塊)。此類模塊直接處理請求,并進行輸出內容和修改headers信息等操作。handlers處理器模塊一般只能有一個
- Filters(過濾器模塊)。此類模塊主要對其他處理器模塊輸出的內容進行修改操作,最后由nginx輸出
- Proxies(代理器模塊)。就是nginx的HTTP Upstream之類的模塊,這些模塊主要與后端一些服務比如fastcgi等操作交互,實現服務代理和負載均衡等功能
nginx模塊
| 事件模塊 | 郵件模塊 | 等等等等 |
| 標準http模塊 | 第三方模塊 |
- nginx基本模塊:所謂基本模塊,指的是nginx默認的功能模塊,它們提供的指令,允許你使用定義nginx基本功能的變量,在編譯時不能被禁用,包括:
- 核心模塊:基本功能和指令,如進程管理和安全。常見的核心模塊指令,大部分是放置在配置文件的頂部
- 事件模塊:在Nginx內配置網絡使用的能力。常見的events(事件)模塊指令,大部分是放置在配置文件的頂部
- 配置模塊:提供包含機制
nginx的工作原理
nginx的模塊直接被編譯進nginx,因此屬于靜態編譯方式。
啟動nginx后,nginx的模塊被自動加載,與Apache不一樣,首先將模塊編譯為一個so文件,然后在配置文件中指定是否進行加載。
在解析配置文件時,nginx的每個模塊都有可能去處理某個請求,但是同一個處理請求只能由一個模塊來完成。
nginx的進程架構:
啟動nginx時,會啟動一個Master進程,這個進程不處理任何客戶端的請求,主要用來產生worker線程,一個worker線程用來處理n個request。
nginx模塊一次常規的HTTP請求和響應的過程
nginx的安裝與配置
nginx的安裝
創建系統用戶nginx [root@localhost ~]# useradd -r -M -s /sbin/nologin nginx安裝依賴環境 [root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ [root@localhost ~]# yum -y groups mark install 'Development Tools'創建日志存放目錄 [root@localhost ~]# mkdir -p /var/log/nginx [root@localhost ~]# chown -R nginx.nginx /var/log/nginx下載nginx編譯安裝 [root@localhost ~]# cd /usr/src/ [root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz --2021-10-26 11:20:41-- http://nginx.org/download/nginx-1.12.0.tar.gz 正在解析主機 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ... 正在連接 nginx.org (nginx.org)|3.125.197.172|:80... 已連接。 已發出 HTTP 請求,正在等待回應... 200 OK 長度:980831 (958K) [application/octet-stream] 正在保存至: “nginx-1.12.0.tar.gz”100%[===================================>] 980,831 639KB/s 用時 1.5s 2021-10-26 11:20:43 (639 KB/s) - 已保存 “nginx-1.12.0.tar.gz” [980831/980831])[root@localhost src]# tar xf nginx-1.12.0.tar.gz [root@localhost src]# cd nginx-1.12.0 [root@localhost nginx-1.12.0]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-debug \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_image_filter_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --http-log-path=/var/log/nginx/access.log \ --error-log-path=/var/log/nginx/error.log[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make installnginx配置
配置環境變量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh [root@localhost ~]# . /etc/profile.d/nginx.sh服務控制使用nginx命令
-t 檢查配置文件語法
[root@localhost ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful-v 輸出nginx的版本
[root@localhost ~]# nginx -v nginx version: nginx/1.12.0-c 指定配置文件的路徑
[root@localhost ~]# nginx -s stop ; nginx -c /opt/nginx.conf ##直接停掉然后啟動 [root@localhost ~]# ps -ef|grep nginx root 99290 1 0 03:32 ? 00:00:00 nginx: master process nginx -c /opt/nginx.conf nginx 99291 99290 0 03:32 ? 00:00:00 nginx: worker process nginx 99292 99290 0 03:32 ? 00:00:00 nginx: worker process root 101138 1653 0 03:33 pts/0 00:00:00 grep --color=auto nginx-s 發送服務控制信號,可選值有{stop|quit|reopen|reload}
[root@localhost ~]# nginx -s quit [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*nginx配置文件詳解
主配置文件:/usr/local/nginx/conf
- 默認啟動nginx時,使用的配置文件是:安裝路徑/conf/nginx.conf文件
- 可以在啟動nginx時通過-c選項來指定要讀取的配置文件
nginx常見的配置文件及其作用
| nginx.conf | nginx的基本配置文件 |
| mime.types | MIME類型關聯的擴展文件 |
| fastcgi.conf | 與fastcgi相關的配置 |
| proxy.conf | 與proxy相關的配置 |
| sites.conf | 配置nginx提供的網站,包括虛擬主機 |
nginx.conf配置詳解
nginx.conf的內容分為以下幾段:
- main配置段:全局配置段。其中main配置段中可能包含event配置段
- event {}:定義event模型工作特性
- http {}:定義http協議相關的配置
nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf # 全局塊user www-data; ##用戶worker_processes 2; ## 默認1,一般建議設成CPU核數1-2倍error_log logs/error.log; ## 錯誤日志路徑pid logs/nginx.pid; ## 進程id# Events塊events {# 使用epoll的I/O 模型處理輪詢事件。# 可以不設置,nginx會根據操作系統選擇合適的模型use epoll;# 工作進程的最大連接數量, 默認1024個worker_connections 2048;# http層面的keep-alive超時時間keepalive_timeout 60;# 客戶端請求頭部的緩沖區大小client_header_buffer_size 2k;}# http塊http { include mime.types; # 導入文件擴展名與文件類型映射表default_type application/octet-stream; # 默認文件類型# 日志格式及access日志路徑log_format main '$remote_addr - $remote_user [$time_local] $status ''"$request" $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main; # 允許sendfile方式傳輸文件,默認為off。sendfile on;tcp_nopush on; # sendfile開啟時才開啟。# http server塊# 簡單反向代理server {listen 80;server_name domain2.com www.domain2.com;access_log logs/domain2.access.log main;# 轉發動態請求到web應用服務器location / {proxy_pass http://127.0.0.1:8000;deny 192.24.40.8; # 拒絕的ipallow 192.24.40.6; # 允許的ip }# 錯誤頁面error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}# 負載均衡upstream backend_server {server 192.168.0.1:8000 weight=5; # weight越高,權重越大server 192.168.0.2:8000 weight=1;server 192.168.0.3:8000;server 192.168.0.4:8001 backup; # 熱備}server {listen 80;server_name big.server.com;access_log logs/big.server.access.log main;charset utf-8;client_max_body_size 10M; # 限制用戶上傳文件大小,默認1Mlocation / {# 使用proxy_pass轉發請求到通過upstream定義的一組應用服務器proxy_pass http://backend_server;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Host $http_host;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;} }}配置指令:要以分號結尾,語法格式如下:
derective value1 [value2 ...];支持使用變量:
- 內置變量:模塊會提供內建變量定義
- 自定義變量:set var_name value
用于調試、定位問題的配置參數
daemon {on|off}; //是否以守護進程方式運行nginx,調試時應設置為off master_process {on|off}; //是否以master/worker模型來運行nginx,調試時可以設置為off error_log 位置 級別; //配置錯誤日志| flile | debug:若要使用debug級別,需要在編譯nginx時使用–with-debug選項 |
| stder | info notice warn error crit alert emerg |
| syslog:server=address[,parameter=value] | |
| memory:size |
正常運行必備的參數
user USERNAME [GROUPNAME]; //指定運行worker進程的用戶和組 pid /path/to/pid_file; //指定nginx守護進程的pid文件 worker_rlimit_nofile number; //設置所有worker進程最大可以打開的文件數,默認為1024 worker_rlimit_core size; //指明所有worker進程所能夠使用的總體的最大核心文件大小,保持默認即可優化性能的配置參數
worker_processes n; //啟動n個worker進程,這里的n為了避免上下文切換,通常設置為cpu總核心數-1或等于總核心數 worker_cpu_affinity cpumask ...; //將進程綁定到某cpu中,避免頻繁刷新緩存 //cpumask:使用8位二進制表示cpu核心,如:0000 0001 //第一顆cpu核心0000 0010 //第二顆cpu核心0000 0100 //第三顆cpu核心0000 1000 //第四顆cpu核心0001 0000 //第五顆cpu核心0010 0000 //第六顆cpu核心0100 0000 //第七顆cpu核心1000 0000 //第八顆cpu核心 timer_resolution interval; //計時器解析度。降低此值,可減少gettimeofday()系統調用的次數 worker_priority number; //指明worker進程的nice值事件相關的配置: event{}段中的配置參數
accept_mutex {off|on}; //master調度用戶請求至各worker進程時使用的負載均衡鎖;on表示能讓多個worker輪流地、序列化地去響應新請求 lock_file file; //accept_mutex用到的互斥鎖鎖文件路徑 use [epoll | rtsig | select | poll]; //指明使用的事件模型,建議讓nginx自行選擇 worker_connections #; //每個進程能夠接受的最大連接數網絡連接相關的配置參數
keepalive_timeout number; //長連接的超時時長,默認為65s keepalive_requests number; //在一個長連接上所能夠允許請求的最大資源數 keepalive_disable [msie6|safari|none]; //為指定類型的UserAgent禁用長連接 tcp_nodelay on|off; //是否對長連接使用TCP_NODELAY選項,為了提升用戶體驗,通常設為on client_header_timeout number; //讀取http請求報文首部的超時時長 client_body_timeout number; //讀取http請求報文body部分的超時時長 send_timeout number; //發送響應報文的超時時長總結
以上是生活随笔為你收集整理的nginx的工作原理及配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sparse Feature Learn
- 下一篇: Django:快速搭建简单的Blog