1. 程式人生 > >Nginx技術深度剖析(1)

Nginx技術深度剖析(1)

web服務器 nginx 深度剖析

(1)Nginx核心模塊:

Nginx核心模塊負責Nginx的全局應用,主要對應用主配置文件的Main區塊和Events區塊區域,這裏有很多Nginx必須的全局參數配置。

(2)標準的HTTP功能模塊集合:

這些模塊雖然不是必須的,但是是都很常用啊。因此會被Nginx自動編譯安裝到Nginx軟件中。不建議擅自改動。除非明確知道要幹什麽,由什麽額外的影響。

在生產環境中,配置調優主要就是根據這些模塊進行相應的更改來實現的。通過官方文檔可以查看。


企業中常用的Nginx http功能模塊:

--------------------------------------------------------------------------------------------

ngx_http_core_module | 包括一些核心的http參數配置,對應Nginx的配置為http區塊

--------------------------------------------------------------------------------------------

ngx_http_access_module | 訪問控制模塊,用來控制網站用戶對Nginx的訪問

--------------------------------------------------------------------------------------------

ngx_http_gzip_module | 壓縮模塊,對Nginx返回的數據壓縮,屬於性能優化模塊

--------------------------------------------------------------------------------------------

ngx_http_gzip_module | Fast CGI模塊,動態應用相關的模塊,例如PHP

--------------------------------------------------------------------------------------------

ngx_http_proxy_module | proxy代理模塊

--------------------------------------------------------------------------------------------

ngx_http_upstream_module | 負載均衡模塊,可以對實現網站的負載均衡功能及節點的健康檢查

--------------------------------------------------------------------------------------------

ngx_http_rewrite_module | URL地址重寫模塊

--------------------------------------------------------------------------------------------

ngx_http_limit_conn_module | 限制用戶並發連接數及請求模塊

--------------------------------------------------------------------------------------------

ngx_http_limit_req_module | 根據定義的key限制Nginx請求過程的速率

--------------------------------------------------------------------------------------------

ngx_http_log_module | 訪問日誌模塊,指定的格式記錄Nginx客戶訪問日誌等信息

--------------------------------------------------------------------------------------------

ngx_http_auth_basic_module | Web認證模塊,設置Web用戶通過賬號、密碼訪問Nginx

--------------------------------------------------------------------------------------------

ngx_http_ssl_module | SSL模塊,用於加密的http連接,例如https

--------------------------------------------------------------------------------------------

ngx_http_stub_status_module | 記錄Nginx基本訪問狀態信息等的模塊

--------------------------------------------------------------------------------------------


Nginx目錄結構說明:

.

├── client_body_temp

├── conf #Nginx默認所有配置文件目錄,極其重要

│ ├── fastcgi.conf #fastcgi相關參數的配置文件

│ ├── fastcgi.conf.default #fastcgi.conf文件的原始配置文件備份

│ ├── fastcgi_params #fastcgi的參數文件

│ ├── fastcgi_params.default #fastcgi的參數文件的備份文件

│ ├── koi-utf

│ ├── koi-win

│ ├── mime.types #媒體類型

│ ├── mime.types.default #媒體類型默認配置備份

│ ├── nginx.conf #Nginx的主配置文件

│ ├── nginx.conf.default #Nginx的主配置文件的默認配置文件

│ ├── scgi_params #scgi相關參數文件,一般用不到

│ ├── scgi_params.default #scgi相關參數文件的備份

│ ├── uwsgi_params #uwsgi相關參數文件,一般用不到

│ ├── uwsgi_params.default #uwsgi相關參數文件的備份文件

│ └── win-utf

├── fastcgi_temp #fastcgi臨時數據目錄

├── html #編譯安裝時Nginx的站點目錄,Apache默認是htdocs

│ ├── 50x.html #錯誤頁面,優雅的顯示錯誤

│ └── index.html #默認使用的首頁文件,一般會是這個,不是必須。

├── logs #Nginx默認的日誌路徑,包括錯誤日誌及訪問日誌

│ ├── access.log #Nginx的訪問日誌文件,使用tail -f access.log命令實時觀看網站用戶的訪問情況。

│ ├── error.log #Nginx的錯誤日誌文件,Nginx故障信息會報錯到該文件。

│ └── nginx.pid #Nginx的pid文件,Nginx進程啟動後,會把所有進程的ID號寫到此文件 。

├── proxy_temp #臨時目錄

├── sbin

│ └── nginx

├── scgi_temp #臨時目錄

└── uwsgi_temp #臨時目錄


Nginx.conf主配置文件:

/application/nginx/conf/nginx.conf:

Nginx的配置文件使用井號(#)註釋沒有用的配置語句。

整個文件都是以區塊的形式組織起來的;

Main區位於最上層,在Main區下面可以有Events區、HTTP區等層級;

在HTTP區中包含一個或多個server區,每個server區中又有一個或多個location區。

#user nobody;

worker_processes 1; #Nginx的work進程數量

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections 1024; #每個worker進程支持的最大連接數

}

http {

include mime.types; #Nginx支持的媒體類型庫文件

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 logs/access.log main;

sendfile on; #開啟高效傳輸模式

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65; #連接超時

#gzip on;

server {

listen 80; #提供服務的端口,默認是80

server_name localhost; #提供服務的主機域名

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root 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 {

root html; #指定站點的根目錄

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#location ~ \.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache‘s document root

# concurs with nginx‘s one

#

#location ~ /\.ht {

# deny all;

#}

}

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

#

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

#}

# HTTPS server

#

#server {

# listen 443 ssl;

# server_name localhost;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# 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的其他配置文件:

如果是配合動態服務(例如PHP服務),Nginx軟件還會用到擴展的FastCGI相關配置文件,這個配置是通過再nginx.conf主配置文件中嵌入include命令實現的,不過默認是註釋狀態。

/application/nginx/conf/fastcgi.conf:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REQUEST_SCHEME $scheme;

fastcgi_param HTTPS $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param REDIRECT_STATUS 200;


/application/nginx/conf/fastcgi_params:


fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REQUEST_SCHEME $scheme;

fastcgi_param HTTPS $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param REDIRECT_STATUS 200;

本文出自 “帥帥的小哥哥” 博客,請務必保留此出處http://xvjunjie.blog.51cto.com/12360960/1955109

Nginx技術深度剖析(1)