nginx配置文件解读

野性酷女 2022-11-29 12:48 251阅读 0赞

nginx配置文件主要分为四个部分:

main{ #(全局设置)
http{ #服务器
upstream{} #(负载均衡服务器设置:主要用于负载均衡和设置一系列的后端服务器)
server{ #(主机设置:主要用于指定主机和端口)
location{} #(URL匹配特点位置的设置)
}
}
}

server继承main,location继承server,upstream即不会继承其他设置也不会被继承。

nginx配置文件结构图

一、main 全局配置

nginx在运行时与具体业务功能(比如http服务或者email服务代理)无关的一些参数,比如工作进程数,运行的身份等。

  1. user www www;
  2. worker_processes 4;
  3. worker_cpu_affinity 0001 0010 0100 1000;
  4. error_log /var/logs/nginx_error.log crit;
  5. pid /usr/local/webserver/nginx/nginx.pid;
  6. worker_rlimit_nofile 65535;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

  • user www www;: 指定nginx进程使用什么用户启动

  • worker_processes 4; : 指定启动多少进程来处理请求,一般情况下设置成CPU的核数,如果开启了ssl和gzip更应该设置成与逻辑CPU数量一样甚至为2倍,可以减少I/O操作。使用grep ^processor /proc/cpuinfo | wc -l查看CPU核数。
  • worker_cpu_affinity 0001 0010 0100 1000;: 在高并发情况下,通过设置将CPU和具体的进程绑定来降低由于多核CPU切换造成的寄存器等现场重建带来的性能损耗。如worker_cpu_affinity 0001 0010 0100 1000; (四核)。
  • error_log /var/logs/nginx_error.log crit;: error_log是个主模块指令,用来定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少。
  • pid /usr/local/webserver/nginx/nginx.pid;: 指定进程pid文件的位置。
  • worker_rlimit_nofile 65535;: 用于指定一个nginx进程可以打开的最多文件描述符数目,这里是65535,需要使用命令“ulimit -n 65535”来设置。

二、events模块

  1. events{
  2. use epoll;
  3. worker_connections 65536;
  4. }
  • 1
  • 2
  • 3
  • 4

  • use epoll;use是个事件模块指令,用来指定Nginx的工作模式。Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll。其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,而kqueue用在BSD系统中。对于Linux系统,epoll工作模式是首选。在操作系统不支持这些高效模型时才使用select。

  • worker_connections 65536;每一个worker进程能并发处理(发起)的最大连接数(包含与客户端或后端被代理服务器间等所有连接数)。nginx作为反向代理服务器,计算公式 最大连接数 = worker_processes * worker_connections/4,所以这里客户端最大连接数是65536,这个可以增到到8192都没关系,看情况而定,但不能超过后面的worker_rlimit_nofile。当nginx作为http服务器时,计算公式里面是除以2。进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令ulimit -n 65536worker_connections的设置才能生效。

三、http服务器

  1. http{
  2. include mime.types;
  3. default_type application/octet-stream;
  4. #charset gb2312;
  5. }
  • 1
  • 2
  • 3
  • 4
  • 5

  • include是个主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。类似于Apache中的include方法。

  • default_type属于HTTP核心模块指令,这里设定默认类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置PHP环境时,Nginx是不予解析的,此时,用浏览器访问PHP文件就会出现下载窗口。
  • charset gb2312; 指定客户端编码格式。

