Nginx核心模块

Nginx核心模块

3.1配置文件说明

  • Nginx的配置文件的组成部分:
    主配置文件:nginx.conf
    子配置文件: include conf.d/*.conf
    fastcgi, uwsgi,scgi 等协议相关的配置文件
    mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网
    邮件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定
    某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自
    动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方
    式。
    MIME参考文档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME
    _Types

nginx 配置文件格式说明

配置文件由指令与指令块构成
每条指令以;分号结尾,指令与值之间以空格符号分隔
可以将多条指令放在同一行,用分号分隔即可,但可读性差,不推荐
指令块以{ }大括号将多条指令组织在一起,且可以嵌套指令块
include语句允许组合多个配置文件以提升可维护性
使用#符号添加注释,提高可读性
使用$符号使用变量
部分指令的参数支持正则表达式

编译安装路径:/apps/nginx/conf

yum安装:ll /etc/nginx/nginx.conf

主配置文件结构:四部分

main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置
event {
...
} 
#http/https 协议相关配置段
http {
...
}     
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
...
}  
#stream 服务器相关配置段
stream {
...
}

3.2全局配置

一般编译安装;

Main 全局配置段常见的配置指令分类

  • 正常运行必备的配置
  • 优化性能相关的配置
  • 用于调试及定位问题相关的配置
  • 事件驱动相关的配置
user nginx nginx; #启动Nginx工作进程的用户和组
worker_processes [number | auto]; #启动Nginx工作进程的数量,一般设为和CPU核心数相同 auto 自动查询  --nginx -s reload

worker_cpu_affinity 00000001 00000010 00000100 00001000 | auto ; #将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
CPU MASK: 00000001:0号CPU
     00000010:1号CPU
 10000000:7号CPU
#示例:
worker_cpu_affinity 0001 0010 0100 1000;第0号---第3号CPU
worker_cpu_affinity 0101 1010;

#示例
worker_processes  4;
worker_cpu_affinity 00000010 00001000 00100000 10000000;
[root@centos8 ~]# ps axo pid,cmd,psr | grep nginx
31093 nginx: master process /apps  1
34474 nginx: worker process     1
34475 nginx: worker process     3
34476 nginx: worker process     5
范例: 实现 nginx 的高并发配置
34477 nginx: worker process     7
35751 grep nginx

killall nginx ---#杀掉nginx

pid文件保存路径
pid    /apps/nginx/logs/nginx.pid;
#进程关闭,文件自动删除


worker_priority 0; #工作进程优先级,-20~20(19)
nice指定优先级  -20~19  #数字越大,优先级越低,


ps axo pid,cmd,ni |grep ping
ps axo pid,cmd,ni |grep nginx
ll /proc/13538/fd  #进程描述符


worker_priority 0; #工作进程优先级,-20~20(19)
worker_rlimit_nofile 65536; #所有worker进程能打开的文件数量上限,包括:Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.最好与ulimit -n 或者limits.conf的值保持一致,

ulimit -n #连接数

vim /etc/security/limits.conf

*       -    nofile  66666

vim /usr/bin/systemd/system/nginx.service
 
  [Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
 systemctl enable --now nginx 


 
 
 daemon off;  #前台运行Nginx服务用于测试、或者以容器运行时,需要设为off
 
 events {
 worker_connections  65536;  #设置单个工作进程的最大并发连接数
 use epoll; #使用epoll事件驱动,Nginx支持众多的事件驱动,比如:select、poll、epoll,只能设置在events模块中设置。
 accept_mutex on; #on为同一时刻一个请求轮流由worker进程处理,而防止被同时唤醒所有worker,避免多个睡眠进程被唤醒的设置,默认为off,新请求会唤醒所有worker进程,此过程也称为"惊
群",因此nginx刚安装完以后要进行适当的优化。建议设置为on
 multi_accept on; #on时Nginx服务器的每个工作进程可以同时接受多个新的网络连接,此指令默认为off,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个。建议设置为on
}
 
NGINx性能优化

#几位数,表明几个cpu;
worker_cpu_affinity   0000  0000 0000 0000       | auto;#auto--自动分配;版本不能太低
worker_processes,与进程相接机;

 主进程:/apps/ngnix/logs/nginx.pid
nice -19---20
#永久保存
vim /etc/security/limits.conf
*  -  nofile  99999

nginx -t 检查语法;

nginx 命令成对使用;
nginx ---nginx -s stop
使用指定的配置文件: -c
指定配置指令:-g
指定运行目录:-p
测试配置文件是否有语法错误:-t -T
打印nginx的版本信息、编译信息等:-v -V
发送信号: -s 示例: nginx -s reload

信号说明:
立刻停止服务:stop,相当于信号SIGTERM,SIGINT
优雅的停止服务:quit,相当于信号SIGQUIT   #老用户保持连接,新用户拒绝;
平滑重启,重新加载配置文件: reload,相当于信号SIGHUP
重新开始记录日志文件:reopen,相当于信号SIGUSR1,在切割日志时用途较大
平滑升级可执行程序:发送信号SIGUSR2,在升级版本时使用
优雅的停止工作进程:发送信号SIGWINCH,在升级版本时使用

短暂的长连接:
80端口长时间会自动断开,在linux断开;


3.3 HTTP配置块

多虚拟机主机的实现

  • IP
  • post
  • host 首部字段,推荐使用
#server 板块
server{
	listen 80;   #监听80
	
	server_name  locathost; #主机名
	

}


两个不同的终端:手机,电脑;--对应两个不同的网站

#是否在响应报文中的Content-Type显示指定的字符集,默认off不显示
charset charset | off;
#示例
charset utf-8;
#是否在响应报文的Server首部显示nginx版本
server_tokens on | off | build | string;
范例: 修改 Server 头部信息
如果想自定义响应报文的nginx版本信息,需要修改源码文件,重新编译



如果server_tokens on,修改 src/core/nginx.h 修改第13-14行,如下示例
#define NGINX_VERSION   "1.68.9"
#define NGINX_VER     "wanginx/" NGINX_VERSION
如果server_tokens off,修改 src/http/ngx_http_header_filter_module.c
第49行,如下示例:
static char ngx_http_server_string[] = "Server: nginx" CRLF;
把其中的nginx改为自己想要的文字即可。



[root@centos8 ~]#vim /usr/local/src/nginx-1.18.0/src/core/nginx.h
#define NGINX_VERSION   "1.68.9"
#define NGINX_VER     "wanginx/" NGINX_VERSION 
[root@centos8 ~]#vim nginx-1.18.0/src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: magedu-nginx" CRLF;





#修改头部信息;
[root@rocky8 core]# pwd
/usr/local/src/nginx-1.20.2/src/core
[root@rocky8 core]# vim /usr/local/src/nginx-1.20.2/src/core/nginx.h


#define nginx_version      1020002    
#define NGINX_VERSION      "1.20.2"    #修改这里版本
#define NGINX_VER          "nginx/" NGINX_VERSION  #名字

root@rocky8 core]# vim nginx-1.20.2/src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: magedu-nginx" CRLF;

#需要重新编译




新建网站

include /apps/nginx/conf.d/*.conf ;

#pc



server{
	listen 80  default_server;#(默认)
	server_name www.ehuo.org;

}
nginx -s reload

修改HTTP头部,增加安全功能;

3.4常见核心模块配置

charset字符集设置

前端可以配置,解决乱码。

#版本号的隐藏
Syntax:	server_tokens on | off | build | string;
Default:	server_tokens on;
Context:	http, , server location
Enables or disables emitting nginx version on error pages and in the “Server” response header field.
#启用或禁用在错误页面和“服务器”响应标头字段中发出nginx版本。


www.ehuo.org/a/b/c.html

/data/nginx/html/pc     -->   www.ehuo.org/
/data/nginx/html/pc/a/b/c.html   ---> www.ehuo.org/a/b/c.html


 

3.4.3root与alias

root:指定web的家目录,在定义location的时候,文件的绝对路径等root+location

server {
	listen 80;
	server_name www.magedu.org;
	location / {
 	root /data/nginx/html/pc;
}
location /about {
 root /opt/html; #必须要在html目录中创建一个名为about的目录才可以访问,否则报错。
}
}
[root@centos8 ~]# mkdir -p /opt/html/about
[root@centos8 ~]# echo about > /opt/html/about/index.html

#重启Nginx并访问测试

alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于location上下文,此指令使用较少

server {
	listen 80;
	server_name www.magedu.org;
	location / {
 	root /data/nginx/html/pc;
	}
	location /about { #注意about后不要加/ , 使用alias的时候uri后面如果加了斜杠,则下面的路径配置必须加斜杠,否则403
	 alias /opt/html/about; #当访问about的时候,会显示alias定义的/opt/html/about里面的内容。
	 root /opt/html/about;#根下的路径,需要在创建
	}
}


#重启Nginx并访问测试

注:location中使用root指令和alias指令的意义不同

root 给定的路径对应于location中的/uri 左侧的/
alias 给定的路径对应于location中的/uri 的完整路径
image-20220605182816186
3.4.4location(重要)

在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配,而后应用其配置

#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
=  #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求

^~  #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对uri的最左边部分做匹配检查,不区分字符大小写

~  #用于标准uri前,表示包含正则表达式,并且区分大小写

~*  #用于标准uri前,表示包含正则表达式,并且不区分大写不带符号 #匹配起始于此uri的所有的uri

\  #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号

#匹配优先级从高到低:

=, ^~, ~/~*, 不带符号

location=/logo.jpg{ #精确匹配

​ xxxxx

}

www.ehuo.org.logo,jpg

location ^- /image { #左边匹配

​ xxxxx

}

www.ehuo.org/image

www.ehuo.org/imagexxxx

location - image { #包含 区分大小写

​ xxxxx

}

www.ehuo.org/abc/aaaimage/

location -~* image { #包含 以这个开头 不区分大小写

​ xxxxx

}

www.ehuo.org/abc/aaaimage/

location /about { #不带符号 #匹配起始于此uri的所有的uri

​ XXXX

}

# cat /etc/nginx/conf.d/location.ehuo.org.conf
server {
listen 80;
server_name location.ehuo.org;
location = / {
default_type text/html;
return 200 'location = /';
}
location / {
default_type text/html;
return 200 'location /';
}
location /documents/ {
default_type text/html;
return 200 'location /documents/';
}
location ^~ /images/ {
default_type text/html;
return 200 'location ^~ /images/';
}
location ~* \.(gif|jpg|jpeg)$ {
default_type text/html;
return 200 'location ~* \.(gif|jpg|jpeg)';
}
}
#测试结果如下,建议是curl测试
#1.请求 http://location.ehuo.org/ 会被 location =/ 匹配
#2.请求 http://location.ehuo.org/index.html 会被 location / 匹配
#3.请求 http://location.ehuo.org/documents/1.html 会被 location /documents/ 匹配
#4.请求 http://location.ehuo.org/images/1.gif 会被 location ^~ /images/ 匹配
#5.请求 http://location.ehuo.org/documents/1.jpg 会被 location ~* \.
(gif|jpg|jpeg)$匹配

nginx运用location实现动静分离

location php jsp asp

location ~ \.php$ {
	[configuration E]

}

location ^~ /api/ {
	java服务器处理;
}


location ~* \.(php|jsp)$ {
	root /opt/pscite/app/;
	php php-fpm ;       #传个程序
	jsp tomcat ;

}

生产使用案例

#直接匹配网站根会加速Nginx访问处理
location = /index.html {
 ......;
}
location / {
 ......;
}
#静态资源配置方法1
location ^~ /static/ {
 ......;
}
#静态资源配置方法2,应用较多
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
 ......;
}
#多应用配置
location ~* /app1 {
  ......;
}
location ~* /app2 {
  ......;
}

Location @重定向

location @name 这样的location不用于常规请求处理,而是用于请求重定向

# cat /etc/nginx/conf.d/www.ehuo.org.conf
server {
	listen 80;
	server_name www.ehuo.org;
	root /data/www;
	location / {
	index index.html;
	}

#如果出现异常,则重新定向到@error_404这个location上
	error_page 404 @error_404;
	location @error_404 {
	default_type text/html;
	charset utf8;
	return 200 '你访问的页面可能走丢了!';
	}
}


#可以精确匹配,直接跳转到首页
error_page 404 /404.html;
    location = /404.html {
       root /data/nginx/error-pages;

   #default_type text/html;
  # charset utf8;
  # return 200 '你访问的页面可能走丢了!';                                                                                          
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ehuo_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值