JAVA后端架构Nginx面试题

Nginx基本属性配置详解

2019-10-31  本文已影响0人  AI乔治

1. Nginx服务的基本配置

1.1 用于调试进程和定位问题的配置项
# 默认on
daemon on|off;
# 默认on,指定了是否以master-worker进程的方式运行,如果设置为off,那么所有的请求将只会由master进程处理
master_process on|off;
# 指定了error日志的目录和日志级别,第二个参数用于指定目录,第三个参数用于指定日志级别,总共有:debug、info、notice、warn、error、crit、alert、emerg,这些日志级别中,从左往右优先级依次增大,默认为info
error_log logs/error.log error;
# 指定了调试点
debug_points stop|abort
debug_connection IP|CIDR

该参数主要用于events模块中,针对指定的ip或者网段记录debug日志:

events {
  debug_connection 10.224.66.14;
  debug_connection 10.224.57.0/24;
}

需要注意的是,在使用该参数时,必须要确保在进行configure时已经加入了--with-debug参数,否则不会生效;

worker_rlimit_core size;

在Linux操作系统中,如果一个进程由于错误或者收到信号而终止时,会将进程执行时的内存内容写入一个文件(core文件),以作为调试之用,这就是所谓的核心转储。在nginx进程宕机时,其就会产生核心转储文件,而且该文件一般都有几个G,因而如果不限制该文件的大小,那么很有可能会把服务器磁盘占满。该参数的作用就是限制核心转储文件的大小的。

working_directory path;

该参数指定了在生成核心转储文件时,将该文件存放的目录。

1.2 正常运行的配置项
env TESTPATH=/tmp/

这个配置项可以让用户直接设置操作系统上的环境变量。

include /path/file

用于将其他的配置文件引入进来,该路径可以是绝对路径,也可以是相对路径,如果是相对路径,则是基于nginx的配置目录而指定的。

pid path/file

用于指定存储nginx的master进程运行所使用的进程id的文件的路径。

user username [groupName]

用于指定worker进程运行时所基于的用户和用户组,默认都为nobody,这里如果不指定groupName,那么组名就与用户名一致。

worker_rlimit_nofile limit;

设置一个worker进程能够打开的最大句柄描述符个数。

worker_rlimit_sigpending limit;

设置了每个用户能够发往nginx的信号队列的大小,如果信号队列已满,那么新发送的信号将会被丢弃。

1.3 优化性能的配置项
worker_processes 1;

用于指定nginx运行时worker进程的个数,在nginx运行时,每个worker进程都是单线程运行的,这里需要判断worker进程是否进行了阻塞性操作,如果有这样的操作,那么稍微多配置一些worker进程比较好,如果没有,那么将worker进程数量设置得与CPU数量一样能够得到更好的性能。

worker_cpu_affinity cpumask [cpumask...]

将worker进程与指定的CPU进行绑定,这样能够防止多个worker进程抢占同一个CPU,从而避免出现同步问题。如下是一个4核CPU的配置方式:

worker_processes 4;
worker_cpu_affinity 1000 0100 0010 0001;

需要注意的是,worker_cpu_affinity仅对于Linux系统有效。

ssl_engine device;

如果服务器上有SSL硬件加速设备,那么就可以进行配置以加快SSL协议的处理速度。用户可以使用OpenSSL提供的命令来查看是否有SSL硬件加速设备:

openssl engine -t
timer_resolution -t

默认情况下,每次内核的事件调用返回时,都会执行一次gettimeofday,在早期的Linux版本中,获取系统时间都会有一次从内核态到用户态的数据复制,其代价比较高,但是在最新的x86-64体系架构中,gettimeofday仅仅只是一次vsyscall,其仅仅只是对共享内存页中的数据的访问,代价不大。

worker_priority nice;

用于设置nginx的worker进程的优先级,其中,nice的默认值为0。在Linux操作系统中,当有多个进程在竞争CPU执行资源时,其就会根据每个进程设置的优先级来优先分配执行权限,并且所分配的时间片也要高一些。优先级的值在-20~+19之间,数值越低优先级越高,一般建议将nginx的优先级设置得更低一些,这样才能保证其执行的权限,但是建议不要设置得比内核的进程优先级(其值为-5)还要低。

1.4 事件类配置项
accept_mutex [on|off]

accept_mutex参数用于控制是否启用负载均衡锁,其默认值为on,该锁会保证各个worker轮流的、序列化的与新客户端建立连接,并且当某个worker的连接数达到了worker_connections配置的最大连接数的7/8时,该锁会降低该worker将要新建立的连接数,从而保证各个worker的负载均衡。

lock_file path/file;