3.1 HTTP参数之客户端head缓存

  1. server_names_hash_bucket_size 128;
  2. client_header_buffer_size 32k;
  3. large_client_header_buffers 4 128k;
  4. client_max_body_size 10m;
  5. client_body_buffer_size 128k;
  6. sendfile on ;
  7. tcp_nopush on;
  8. tcp_nodelay on;
  9. keepalive_timeout 65 :
  10. client_body_timeout 60s;
  11. send_timeout 60s;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

  • server_names_hash_bucket_size 128;: 服务器名字的hash表大小。

  • client_header_buffer_size 32k;:用来指定来自客户端请求头的header buffer 大小。对于大多数请求,1K的缓存已经足够了,如果自定义了消息头或有更大的cookie,可以增大缓存区大小。
  • large_client_header_buffers 4 128k;:用来指定客户端请求中较大的消息头的缓存最大数量和大小,4为个数,128k为大小,最大缓存为4个128KB。
  • client_max_body_size 8m; : 客户端请求的最大的单个文件字节数。
  • client_max_body_size 10m; : 允许客户端请求的最大单文件字节数。如果有上传较大文件,请设置它的限制值。
  • client_body_buffer_size 128k;: 缓冲区代理缓冲用户端请求的最大字节数。
  • sendfile on ; : 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。开启 tcp_nopush on;tcp_nodelay on; 防止网络阻塞。
  • keepalive_timeout 65 : : 长连接超时时间,单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操作系统的设置,可以另外起一片文章了。长连接请求大量小文件的时候,可以减少重建连接的开销,但假如有大文件上传,65s内没上传完成会导致失败。如果设置时间过长,用户又多,长时间保持连接会占用大量资源。
  • client_body_timeout 60s; : 用于设置客户端请求主体读取超时时间,默认是60s。如果超过这个时间,客户端还没有发送任何数据,nginx将返回Request time out(408)错误。
  • send_timeout : : 用于指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

3.2 HTTP参数之FastCGI参数

FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。

  1. fastcgi_connect_timeout 300;
  2. fastcgi_send_timeout 300;
  3. fastcgi_read_timeout 300;
  4. fastcgi_buffer_size 64k;
  5. fastcgi_buffers 4 64k;
  6. fastcgi_busy_buffers_size 128k;
  7. fastcgi_temp_file_write_size 128k;
  8. fastcgi_cache TEST;
  9. fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
  10. fastcgi_cache_valid 200 302 1h;
  11. fastcgi_cache_valid 301 1d;
  12. fastcgi_cache_valid any 1m;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

  • fastcgi_connect_timeout 300; 指定连接到后端FastCGI的超时时间。

  • fastcgi_send_timeout 300;指定向FastCGI传送请求的超时时间,这个值是已经完成两次握手后向FastCGI传送请求的超时时间。
  • fastcgi_read_timeout 300;指定接收FastCGI应答的超时时间,这个值是已经完成两次握手后接收FastCGI应答的超时时间。
  • fastcgi_buffer_size 64k; 用于指定读取FastCGI应答第一部分需要多大的缓冲区,这个值表示将使用1个64KB的缓冲区读取应答的第一部分(应答头),可以设置为fastcgi_buffers选项指定的缓冲区大小。
  • fastcgi_buffers 4 64k; 指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,那么会为其分配4个64KB的缓冲区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于硬盘。一般这个值应该为站点中PHP脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“16 16k”、“4 64k”等。
  • fastcgi_busy_buffers_size 128k; 默认值是fastcgi_buffers的两倍。
  • fastcgi_temp_file_write_size 128k; 表示在写入缓存文件时使用多大的数据块,默认值是fastcgi_buffers的两倍。
  • fastcgi_cache TEST; 表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502错误的发生。但是开启缓存也会引起很多问题,要视具体情况而定。
  • fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; FastCGI缓存指定一个文件路径、目录结构等级、关键字区域存储时间和非活动删除时间。
  • fastcgi_cache_valid 200 302 1h; 用来指定应答代码的缓存时间。实例中的值表示将200和302应答缓存一个小时,将301应答缓存1天,其他应答均缓存1分钟。

3.3. HTTP参数之gzip模块设置

  1. gzip on;
  2. gzip_min_length 1k;
  3. gzip_buffers 4 16k;
  4. gzip_http_version 1.1;
  5. gzip_comp_level 6;
  6. gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  7. gzip_vary on;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

  • gzip on;开启gzip压缩输出

  • gzip_min_length 1k; 最小压缩文件大小,页面字节数从header头的Content-Length中获取。默认值为0,不管多大页面都压缩,建议设置成大于1K的字节数,小于1K可能会越压越大。
  • gzip_buffers 4 16k; 压缩缓冲区,表示申请四个16K的内存作为压缩结果流缓存,默认是申请与原始数据大小相同的内存空间来存储gzip压缩结果。
  • gzip_http_version 1.1; 用于设置识别HTTP协议版本,默认是1.1,目前主流浏览器都已成指出。(默认1.1,前端如果是squid2.5请使用1.0)
  • gzip_comp_level 6; 压缩等级,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但是消耗CPU资源。
  • gzip_types text/plain application/x-javascript text/css application/xml;压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
  • gzip_vary on; 和http头有关系,会在响应头加个 Vary: Accept-Encoding ,可以让前端的缓存服务器缓存经过gzip压缩的页面,例如,用Squid缓存经过Nginx压缩的数据。
  • gzip_proxied any; Nginx作为反向代理的时候启用,决定开启或者关闭后端服务器返回的结果是否压缩,匹配的前提是后端服务器必须要返回包含”Via”的 header头。
  • limit_zone crawler $binary_remote_addr 10m; 开启限制IP连接数的时候需要使用

