版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Zen99T/article/details/89048150
1、主配置文件:nginx.conf
使用homebrew安装后,配置文件路径:/usr/local/etc/nginx/nginx.conf
#user nobody;
worker_processes 1;
#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;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
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;
# }
#}
include servers/*;
}
1.1、nginx.conf文件结构
去掉注释后,剩下比较清晰的结构和内容:
# (全局块)全局生效
worker_processes 1;
# (events块)在events部分中生效
events {
worker_connections 1024;
}
# (http块)
http {
# 以下指令在http部分中生效(http全局块)
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 以下指令在http的server部分中生效(server块)
server {
# server全局块
listen 8080;
server_name localhost;
#以下指令在http/server的location部分中生效(location块)
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# include指令:配置文件的引入,支持相对路径
include servers/*;
}
- nginx.conf一共由三部分组成:全局块、events块和http块。
- http块中又包含多个子层级块。
- 在同一配置块中嵌套的配置块,各个之间不存在次序关系。
1.1.1、全局块
主要设置一些影响Nginx服务器整体运行的配置指令,指令作用域为Nginx服务器全局。通常包括:
- 配置运行Nginx服务器的用户(组)
- 允许生成的worker process数
- Nginx进程PID存放路径
- 日志的存放路径和类型
- 配置文件引入
1.1.2、events块
主要配置影响Nginx服务器与用户的网络连接。这部分指令对Nginx服务器性能影响较大,根据实际情况灵活调整。常用到的配置:
- 是否开启对多worker process下的网络连接进行序列化
- 是否允许同时接收多个网络连接
- 选取哪种事件驱动模型处理连接请求
- 每个worker process可以同时支持的最大连接数
1.1.3、http块
Nginx服务器配置中的重要部分,代理、缓存和日志定义等绝大多数的功能和第三方模块的配置都可以放在这个模块中。其下包含http全局块、server块。http全局块的指令包括:
- 文件引入
- MIME-Type定义
- 日志自定义
- 是否使用sendfile传输文件
- 连接超时时间
- 单连接请求数上限
1.1.4、server块
server块与"虚拟主机"的概念密切相关。
虚拟主机,即虚拟服务器、主机空间或网页空间。该技术为了节省互联网服务器硬件成本而出现,主要应用于HTTP、FTP及EMAIL等多项服务,将一台服务器的某项或者全部服务器内容逻辑划分为多个服务单元,对外表现为多个服务器,从而充分利用服务器硬件资源。从用户角度看,一台虚拟主机和一台独立的硬件主机是完全一样的。
使用Nginx服务器提供web服务时,利用虚拟主机的技术就可以避免为每一个要运行的网站提供单独的Nginx服务器,也无需为每个网站对应运行一组Nginx进程。虚拟主机技术使得Nginx服务器可以在同一台服务器上只运行一组Nginx进程,就可以运行多个网站。
一个http块可以包含多个server块,每个server块相当于一台虚拟主机。server块包含server全局块、location块。server全局块常见的两个配置:
- 本虚拟主机的监听配置
- 本虚拟主机的名称和IP配置
1.1.5、location块
一个server块可以包含多个location块,主要的作用是:
- 基于Nginx服务器接收到的请求字符串,对除虚拟主机名称之外的字符串进行匹配,对特定的请求进行处理
- 地址定向
- 数据缓存
- 应答控制
- 多数第三方模块的配置
2、参考:
- 《Nginx高性能Web服务器详解》苗泽/著
- Nginx配置文件简单说明
- Nginx配置文件(nginx.conf)配置详解