该参数指定了lock文件的路径。nginx会使用操作系统提供的锁功能,但如果操作系统不支持原子锁,此时才会使用文件锁来实现lock。如果accept_mutex参数设置为off,那么该参数将不会生效。

accept_mutex_delay Nms;

如果一个worker进程尝试获取锁失败了,那么其就会等待该参数指定的时间段之后再次尝试获取锁,该值默认为500ms。

multi_accept [on|off];

当事件模型通知此次有新的连接建立请求时,尽可能的对本次调度中客户端发起的的所有TCP请求都建立连接,该值默认为off。

use [kqueue|rtsig|epoll|/dev/poll|select|poll|eventport];

nginx所选用的事件模型,其会自动使用最适合的模型。在Linux操作系统下支持poll、select和epoll三种,其中epoll的性能是最高的。

worker_connections number;

指定了每个worker进程能够建立的最大连接数。

2. http核心模块配置

2.1 监听端口
listen address:port[default(deprecated)|default_server|[backlog=num|rcvbuf=size|sndbuf=size|accept_filter=filter|deferred|b
listen 127.0.0.1:8080;
listen *:8080;

1、default和default_server:将当前server块作为整个web服务器的默认server块,如果没有server设置了该参数,则将nginx.conf中的第一个server块作为默认server块。设置该参数的原因在于,如果当前请求没有匹配到任意一个server,那么就使用第一个server处理请求;

2、backlog=num:指定了TCP中的backlog队列的大小,默认值为-1。在TCP的三次握手过程中,进程此时还没有开始处理监听句柄,而这些请求都会放在backlog队列中,当backlog队列满时,客户端新的握手请求就会被拒绝;

3、rcvbuf=size:设置监听句柄的SO_RCVBUF参数;

4、sndbuf=size:设置监听句柄的SO_SNDBUF参数;

5、accept_filter:设置accept过滤器,只对FreeBSD操作系统有用;

6、deferred:如果设置了该参数,如果用户发起了TCP连接请求,那么在三次握手成功之后内核也不会调度相应的进程处理请求,而是在用户真正的发送了数据包之后才会将请求发送给具体的进程进行处理;

7、bind:绑定当前端口/地址对,如127.0.0.1:8000,只有同时对一个端口监听多个地址时才会生效;

8、ssl:在当前监听的端口上建立的连接必须基于SSL协议;

2.2 主机名称
语法:server_name name [...];
默认:server_name "";
配置块:server
  1. 首先匹配主机名完全匹配的server块;
  2. 然后匹配前缀使用通配符的server块;
  3. 接着匹配后缀使用通配符的server块;
  4. 最后匹配使用正则表达式的server块;、
    如果没有找到能够匹配的主机名,那么就会按照如下规则寻找server块:
  5. 优先选择在listen项中加入了[default|default_server]的server块;
  6. 找到匹配的listen端口的第一个server块;
2.3 server_names_hash_bucket_size
语法:server_names_hash_bucket_size size;
默认:server_names_hash_bucket_size 32|64|128;
配置块:http、server、location
2.4 server_names_hash_max_size
语法:server_names_hash_max_size size;
默认:server_names_hash_max_size 512;
配置块:http、server、location
2.5 重定向主机名称的处理
语法:server_name_in_redirect on|off;
默认:server_name_in_redirect on;
配置块:http、server或者location
2.6 location
语法:location [=|~|~*|^~|@] /uri/{...}
配置块:server

=表示把URI作为字符串,以便于参数中的uri做完全匹配。例如:

location = / {
  # 只有当用户请求是/时,才会使用该location下的配置
}

~表示匹配URI时是字母大小写敏感的;
~表示匹配URI时是字母大小写不敏感的;*
^~表示匹配URI时只需要其前半部分与uri参数匹配即可。例如:

  # 以/images/开始的请求都会匹配上
}

@表示仅用于nginx服务内部请求之间的重定向,带有@的location不直接处理用户请求;
可以在uir参数里使用正则表达式。如:

  # 匹配以.gif、.jpg、.jpeg结尾的请求
}

3. 文件路径的定义

3.1 以root方式设置资源路径
语法:root path;
默认:root html;
配置块:http、server、locationo、if
location /download/ {
  root /opt/web/html/;
}

这种配置方式会将/download/开始的请求映射到/opt/web/html/目录下,比如某个请求为/download/test/index.html,那么nginx就会到服务器上查找/opt/web/html/download/test/index.html文件。

3.2 以alias方式设置资源文件
语法:alias path;
配置块:location;
location /conf {
  alias /usr/local/nginx/conf;
}

此时如果一个请求为/conf/index.html,那么其前缀/conf将会与当前location匹配,并且会将alias参数替换请求uri中匹配的部分,也就是转换后的uri为/usr/local/nginx/conf/index.html。

