相信我们都会用到 nginx 的配置文件 nginx.conf,那么接下来记录一下 nginx.conf 的配置文件里的部分参数讲解。
本次以 nginx的版本 1.16.0 为例。
1、nginx的原始默认配置
#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 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;
# }
#}
}
2、全局配置
user:运行用户(全局配置),如果配置了那么需要访问的文件需要授权给这个用户权限,在windows平台上,nginx不支持指定用户。
worker_processes:启动的进程数,可与CPU的数量设置相同
error_log:错误日志
pid:PID文件
events:事件模型和连接数限制
worker_connections:每一个进程的最大连接数
补充一个默认配置没有看到的配置:
events {
use epoll;
}
use:事件模型,对于liunx系统,最常见的是epoll;
事件模型 | 值 | 描述 |
标准事件模型 | select 使用配置参数 –with-select_module 和 –without-select_module 来启用或禁用 | 当前系统不存在更有效的方法,nginx会选择select或者poll |
poll 使用配置参数 –with-poll_module 和 –without-poll_module 来启用或禁用 | ||
高效事件模型 | kqueue | 使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。 |
epoll | 使用于Linux内核2.6版本及以后的系统。 | |
/dev/poll | 使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+ | |
eventport | 使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装 这个 安全补丁 |
3、http 配置
3.1、http 全局配置
设置mime类型:
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"';
语法:log_format 格式名 格式样式,如果有多个,格式名称不能重复
$XXX 为内置参数,参考 后面的4、内置参数。
日志输出文件的存放路径、格式、缓存大小:
#access_log logs/access.log main;
日志格式和存放路径可参考:Module ngx_http_log_module
文件传输模式:
sendfile on;
设置为on表示启动高效传输文件的模式。sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个缓冲流,再把数据从磁盘读到缓存,再从缓存读取到用户空间的缓冲流中,再把数据从用户空间的缓冲流写入到内核的缓冲流中,最后到tcp socket。开启这个参数后可以让数据不用经过用户缓冲区域。
连接超时配置:
#keepalive_timeout 0;
keepalive_timeout 65;
开启gzip压缩:
#gzip on;
3.2、server 配置
3.2.1、server 全局配置
server:虚拟机的配置。
listen:代理端口号
server_name:匹配主机名,可以设置成服务器的ip
最终在浏览器上访问的是:server_name:listen
3.2.2、location 配置
location:nginx页面访问路径跳转的配置。
root:文件根目录,访问 ip:port 会访问 /html/index配置的文件。
index:根目录下访问的文件名。
location 路径匹配规则:
符号 | 描述 | 案例 |
= | 精确匹配 | location = /index{} 匹配:http:XXX/index |
^~ | uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意static/ /aa中间是空格) | location ^~ /static/ { } 匹配:http:XXX/static/** |
~ | 正则匹配,区分大小写 | location ~ /GuFang/ {} 匹配:http:XXX/GuFang/ |
~* | 正则匹配,不区分大小写 | location ~* /GuFang/ {} 匹配:http:XXX/GuFang/ http:XXX/gufang/ |
!~ | 正则匹配,区分大小写不匹配 | location !~ \.xhtm$ {} 不会匹配: http:XXX/*.xhtm |
!~* | 正则匹配,不区分大小写不匹配 | location !~* \.xhtm$ {} 不会匹配: http:XXX/*.xhtm http:XXX/*XHTM |
/ | 任何请求都会匹配 | location / {} 都可以匹配 |
@ | 定义一个location,在内部定向时使用,不参与路径匹配。列如:error_page 、 try_files | location /dem/ { error_page 404 @dem_err; } location @dem_err {} 匹配以http:XXX/dem/ 的请求,如果状态为404,则会转到 @dem_err 这个location 上 |
优先级:首先 =,其次^~, 然后是按文件中顺序的正则匹配,最后才是 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
3.2.3、【补充】rewrite 配置
rewrite:配置 url 重定向的关键指令,该功能需要PCRE软件的支持,通过正则表达式进行规则匹配。
语法:
rewrite 正则表达式 替代内容 flag标记;
正则表达式:
符号 | 描述 |
\ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$” |
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次 |
+ | 匹配前面的字符一次或多次 |
? | 匹配前面的字符零次或一次 |
. | 匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式。 |
(pattern) | 匹配括号内pattern并可以在后面获取对应的匹配,常用$0...$9属性获取小括号中的匹配内容。 |
替代内容:就是将正则匹配的内容替换成的内容。
flag标记:
符号 | 描述 |
last | 规则匹配完成后,继续向下匹配新的location URI规则 |
break | 规则匹配完成即终止,不再匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址会显示跳转后的URL地址 |
permanent | 返回301永久重定向,浏览器地址栏显示跳转后的URL地址 |
4、内置参数
参数 | 说明 | 案例 |
$remote_addr | 客户端地址,ip | 211.28.65.253 |
$remote_user | 远程客户端用户名称 | -- |
$time_local | 访问时间和时区 | 18 /Jul/2012 :17:00:01 +0800 |
$request | 请求的URI和HTTP协议 | "GET /article-10000.html HTTP/1.1" |
$http_host | 请求地址,即浏览器中你输入的地址(IP或域名) | www.baidu.com 192.168.100.100 |
$status | HTTP请求状态 | 200 |
$upstream_status | upstream状态 | 200 |
$body_bytes_sent | 发送给客户端文件内容大小 | 1547 |
$http_referer | url跳转来源 | https: //www .baidu.com/ |
$http_user_agent | 用户终端浏览器等信息 | "Mozilla /4 .0 (compatible; MSIE 8.0; Windows NT 5.1; Trident /4 .0; SV1; GTB7.0; .NET4.0C; |
$ssl_protocol | SSL协议版本 | TLSv1 |
$ssl_cipher | 交换数据中的算法 | RC4-SHA |
$upstream_addr | 后台upstream的地址,即真正提供服务的主机地址 | 10.10.10.100:80 |
$request_time | 整个请求的总时间 | 0.205 |
$upstream_response_time | 请求过程中,upstream响应时间 | 0.002 |
$http_x_forwarded_for | 客户端通过代理连接web服务器的情况下,客户端的真实ip地址 |
nginx的内置参数很多,有需要的可以去了解了解,这里只举例了几个常用的。
5、命令
检查nginx配置文件是否正确:
./nginx -t
这里只记录了部分nginx默认配置的讲解
这里有两个可供在liunx系统上和windows系统上使用的nginx配置文件,可下载使用。
https://download.csdn.net/download/qq_38035876/89331077
希望对您学习nginx配置有所帮助!