四、nginx 配置虚拟主机

4.1 配置虚拟主机流程

  1. 复制一段完整的server标签段,到结尾。注意:要放在http的结束大括号前,也就是server标签段放入http标签。
  2. 更改server_name 及对应网页的root根目录。
  3. 检查配置文件语法,平滑重启服务。
  4. 创建server_name 对应网页的根目录,并且建立测试文件,如果没有index首页会出现403错误。
  5. 对客户端server_name 的主机做host 解析或DNS配置。并检查(ping)。
  6. 浏览器访问,或者在Linux客户端做host解析,用wget或curl 访问。

http服务上支持若干虚拟主机。每个虚拟主机一个对应的server配置项,配置项里面包含该虚拟主机相关的配置。在提供mail服务的代理时,也可以建立若干server。每个server通过监听地址或端口来区分。

  1. server{
  2. listen 80 default;
  3. server_name _;
  4. index index.html index.htm index.php;
  5. root /data/htdocs/www;
  6. #server_name_in_redirect off;
  7. location ~ .*\.(php|php5)?${
  8. #fastcgi_pass unix:/tmp/php-cgi.sock;
  9. fastcgi_pass 127.0.0.1:9000;
  10. fastcgi_index index.php;
  11. include fcgi.conf;
  12. }
  13. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
  14. expires 30d;
  15. }
  16. location ~ .*\.(js|css)?${
  17. expires 1h;
  18. }
  19. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

  • listen 80; 监听端口,默认80,小于1024的要以root启动。可以为listen *:80、listen 127.0.0.1:80等形式。

  • server_name blog.biglittleant.cn; 服务器名,如localhost、www.example.com,可以通过正则匹配。
  • root /var/www/html 定义服务器的默认网站根目录位置。如果locationURL匹配的是子目录或文件,root没什么作用,一般放在server指令里面或/下。
  • index index.jsp index.html index.htm 定义路径下默认访问的文件名,一般跟着root放。

4.2 sever模块下location模块的写法

关于location匹配规则的写法,参考死磕nginx系列–使用nginx做负载均衡

proxy_pass http://backend

请求转向backend定义的服务器列表,即反向代理,对应upstream负载均衡器。也可以proxy_pass http://ip:port。

  1. proxy_redirect off;
  2. proxy_set_header Host $host;
  3. proxy_set_header X-Real-IP $remote_addr;
  4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  • 1
  • 2
  • 3
  • 4

五、其它nginx参数

5.1 访问控制 allow/deny