3.3 访问首页
语法:index file...;
默认:index index.html;
配置块:http、server、location
location / {
  root path;
  index /index.html /html/index.php /index.php
}

当接收到用户的/请求后,其首先会查询/path/index.html文件是否存在,如果不存在,则查询下一个/path/html/index.php是否存在,如果存在,则直接返回,依此类推。

3.4 根据http返回码重定向页面
语法:error_page code[code...][=|=answer-code]uri|@named_location
配置块:http、server、location、if
error_page 404 /404.html
error_page 502 503 504 /50x.html
error_page 403 http://example.com/forbidden.html
error_page 404 = @fetch;

需要注意的是,即使重定向了URI,返回的HTTP状态码还是原来的状态码,如果需要修改状态码,可以使用=来修改原来的状态码,如:

error_page 404 =200 /empty.gif;
error_page 404 =403 /forbidden.gif;

也可以不指定修改后的状态码,而是由重定向后的请求决定其返回的状态码:

error_page 404 = /empty.gif;

在重定向后,也可以不修改URI,而是将这个请求重定向到另一个location中进行处理,比如:

location / {
  error_page 404 @fallback;
}

location @fallback {
  proxy_pass http://backend;
}
3.5 是否支持递归的使用error_page
语法:recursive_error_pages [on|off];
默认:recursive_error_pages off;
配置块:http、server、location;
3.6 try_files
语法:try_files path1[path2]uri;
配置块:server、location
try_files /system/maintenance.html $uri $uri/index.html $uri.html @other
location @other {
  proxy_pass http://backend;
}

4. 内存及磁盘资源的分配

4.1 http包体只存储到磁盘文件中
语法:client_body_in_file_only on|clean|off;
默认:client_body_in_file_only off;
配置块:http、server、location
4.2 http包体尽量写入到一个内存buffer中
语法:client_body_in_single_buffer on|off;
默认:client_body_in_single_buffer off;
配置块:http、server、location
4.3 存储http头部的内存buffer大小
语法:client_header_buffer_size size;
默认:client_header_buffer_size 1k;
配置块:http、server
4.4 存储超大http头部的内存buffer大小
语法:large_client_header_buffers number size;
默认:large_client_header_buffers 48k;
配置块:http、server
4.5 存储http包体的内存buffer的大小
语法:client_body_buffer_size size;
默认:client_body_buffer_size 8k/16k;
配置块:http、server、location
4.6 http包体的临时存放目录
语法:client_body_temp_path dir-path[level1[level2[level3]]]
默认:client_body_temp_path client_body_temp;
配置块:http、server、location
client_body_temp_path /opt/nginx/client_temp 1 2;

那么nginx就会截取目标文件名的最后1个字符作为一级目录,倒数第二个和第三个总共两个字符作为二级目录,最终文件将会存储在如下目录:

/opt/nginx/client_temp/6/45/00000123456
4.7 connection_pool_size
语法:connection_pool_size size;
默认:connection_pool_size 256;
配置块:http、server
4.8 request_pool_size
语法:request_pool_size size;
默认:request_pool_size 4k;
配置块:http、server

5. 网络连接的设置

5.1 读取http头部的超时时间
语法:client_header_timeout time(默认单位:秒);
默认:client_header_timeout 60;
配置块:http、server、location
5.2 读取http包体的超时时间
语法:client_body_timeout time(默认单位:秒);
默认:client_body_timeout 60;
配置块:http、server、location
5.3 发送响应的超时时间
语法:send_timeout time;
默认:send_timeout 60;
配置块:http、server、location
5.4 reset_timeout_connection
语法:reset_timeout_connection on|off;
默认:reset_timeout_connection off;
配置块:http、server、location
5.5 lingering_close
语法:lingering_close off|on|always;
默认:lingering_close on;
配置块:http、server、location
5.6 lingering_time
语法:lingering_time time;
默认:lingering_time 30s;
配置块:http、server、location
5.7 lingering_timeout
语法:lingering_timeout time;
默认:lingering_timeout 5s;
配置块:http、server、location
5.8 对某些浏览器禁用keepalive功能
语法:keepalive_disable [msie6|safari|none]...
默认:keepalive_disablemsie6 safari
配置块:http、server、location
5.9 keepalive超时时间
语法:keepalive_timeout time(默认单位:秒);
默认:keepalive_timeout 75;
配置块:http、server、location
5.10 keepalive长连接上能够承载的最大请求数
语法:keepalive_requests n;
默认:keepalive_requests 100;
配置块:http、server、location
5.11 tcp_nodelay
语法:tcp_nodelay on|off;
默认:tcp_nodelay on;
配置块:http、server、location
5.12 tcp_nopush
语法:tcp_nopush on|off;
默认:tcp_nopush off;
配置块:http、server、location

