nginx-1.22.1nginx 官网:http://nginx.org/en/nginx 文档:http://nginx.org/en/docs/nginx 官网(中文):http://nginx.p2hp.com/nginx 文档(中文):http://nginx.p2hp.com/en/docs/index.html
#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;# }#}}
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name localhost;location / {root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
}
第一部分:全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以 及配置文件的引入等。
比如上面第一行配置的
worker_processes 1;
第二部分:events块
events 块涉及的指令 主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否 允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
上述例子就表示每个 work process 支持的最大连接数为 1024.
这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
第二部分:http块
这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
需要注意的是:http 块也可以包括 http全局块、server 块。
http全局块
http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。server 块
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,
该技术的产生是为了 节省互联网服务器硬件成本。每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。
而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。全局 server 块
最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。location 块
一个 server 块可以配置多个 location 块。
这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),
对虚拟主机名称 (也可以是IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,
对特定的请求进行处理。 地址定向、数据缓 存和应答控制等功能,
还有许多第三方模块的配置也在这里进行。
#user nobody;#配置用户或者组,默认为nobody nobody。worker_processes 1;:允许生成的进程数,默认为1;这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是 会受到硬件、软件等设备的制约。
#error_log:2.2中默认注释掉的配置,制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg#pid:2.2中默认注释掉的配置,指定nginx进程运行文件存放地址worker_connections 204800;:没个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为:worker_processes*worker_connectionsaccept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为onmulti_accept on; #设置一个进程是否同时接受多个网络连接,默认为off#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventportinclude mime.types;:#文件扩展名与文件类型映射表default_type application/octet-stream;:#默认文件类型,默认为text/plain#access_log off; #取消服务日志#log_format main ...:#自定义格式 为 main#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;:# main 为日志格式的默认值sendfile on;:#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。#tcp_nopush on;:keepalive_timeout 65;:#连接超时时间,默认为65s,可以在http,server,location块。upstream块:定义服务器组合 myTomcats。在 proxy_pass 指令的后面使用upstream myTomcats {server 192.168.0.100:8080;server 192.168.0.101:8080;server example.com:8080 backup; #热备}
#gzip on;:server块: keepalive_requests 120; #单连接请求上限次数。listen 80;:#监听端口server_name localhost; :#监听地址#charset koi8-r;:#access_log logs/host.access.log main;:location块:#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。 #root path; #根目录#index vv.txt; #设置默认页proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表deny 127.0.0.1; #拒绝的ipallow 172.18.5.54; #允许的iplocation / {root html;index index.html index.htm;}
#error_page 404 /404.html;:#错误页location 中可用的匹配命令有两种:普通字符串和正则表达式。~ 和~* 用于正则表达式,其他前缀和无任何前缀都用于普通字符串。正则表达式会根据匹配顺序,匹配到第一个正则表达式后停止搜索。普通字符串匹配则无视顺序,只会选择最精确的匹配。常用的匹配命令和作用如下:
命令 |作用
|-------|------|
~| 表示执行一个正则匹配,区分大小写
~*| 表示执行一个正则匹配,不区分大小写
^~ | 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配其他。一般用来匹配目录
= | 进行普通字符精确匹配
无前缀 | 用于普通字符串
@ | 定义一个命名的location,使用在内部定向时,例如error_page,try_files
location = /{}
location / {
}
ocation ~* .(gif|jpg|jpeg)$ {
}
location /index/ {error_page 404 @index_error;
}
location @index_error {
… }
nginx.conf 配置文件中 location 代码块详解:https://blog.csdn.net/lch551218/article/details/104256019
先放官网文档:http://nginx.org/en/docs/http/load_balancing.html
round-robin:轮询算法(默认),每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
http {upstream myapp1 {server srv1.example.com;server srv2.example.com;server srv3.example.com;}server {listen 80;location / {proxy_pass http://myapp1;}}
}
least-connected:最少连接算法, — next request is assigned to the server with the least number of active connections,
upstream myapp1 {least_conn;server srv1.example.com;server srv2.example.com;server srv3.example.com;
}
ip-hash:IP哈希算法,每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream myapp1 {ip_hash;server srv1.example.com;server srv2.example.com;server srv3.example.com;
}
weight,指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream myapp1 {server srv1.example.com weight=3;server srv2.example.com;server srv3.example.com;
}
With this configuration, every 5 new requests will be distributed across the application instances as the following: 3 requests will be directed to srv1, one request will go to srv2, and another one — to srv3.
It is similarly possible to use weights with the least-connected and ip-hash load balancing in the recent versions of nginx.
fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
server server1;
server server2;
fair;
}
url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
upstream 块 白话一点就是: 定义的一个 服务器IP 组合,每一个服务器可以单独设置参数。weight 就不说,上面负载均衡算法中已经说过了max_fails 默认为1。某台Server允许请求失败的次数,超过最大次数后,在fail_timeout时间内,新的请求将不会分配给这台机器。如果设置为0,Nginx会将这台Server置为永久无效状态,然后将请求发给定义了proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, and memcached_next_upstream指令来处理这次错误的请求。fail_timeout 默认为10秒。某台Server达到max_fails次失败请求后,在fail_timeout期间内,nginx会认为这台Server暂时不可用,不会将请求分配给它max_fails和 fail_timeout 案例:192.168.0.100这台机器,如果有3次请求失败,nginx在15秒内,不会将新的请求分配给它。upstream tomcats {server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;server 192.168.0.101:8080 weight=3;server 192.168.0.102:8080 weight=1;
}
backup 备份机,所有服务器挂了之后才会生效upstream tomcats {server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;server 192.168.0.101:8080 weight=3;server 192.168.0.102:8080 backup;
}
upstream tomcats {server 192.168.0.100:8080 weight=2 max_fails=3 fail_timeout=15;server 192.168.0.101:8080 down;server 192.168.0.102:8080 backup;
}
max_conns 限制分配给某台Server处理的最大连接数量,超过这个数量,将不会分配新的连接给它。默认为0,表示不限制。注意:1.5.9之后的版本才有这个配置upstream tomcats {server 192.168.0.100:8080 max_conns=1000;
}
esolve 将server指令配置的域名,指定域名解析服务器。需要在http模块下配置resolver指令,指定域名解析服务http {resolver 10.0.0.1;upstream u {zone ...;...server example.com resolve;}
}
==upstream模块server指令的其它参数和详细配置说明,请参考官方文档。 ==
http://nginx.org/en/docs/