Nginx 的访问控制模块默认就会安装,而且写法也非常简单,可以分别有多个allow,deny,允许或禁止某个ip或ip段访问,依次满足任何一个规则就停止往下匹配。如:

  1. location /nginx-status {
  2. stub_status on;
  3. access_log off;
  4. # auth_basic "NginxStatus";
  5. # auth_basic_user_file /usr/local/nginx-1.6/htpasswd;
  6. allow 192.168.10.100;
  7. allow 172.29.73.0/24;
  8. deny all;
  9. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

我们也常用 httpd-devel 工具的 htpasswd 来为访问的路径设置登录密码:

  1. # htpasswd -c htpasswd admin
  2. New passwd:
  3. Re-type new password:
  4. Adding password for user admin
  5. # htpasswd htpasswd admin //修改admin密码
  6. # htpasswd htpasswd sean //多添加一个认证用户
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样就生成了默认使用CRYPT加密的密码文件。打开上面nginx-status的两行注释,重启nginx生效。

5.2 列出目录 autoindex

Nginx默认是不允许列出整个目录的。如需此功能,打开nginx.conf文件,在location,server 或 http段中加入如下参数:

  1. location /images {
  2. root /var/www/nginx-default/images;
  3. autoindex on;
  4. autoindex_exact_size off;
  5. autoindex_localtime on;
  6. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

  • autoindex on;运行列出目录内容。另外两个参数最好也加上去。

  • autoindex_exact_size off; 默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB。
  • autoindex_localtime on; 默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间。

六、附录:通用配置文件

  1. user www www;
  2. worker_processes 2;
  3. error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. pid logs/nginx.pid;
  7. events {
  8. use epoll;
  9. worker_connections 2048;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. # '$status $body_bytes_sent "$http_referer" '
  16. # '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. sendfile on;
  19. # tcp_nopush on;
  20. keepalive_timeout 65;
  21. # gzip压缩功能设置
  22. gzip on;
  23. gzip_min_length 1k;
  24. gzip_buffers 4 16k;
  25. gzip_http_version 1.1;
  26. gzip_comp_level 6;
  27. gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  28. gzip_vary on;
  29. # http_proxy 设置
  30. client_max_body_size 10m;
  31. client_body_buffer_size 128k;
  32. proxy_connect_timeout 75;
  33. proxy_send_timeout 75;
  34. proxy_read_timeout 75;
  35. proxy_buffer_size 4k;
  36. proxy_buffers 4 32k;
  37. proxy_busy_buffers_size 64k;
  38. proxy_temp_file_write_size 64k;
  39. proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
  40. # 设定负载均衡后台服务器列表
  41. upstream backend {
  42. #ip_hash;
  43. server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
  44. server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
  45. }
  46. # 很重要的虚拟主机配置,多个虚拟机可以复制修改此部分
  47. server {
  48. listen 80;
  49. server_name test.example.com;
  50. root /apps/oaapp;
  51. charset utf-8;
  52. access_log logs/host.access.log main;
  53. #对 / 所有做负载均衡+反向代理
  54. location / {
  55. root /apps/oaapp;
  56. index index.php index.html index.htm;
  57. proxy_pass http://backend;
  58. proxy_redirect off;
  59. # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
  60. proxy_set_header Host $host;
  61. proxy_set_header X-Real-IP $remote_addr;
  62. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  63. proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  64. }
  65. #静态文件,nginx自己处理,不去backend请求后端的服务
  66. location ~* /download/ {
  67. root /data/app/nginx/downloads;
  68. }
  69. location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
  70. root /data/app/nginx/images;
  71. expires 7d;
  72. }
  73. location /nginx_status {
  74. stub_status on;
  75. access_log off;
  76. allow 192.168.10.0/24;
  77. deny all;
  78. }
  79. location ~ ^/(WEB-INF)/ {
  80. deny all;
  81. }
  82. #error_page 404 /404.html;
  83. # redirect server error pages to the static page /50x.html
  84. #
  85. error_page 500 502 503 504 /50x.html;
  86. location = /50x.html {
  87. root html;
  88. }
  89. }
  90. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

六、附录:如何查看是否开启了gzip压缩

如果response header中包含Content-Encoding:gzip则表示开启gzip压缩。

  1. Connection:keep-alive
  2. Content-Encoding:gzip
  3. Content-Type:text/html
  4. Date:Wed, 29 Mar 2017 10:55:54 GMT
  5. ETag:W/"58db92af-331a6"
  6. Last-Modified:Wed, 29 Mar 2017 10:55:43 GMT
  7. Server:nginx/1.10.3
  8. Transfer-Encoding:chunked
  9. Vary:Accept-Encoding

发表评论

表情:
评论列表 (有 0 条评论,251人围观)

还没有评论,来说两句吧...

相关阅读

    相关 解读 Nginx 配置

    tip:作为程序员一定学习编程之道,一定要对代码的编写有追求,不能实现就完事了。我们应该让自己写的代码更加优雅,即使这会费时费力。 推荐:[体系化学习Java(Java面试专

    相关 shiro 配置文件解读

    注:这里只介绍 spring 配置模式。 因为官方例子虽然中有更加简洁的ini配置形式,但是使用ini配置无法与spring整合。而且两种配置方法一样,只是格式不一样。