6. MIME类型的设置

6.1 MIME type与文件扩展的映射
语法:type {...};
配置块:http、server、location
types {
  text/html html;
  text/html conf;
  image/gif gif;
  image/jpeg jpg;
}
6.2 默认MIME type
语法:default_type MIME-type;
默认:default_type text/plain;
配置块:http、server、location
6.3 types_hash_bucket_size
语法:types_hash_max_size size;
默认:types_hash_max_size 1024;
配置块:http、server、location
6.4 types_hash_max_size
语法:types_hash_max_size size;
默认:types_hash_max_size 1024;
配置块:http、server、location

7. 对客户端请求的限制

7.1 按http方法名限制用户请求
语法:limit_except method...{...}
配置块:location
limit_except GET {
  allow 192.168.1.0/32;
  deny all;
}

上述配置将会限制所有的GET请求的访问,而允许其他方法的请求。

7.2 http请求包体的最大值
语法:client_max_body_size size;
默认:client_max_body_size 1m;
配置块:http、server、location
7.3 对请求的限速
语法:limit_rate speed;
默认:limit_rate 0;
配置块:http、server、location、if
server {
  if ($slow) {
    set $limit_rate 4k;
  }
}
7.4 limit_rate_after
语法:limit_rate_after time;
默认:limit_rate_after 1m;
配置块:http、server、location、if

8. 文件操作的优化

8.1 sendfile系统调用
语法:sendfile on|off;
默认:sendfile off;
配置块:http、server、location
8.2 AIO系统调用
语法:aio on|off;
默认:aio off;
配置块:http、server、location
8.3 directio
语法:directio size|off;
默认:directio off;
配置块:http、server、location
8.4 directio_alignment
语法:directio_alignment size;
默认:directio_alignment 512;
配置块:http、server、location
8.5 打开文件缓存
语法:open_file_cache max=N[inactive=time]|off;
默认:open_file_cache off;
配置块:http、server、location
open_file_cache max=1000 inactive=20s;
8.6 是否缓存打开文件错误的信息
语法:open_file_cache_errors on|off;
默认:open_file_cache_errors off;
配置块:http、server、location
8.7 不被淘汰的最小访问次数
语法:open_file_cache_min_uses number;
默认:open_file_cache_min_uses 1;
配置块:http、server、location
8.8 检验缓存中元素有效性的频率
语法:open_file_cache_valid time;
默认:open_file_cahce_valid 60s;
配置块:http、server、location

9. 对客户端请求的特殊处理

9.1 忽略不合法的http头部
语法:ignore_invalid_headers on|off;
默认:ignore_invalid_headers on;
配置块:http、server
9.2 http头部是否允许下划线
语法:underscores_in_headers on|off;
默认:underscores_in_headers off;
配置块:http、server
9.3 对If-Modified-Since头部的处理策略
语法:if_modified_since [off|exact|before];
默认:if_modified_since exact;
配置块:http、server、location

off:表示忽略用户请求中的If-Modified-Since头部,在每次请求时都将文件内容返回,此时响应状态码为200;

exact:将If-Modified-Since头部包含的时间与将要返回的文件的上次修改时间做精确比较,如果没有匹配上,则返回200和文件的实际内容,如果匹配上了,则表示文件内容已经是最新的,此时就会返回304(Not Modified)状态码,浏览器收到后会直接读取本地缓存;

before:这个是比exact更宽松的策略,只要文件的上次修改时间在用户请求的If-Modified-Since头部指定的时间之前,那么就会向客户端返回304(Not Modified)状态码。

9.4 文件未找到时是否返回error日志
语法:log_not_found on|off;
默认:log_not_found on;
配置块:http、server、location
9.5 merge_slashes
语法:merge_slashes on|off;
默认:merge_slashes on;
配置块:http、server、location
9.6 DNS解析地址
语法:resolver address...;
配置块:http、server、location
resolver 127.0.0.1 192.0.2.1;
9.7 DNS解析的超时时间
语法:resolver_timeout time;
默认:resolver_timeout 30s;
配置块:http、server、location

9.8 返回错误页面时是否在server中注明nginx版本

语法:server_tokens on|off;
默认:server_tokens on;
配置块:http、server、location

对 JAVA 开发有兴趣的朋友欢迎加入QQ群:833145934 里面资深架构师会分享一些整理好的录制视频录像和BATJ面试题:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多。

共同探讨!

原文:https://my.oschina.net/zhangxufeng/blog/3120456

上一篇 下一篇

猜你喜欢

热点阅读