nginx配置文件
一、Nginx配置文件
1.主配置文件
tip:安装方式不同,路径不同
#主配置文件格式 main block:主配置段,即全局配置段,对http,mail都有效 #配置Nginx服务器的事件模块相关参数 events { ... } #http/https 协议相关配置段 http { ... } #默认配置文件不包括下面两个模块 #mail 协议相关配置段 mail { ... } #stream 服务器相关配置段 stream {负载均衡 ... }
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf
#编译安装nginx后,nginx.conf为例
子配置文件位置:conf.d/*.conf
tip:须在http模块下添加include 子配置文件位置
http块中可以包含多个子配置文件,常见的子配置文件
server块:用于配置HTTP服务器的具体行为,包括监听的端口、虚拟主机的配置、请求处理逻辑等。
location块:用于指定不同URL请求的处理方式,例如静态文件的服务、反向代理等。
upstream块:用于配置反向代理的目标服务器列表。
include指令:用于引入其他的子配置文件,可以将一些通用的配置项单独放在一个文件中,然后通过include指令引入。
通过使用 auto 参数,Nginx 可以根据系统的负载情况智能地分配工作进程,以提供更好的性能和资源利用率。
#修改主配置文件中的work_processes项
vim /apps/nginx/conf/nginx.conf
[root@localhost ~]# nginx -s reload #重新加载配置文件 [root@localhost ~]# ps axo pid,cmd,psr,ni|grep nginx 13013 nginx: master process /apps 1 0 13019 nginx: worker process 0 0 13020 nginx: worker process 0 0 13021 nginx: worker process 0 0 13022 nginx: worker process 1 0 13035 grep --color=auto nginx 0 0
2.
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf user nginx; worker_processes auto; worker_cpu_affinity 00000001 00000010; #绑定到 第一 和 第二块cpu上 error_log /var/log/nginx/error.log; #指定错误日志的路径和文件名
[root@localhost ~]# nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
工作进程优先级,-20~19
[root@localhost ~]# nginx -s reload [root@localhost ~]# ps axo pid,cmd,psr,ni|grep nginx|sort -n 13501 nginx: master process /apps 2 0 14025 nginx: worker process 2 -20 14026 nginx: worker process 3 -20 14027 nginx: worker process 1 -20 14028 nginx: worker process 0 -20 14038 grep --color=auto nginx 3 0
所有worker进程能打开的文件数量上限,包括:Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.最好与ulimit -n 或者limits.conf的值保持一致,
先修改主配置文件
vim /apps/nginx/conf/nginx.conf #添加 worker_rlimit_nofile 65536
再修改系统默认项
ulimit -n 70000 #修改单个进程能打开的最大文件数为 70000 #仅应用于当前会话即不会永久修改限制 ulimit -a #显示当前用户的所有资源限制信息
[root@localhost ~]# ulimit -n 70000 [root@localhost ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 7168 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 70000 #默认值为1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 7168 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
vim /etc/security/limits.conf #在最后加入 * soft core unlimited * hard core unlimited * soft nproc 1000000 * hard nproc 1000000 * soft nofile 1000000 * hard nofile 1000000 * soft memlock 32000 * hard memlock 32000 * soft msgqueue 8192000 * hard msgqueue 8192000 `nproc`(最大进程数限制)的软限制和硬限制都设置为 1000000,当前用户在单个会话中可以创建的最大进程数为 1000000 `nofile`(打开文件描述符限制)的软限制和硬限制都设置为 1000000,这意味着当前用户在单个会话中可以使用的最大文件描述符数将被限制为 1000000。软限制是软性限制,用户可以根据需要进行调整,而硬限制是硬性限制,一旦设定,用户无法超过该限制 `memlock`(锁定内存限制)的软限制和硬限制都设置为 32000,这意味着当前用户在单个会话中可以锁定的最大内存量为 32000KB `msgqueue`(消息队列限制)的软限制和硬限制都设置为 8192000,这意味着当前用户在单个会话中可以使用的最大消息队列大小为 8192000字节
master_process off|on;
#是否开启Nginx的master-worker工作模式,仅用于开发调试场景,默认为on
在Nginx的主配置文件中,events部分用于配置Nginx服务器的事件模块相关参数,控制Nginx服务器在处理连接请求时的行为。
常见的events配置参数:
worker_connections:指定每个工作进程可以同时处理的最大连接数。
multi_accept:指定是否一次接受多个连接。默认情况下,Nginx在每个循环中只接受一个连接,但设置multi_accept为"on"后可以同时接受多个连接。
use:指定Nginx使用的事件模块。常见的事件模块有"epoll"、"kqueue"和"eventport"等。
#举个例子 events { worker_connections 1024; multi_accept on; use epoll; } #指定了每个工作进程可以处理的最大连接数为1024,启用了多个连接同时接受,以及使用了epoll事件模块
include:引入其他配置文件,通常用于加载 MIME 类型配置文件。
default_type:指定默认的 MIME 类型。
server:定义一个或多个虚拟主机。
listen:指定该虚拟主机监听的端口。
server_name:指定域名,用于匹配请求的主机头。
root:指定虚拟主机的根目录。
location:用于匹配不同的 URL,并定义相关配置规则。
#基本格式 http { ... ... #各server的公共配置 server { #每个server用于定义一个虚拟主机,第一个server为默认虚拟服务器 ... } server { ... server_name #虚拟主机名 root #主目录 alias #路径别名 location [OPERATOR] URL { #指定URL的特性 ... if CONDITION { ... } } } }
#详解 http { include mime.types; #导入支持的文件类型,是相对于/apps/nginx/conf的目录 default_type application/octet-stream; #除mime.types中文件类型外,设置其它文件默认类型,访问其它类型时会提示下载不匹配的类型文件 #日志配置部分 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' refer是跳转的源地址,如果没有跳转 没有refer # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; #自定义优化参数 sendfile on; #tcp_nopush on; #在开启了sendfile的情况下,合并请求后统一发送给客户端。 #tcp_nodelay off; #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认On时,不延迟发送,立即发送用户响应报文。 #keepalive_timeout 0; keepalive_timeout 65 65; #设置会话保持时间,第二个值为响应首部:keepAlived:timeout=65,可以和第一个值不同 #gzip on; #开启文件压缩 server { listen 80; #设置监听地址和端口 server_name localhost; #设置server name,可以以空格隔开写多个并支持正则表达式,如:*.kgc.com www.kgc.* ~^www\d+\.kgc\.com$ default_server #charset koi8-r; #设置编码格式,默认是俄语格式,建议改为utf-8 #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$ { #以http的方式转发php请求到指定web服务器 # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #以fastcgi的方式转发php请求到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 { #拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件 来改变自己的重定向等功能。 # deny all; #} location ~ /passwd.html { deny all; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { #自定义虚拟server
在Nginx中,“mime” 是一种配置指令,用于设置 MIME 类型与文件扩展名的映射关系。
vim /apps/nginx/conf/mime.types
#查看当前Nginx服务器配置的MIME类型列表
在Nginx配置中,"root"指令用于设置服务器块(server block)的根目录,即指明软件的根目录。
通常,"root"指令位于Nginx配置文件中的服务器块(server block)中。
server { listen 80; server_name example.com; root /var/www/html; location / { ... } ... } #指定了服务器块的根目录为"/var/www/html" #访问该服务器的网站时,Nginx会在"/var/www/html"文件夹中查找并提供相应的静态文件
以手机端和电脑端为例
mkdir -p /apps/nginx/conf.d/#建立虚拟主机配置文件目录 cd /apps/nginx/conf #主配置文件所在目录 #添加到http模块中 vim nginx.conf include /apps/nginx/conf.d/*.conf; #声明子配置文件的位置,需要写在最上面 nginx -t #保存后检查语法
cd /apps/nginx/conf.d/ #编写电脑端的配置文件 vim computer.conf server{ listen 192.168.10.10:80; server_name www.computer.com; location / { root /data/nginx/html/pc; } } #编写手机端的配置文件 vim mobile.conf server { listen 192.168.10.10:80; server_name www.mobile.com; root /data/nginx/html/mobile/; } [root@localhost logs]# nginx -t nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful [root@localhost logs]# nginx -s reload
#构建数据文件夹 mkdir /apps/nginx/html/pc -pv mkdir /apps/nginx/html/mobile -pv
#构建数据文件 echo This is computer > /apps/nginx/html/pc/index.html echo This is mobile > /apps/nginx/html/mobile/index.html
#切换到测试机 #修改本地hosts文件 添加对应映射 vim /etc/hosts 192.168.10.10 www.computer.com 192.168.10.10 www.mobile.com
#测试
curl www.mobile.com
This is mobile
curl www.computer.com
This is computer
服务端 #编辑子配置文件 mobile.conf,修改端口号 server{ listen 192.168.10.10:8080; server_name www.mobile.com; root /data/nginx/html/moblie; } #computer.conf 不变 server{ listen 192.168.10.10:80; server_name www.computer.com; root /data/nginx/html/pc; }
切换到客户端 curl 192.168.10.10:8080 This is mobile curl 192.168.10.10:80 This is computer
服务端
添加新网卡并配置
#编辑子配置文件mobile.conf,修改IP地址 cd /apps/nginx/conf.d/ vim mobile.conf server{ listen 192.168.10.11:80; server_name www.mobile.com; root /data/nginx/html/mobile; } #computer.conf不变 server{ listen 192.168.10.10:80; server_name www.computer.com; root /data/nginx/html/pc; }
修改客户端hosts文件 vim /etc/hosts 192.168.10.10 www.computer.com 192.168.10.11 www.mobile.com
curl 192.168.10.11 This is mobile curl 192.168.10.10 This is computer
在 Nginx 中,alias 用于创建一个路径别名的指令。
别名可以用于将文件或目录从一个位置映射到另一个位置,提供更灵活的访问控制。
服务端 #编辑子配置文件cxk.conf,使用alias server { listen 80; location /test{ alias /apps/nginx/html/pc; #相当于替换,访问/test/就是访问/apps/nginx/html/pc; } }
#官方帮助文档 http://nginx.org/en/docs/http/ngx_http_core_module.html#location
#基本语法
location [修饰符] 匹配的路径 {
... 配置指令 ...
}
```
修饰符 | 功能 |
---|---|
= | 精确字符串匹配 |
^~ | 前缀匹配,如果URL路径以指定的字符串开始,则停止查找其他匹配。 |
~ | 使用正则表达式进行匹配,区分大小写 |
~* | 使用正则表达式进行匹配,不区分大小写 |
不带符号 | 匹配起始于此uri的所有的uri |
\ |
优先级
Nginx的access
模块允许用户定义基于IP地址、请求方法、URI等条件的访问规则,以控制客户端对NGINX服务器上特定资源的访问。
1)IP地址访问控制:允许或拒绝特定IP地址或IP地址范围的访问
拒绝特定IP地址的访问 location / { deny 192.168.10.10; #默认允许所有 }
允许特定IP地址段的访问 location / { allow 192.168.2.0/24; deny all; }
2)请求方法访问控制:允许或拒绝特定HTTP请求方法(如GET、POST等)的访问。
仅允许GET请求 if ($request_method != GET) { return 403; }
3)URI访问控制:允许或拒绝特定URI或URI模式的访问。
拒绝特定URI的访问 location /cxk { deny all; }
允许特定URI模式的访问 location ~ ^/api/ { allow all; }
4)条件组合访问控制:根据多个条件的组合来实施访问控制策略。
仅允许特定IP地址段的GET请求访问特定URI模式 #举个例子 location ~ ^/api/ { if ($request_method != GET) { return 403; } allow 192.168.1.0/24; deny all; }
安装 yum install httpd-tools -y
常用命令 #第一次生成文件 htpasswd -c 文件路径 姓名 交互式生成密码 htpasswd -bc 文件路径 姓名 密码 直接将密码跟在 -c 代表新建用户名和密码对应的文件 -b 将密码跟在用户名后
#非第一次生成文件
htpasswd 文件路径 姓名 交互式生成密码
htpasswd -b 文件路径 姓名 密码 直接将密码跟在后面
#编辑配置文件 vim cxk.conf server { listen 80; location /test{ alias /apps/nginx/html/pc; } location /admin{ root /data/nginx/html/pc; auth_basic "admin site"; #提示信息,不是所有浏览器都有用 auth_basic_user_file /apps/nginx/conf.d/.httpuser; #密码文件存放位置 } }
使用 Basic 认证(基本认证)对用户进行身份验证
htpasswd -bc /apps/nginx/conf.d/.httpuser byyd 123456 #创建一个.htpasswd文件,并添加一个使用Basic认证的用户名和密码
使用主机浏览器
Syntax: server_tokens on | off | build | string; Default: server_tokens on; Context: http, server, location #修改配置文件 放在 http语句中 http { server_tokens off; [root@localhost nginx]#nginx -s reload #重新加载 验证 [root@localhost sbin]#curl -I http://192.168.10.10/ HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Thu, 21 Apr 2022 03:34:51 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 19 Apr 2022 02:43:17 GMT Connection: keep-alive ETag: "625e21c5-264" Accept-Ranges: bytes [root@localhost sbin]#curl -I http://192.168.10.10/ HTTP/1.1 200 OK Server: nginx Date: Thu, 21 Apr 2022 04:04:23 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 19 Apr 2022 02:43:17 GMT Connection: keep-alive ETag: "625e21c5-264" Accept-Ranges: bytes
思路: 修改安装包里源码, 再重新编译 源码路径 /nginx-1.18.0/src/core/nginx.h
cd /opt vim./nginx-1.18.0/src/core/nginx.h #修改前 #define nginx_version 1018000 #define NGINX_VERSION "1.18.0" #define NGINX_VER "nginx/" NGINX_VERSION #修改后 #define nginx_version 1018000 #define NGINX_VERSION "cxk" #define NGINX_VER "nginx/" NGINX_VERSION vim ./nginx-1.18.0/src/http/ngx_http_header_filter_module.c #修改模块的源代码文件 #修改前 static u_char ngx_http_server_string[] = "Server: nginx" CRLF; static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF; #修改后 static u_char ngx_http_server_string[] = "Server: cxk" CRLF; static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
##然后重新编译安装 ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module make -j2 && make install 编译安装完成后,重启 nginx -v #查看版本信息 curl -I 192.168.2.100
#error_page 404 /404.html; #默认错误页面
修改错误页面的主要目的是为了提供更友好的用户体验和品牌一致性。
#部分响应状态码 400 Bad Request:请求不正确或无效的错误页面。 401 Unauthorized:未授权访问的错误页面。 403 Forbidden:禁止访问的错误页面。 404 Not Found:页面未找到的错误页面。 500 Internal Server Error:服务器内部错误的错误页面。 502 Bad Gateway:错误的网关请求的错误页面。 503 Service Unavailable:服务不可用的错误页面
配置文件中的可用位置:http, server, location, if in location。
#基本格式 error_page code ... [=[response]] uri; #构成详解 error_page 固定写法 code 响应码 = 可以将响应码转换 uri 访问连接
新建错误页面和错误页面目录 mkdir /apps/nginx/html/pc/error/ cd /appsnginx/html/pc/error/ vim 40x.html #错误页面 在子配置文件的sever语块下 error_page 404 =302 /40x.html; location = /40x.html { root /apps/nginx/html/pc/error/; }
通过修改日志的路径,可以实现日志分离,即不同网站的日志单独存放。
mkdir /data/logs #新建存放日志的目录 #编辑子配置文件,指定日志存放位置 在server语块下添加 error_log /data/logs/m_error.log; access_log /data/logs/m_access.log; nginx -t nginx -s reload #语法检查无误后重载
HTTP Keep-Alive 功能用于实现长连接,允许客户端和服务器之间的 TCP 连接在发送完一个请求后保持打开状态,以便在同一连接上发送多个请求和响应。
可以加在全局或者 server 。
keepalive 配置指令仅对 HTTP/1.0 和 HTTP/1.1 版本的连接有效。
对于 HTTP/2 连接,keepalive 功能是默认启用的,并且无需额外配置。
keepalive_timeout timeout [header_timeout];
#设定保持连接超时时长
keepalive_timeout
用于定义长连接超时时间。
当一个客户端与服务器之间的连接完成一个请求后的等待时间。 如果在这个时间内没有收到新的请求,服务器会关闭连接。
keepalive_requests number;
#在一次长连接上所允许请求的资源的最大数量
当达到指定数量后,服务器会关闭该连接并且客户端需要重新建立新连接。
默认情况下,keepalive_requests 的值是 100。
如果将 keepalive_requests 设置为 0,则表示在完成每个请求后立即关闭连接,禁用了 Keep-Alive 功能。
client_max_body_size 1m;
#设置允许客户端上传单个文件的最大值,默认值为1m,上传文件超过此值会出413错误
client_body_buffer_size size;
#用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
client_body_temp_path path [level1 [level2 [level3]]];
#设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量,目录名为16进制的数字,使用hash之后的值从后往前截取1位、2位、2位作为目录名
上传文件大于限制 错误代码413
directio size | off; #操作完全和aio相反,aio是读取文件而directio是写文件到磁盘,启用直接I/O,默认为关闭,当文件大于等于给定大小时,例如:directio 4m;同步(直接)写磁盘,而非写缓存。 直接 写入 磁盘 还是等待一定数据量写入磁盘
open_file_cache off; #是否缓存打开过的文件信息 open_file_cache max=N [inactive=time]; #nginx可以缓存以下三种信息: (1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间 (2) 打开的目录结构 (3) 没有找到的或者没有权限访问的文件的相关信息 max=N:#可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used,最近最少使用)算法实现管理 inactive=time:#缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于 open_file_cache_min_uses #指令所指定的次数的缓存项即为非活动项,将被删除 open_file_cache_valid time; #缓存项有效性的检查验证频率,默认值为60s open_file_cache_errors on | off; #是否缓存查找时发生错误的文件一类的信息,默认值为off open_file_cache_min_uses number; #open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项,默认值为1 范例: open_file_cache max=10000 inactive=60s; #最大缓存10000个文件,非活动数据超时时长60s open_file_cache_valid 60s; #每间隔60s检查一下缓存数据有效性 open_file_cache_min_uses 5; #60秒内至少被命中访问5次才被标记为活动数据 open_file_cache_errors on; #缓存错误信息 limit_except method ... { ... },仅用于location #限制客户端使用除了指定的请求方法之外的其它方法 method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH limit_except GET { allow 192.168.91.101; deny all; } #除了GET和HEAD 之外其它方法仅允许192.168.1.0/24网段主机使用