您的位置:首页 > 运维架构 > Nginx

Nginx技术深度剖析(1)

2017-08-10 13:28 423 查看
(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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息