目录
2.3 server块构 建虚拟主机 实际只有1台, 好像有多台主机
一、event事件
events {
worker_connections 65536; #设置单个工作进程的最大并发连接数
use epoll;
#使用epoll事件驱动,Nginx支持众多的事件驱动,比如:select、poll、epoll,只能设置在events模块中设置。
accept_mutex on;
#on为同一时刻一个请求轮流由work进程处理,而防止被同时唤醒所有worker,避免多个睡眠进程被唤醒的设置,默认为off,新请求会唤醒所有worker进程,此过程也称为"惊群",因此nginx刚安装完以后要进行适当的优化。建议设置为on
multi_accept on;
#ON时Nginx服务器的每个工作进程可以同时接受多个新的网络连接,此指令默认为off,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个。建议设置为on
}
小问题:一个服务端最多能接受多少客户端
服务器的硬件配置、操作系统的设置、网络带宽等。在理论上,一个服务器可以接受的最大客户端数量是有限的,这个数量通常被称为"最大并发连接数"。然而,这个数量可能会受到服务器硬件的限制,例如 CPU、内存和网络带宽的限制。此外,操作系统的设置也可能会影响这个数量,例如操作系统可能会限制一个进程可以打开的文件描述符的数量。
Nginx 是一个 HTTP 和反向代理服务器,它主要用于处理 HTTP 请求。它不能直接控制或管理设备的 MAC 地址,因为 MAC 地址是设备的物理地址,由设备的网络接口硬件决定,而不是由软件决定。
反向代理服务器是一种服务器配置,它接收客户端的请求,然后将这些请求转发给后端服务器,最后将后端服务器的响应返回给客户端。
二、 http设置
http 是一个大的语句块,包含若干个小的语句块(比如server语句块)
http {
...
... #各server的公共配置
server { #每个server用于定义一个虚拟主机,第一个server为默认虚拟服务器
...
}
server {
...
server_name #虚拟主机名
root #主目录
alias #路径别名
location [OPERATOR] URL { #指定URL的特性
...
if CONDITION {
...
}
}
}
}
http 协议配置说明
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" '
# '"$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 /fxj { www.ky31.com/fsj /apps/nginx/html
root /data;
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
3.3.1 MIME
范例: 识别php文件为text/html
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm; #指定默认网页文件,此指令由
ngx_http_index_module模块提供
# }
#}
# HTTPS server
#
#server { #https服务器配置
# 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;
# }
#}
2.1 mime
此项为支持的 文件格式,如果不支持的格式 会自动帮你下载,如果支持 就会显示在网页上
[root@localhost ~]#vim /etc/nginx/mime.types
types {
text/html html htm shtml;
.....................................................................
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
2.2 sever 下的 root
root指定了主页文件的位置
root路径格式 指定文件的路径 url
Syntax: root path;
Default:
root html;
Context: http, server, location,
指明 你软件的根目录
root在 http,server,location都可以写,可以写入哪里,可以参考nginx官网
写在server中
解释下上图
补充
也可以写在location中
检测:
root:指明根目录位置
location:根据用户访问地址的不同,指定访问的目录
如果定义了 location,但在它的下面没有写内容,相当于没写
location实际是对用户的请求做个性化设置,根据用户访问的地址不同,指定访问的目录
如果没有对location做定义目录,那么会找root目录
2.3 server块构 建虚拟主机 实际只有1台, 好像有多台主机
A 建立独立的配置文件 构建不同虚拟主机 PC端和手机端
第一种:基于不同域名的虚拟主机
[root@zzzcentos1 conf.d]#vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf.d/*.conf;
开始编写PC页面
[root@zzzcentos1 conf.d]#mkdir /data/
[root@zzzcentos1 conf.d]#mkdir /data/html
[root@zzzcentos1 conf.d]#echo /data/html > /data/html/index.html
[root@zzzcentos1 conf.d]#echo pcpcpc >> /data/html/index.html
[root@zzzcentos1 conf.d]#cat /data/html/index.html
/data/html
pcpcpc
[root@zzzcentos1 conf.d]#
开始编写手机端页面
server{
listen 80;
server_name www.m.com;
root /mnt/html/;
}
[root@zzzcentos1 conf.d]#cp pc.conf m.conf
[root@zzzcentos1 conf.d]#ls
m.conf pc.conf
[root@zzzcentos1 conf.d]#vim m.conf
server{
listen 80;
server_name www.m.com;
root /mnt/html/;
}
[root@zzzcentos1 conf.d]#mkdir /mnt/html/
[root@zzzcentos1 conf.d]#echo /mnt/html > /mnt/html/index.html
[root@zzzcentos1 conf.d]#echo mmmmm >> /mnt/html/index.html
[root@zzzcentos1 conf.d]#cat /mnt/html/index.html
/mnt/html
mmmmm
[root@zzzcentos1 conf.d]#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@zzzcentos1 conf.d]#nginx -s reload
测试:
我们用了一台主机设置了2个域名
第二种:基于不同ip地址
检测:
第三种:基于不同的端口
server{
listen 9527;
server_name www.lucky.com;
root /data/html/;
}
检测:
2.4 alias 别名
server {
listen 80;
server_name www.lucky.com;
location /news {
root /data/nginx/html/pc/;
#root 相当于追加 将文件夹news追加到/data/nginx/html/pc/news
}
location /study {
alias /mnt/nginx/sports/;
#alias 相当于替换 你访问 study 就是访问/mnt/nginx/sports
}
}
[root@zzzcentos1 conf.d]#vim pc.conf
server{
listen 80;
server_name www.lucky.com;
root /data/html/;
location /shouji {
alias /opt/;
}
}
检测:
2.5 location去匹配 追踪url
在一个server中location配置段可存在多个,用于实现从url到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,按一定的优化级找出一个最佳匹配,而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个url,uri是用户请求的字符串,即域名后面的web文件路径,然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理此请求。
可以参考网方文档内容
location实际是对用户的请求做个性化设置,根据用户访问的地址不同,指定访问的目录
如果没有对location做定义目录,那么会找root目录
location是追踪URL,找到资源所在位置
#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准url前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求
^~ #用于标准url前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对URI的最左边部分做匹配检查,不区分字符大小写
~ #用于标准url前,表示包含正则表达式,并且区分大小写
~* #用于标准url前,表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri
#\ #用于标准url前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号
正则表达式匹配:
location ~* /A.?\.jpg {
#匹配 已A后面一个或没有字符,已.jpg结尾的图片
root /opt/nginx/html/image;
}
匹配越精确,优先级越高
匹配优先级从高到低: = , ^~ , ~/~*, 不带符号
= 精确匹配
不带符号 起始于根
^~ 匹配开头的内容(不区分大小写)
~* 包含正则表达式(不区分大小写)
~ 包含正则表达式(区分大小写)
重点哦
优先级比较:参考下面解释
题目
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
先找出能匹配的项,在比较优先级
访问路径选择: 题目与答案
第一题:
第二题
第三题
第四题
第五题
总结下:
休息下,来点练习
如下请求内容,会匹配哪个Location?C
http://www.example.com/gallery/images/cat.png
A.Location /gallery {}
B.Location ~* ^/.(png|jpg)$ {}
C.Location ^~ /gallery/images {}
D.Location / {}
应用1:
局部如果没有写,按全局root来,如果局部写了,按局部来,局部优先级高
应用2:
检测
理解:相当于给root做子配置
补充
当这个里面有相同配置的文件,按照字母顺序(26字母)往下读取,第一位相同再比较第二位
2.5.1生产案例
#直接匹配网站根会加速Nginx访问处理
location = /index.html {
......;
}
location / {
......;
}
#静态资源配置方法1
location ^~ /static/ {
......;
}
#静态资源配置方法2,应用较多
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
......;
}
#多应用配置
location ~* /app1 {
......;
}
location ~* /app2 {
......;
}
理解
^~ 匹配开头的内容(不区分大小写)
匹配以STATIC开头的不分大小写
= 精确匹配
不带符号 起始于根
起始于根,无论后面跟什么都没事
~ 包含正则表达式(区分大小写)
~* 包含正则表达式(不区分大小写)
= 精确匹配
不带符号 起始于根
^~ 匹配开头的内容(不区分大小写)
~* 包含正则表达式(不区分大小写)
~ 包含正则表达式(区分大小写)
小bug
那么去修改,修改之后重新加载下
location 匹配优先级:=, ^~, ~/~*,/
2.5.2 不区分大小写
~* 用来对用户请求的uri做模糊匹配,uri中无论都是大写、都是小写或者大小写混合,此模式也都会匹配,通常使用此模式匹配用户request中的静态资源并继续做下一步操作,此方式使用较多注意: 此方式中,对于Linux文件系统上的文件仍然是区分大小写的,如果磁盘文件不存在,仍会提示404 ~*
先把之前结果删除,这样影响后续操作
虽然 不区分大小写 但是系统的文件系统区分大小写
server{
listen 80;
server_name www.lucky.com;
root /data/html/;
location ~* /A.?\.jpg {
root /opt/image/;
}
}
去检测
虽然软件不区分大小写,但是Linux系统还是区分大小写
2.5.3 location 优先级举例
都改名为1.jpg这样我们访问同一个名字1.jpg看谁先出来
server{
listen 80;
server_name www.lucky.com;
root /data/html/;
location = /1.jpg {
root /data/nginx/static1;
index index.html;
}
location /1.jpg {
root /data/nginx/static2;
index index.html;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/static3;
index index.html;
}
}
[root@zzzcentos1 image]#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@zzzcentos1 image]#nginx -s reload
[root@zzzcentos1 image]#
[root@zzzcentos1 image]#mkdir -p /data/nginx/static{1,2,3}
[root@zzzcentos1 image]#ls
a.jpg A.jpg
[root@zzzcentos1 image]#cd /data/nginx/
[root@zzzcentos1 nginx]#ls
static1 static2 static3
[root@zzzcentos1 nginx]#cd static1
[root@zzzcentos1 static1]#rz -E
rz waiting to receive.
[root@zzzcentos1 static1]#ls
微信图片_20240222152943.jpg
[root@zzzcentos1 static1]#mv 微信图片_20240222152943.jpg 1.jpg
[root@zzzcentos1 static1]#ls
1.jpg
[root@zzzcentos1 static1]#cd ../static2
[root@zzzcentos1 static2]#ls
[root@zzzcentos1 static2]#rz -E
rz waiting to receive.
[root@zzzcentos1 static2]#ls
微信图片_20240222152951.jpg
[root@zzzcentos1 static2]#mv 微信图片_20240222152951.jpg 1.jpg
[root@zzzcentos1 static2]#cd ../static3
[root@zzzcentos1 static3]#ls
[root@zzzcentos1 static3]#rz -E
rz waiting to receive.
[root@zzzcentos1 static3]#ls
微信图片_20240222152958.jpg
[root@zzzcentos1 static3]#mv 微信图片_20240222152958.jpg 1.jpg
[root@zzzcentos1 static3]#ls
1.jpg
[root@zzzcentos1 static3]#cd ..
[root@zzzcentos1 nginx]#tree
bash: tree: 未找到命令...
[root@zzzcentos1 nginx]#nginx -s reload
[root@zzzcentos1 nginx]#
验证
查看结果,是static1 按照优先级先看到等于号匹配的图片 = 优先级最高
接下来见证奇迹
删除static1后,查看结果,看到的是static3 匹配的图片 ~* 比 / 优先级高
[root@zzzcentos1 nginx]#vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.lucky.com;
root /data/html/;
location /1.jpg {
root /data/nginx/static2;
index index.html;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/static3;
index index.html;
}
}
[root@zzzcentos1 nginx]#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@zzzcentos1 nginx]#nginx -s reload
[root@zzzcentos1 nginx]#
比较 ~与~*的优先级
[root@zzzcentos1 nginx]#vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.lucky.com;
root /data/html/;
location ~ \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/static1;
index index.html;
}
location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
root /data/nginx/static3;
index index.html;
}
}
[root@zzzcentos1 nginx]#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@zzzcentos1 nginx]#nginx -s reload
~* 和 ~不分优先级,谁在前面就谁高
结束了,轻松一刻哈
2.3.6 access 模块 四层控制
如何查看模块是否默认安装
[root@zzzcentos1 nginx]#cd /data/
[root@zzzcentos1 data]#ls
html nginx
[root@zzzcentos1 data]#cd /opt/
[root@zzzcentos1 opt]#ls
image index.html nginx-1.18.0 nginx-1.18.0.tar.gz nginx-1.20.2 nginx-1.20.2.tar.gz shouji
[root@zzzcentos1 opt]#cd /opt/nginx-1.18.0/
[root@zzzcentos1 nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@zzzcentos1 nginx-1.18.0]#./configure --help|grep access
--without-http_access_module disable ngx_http_access_module
--http-log-path=PATH set http access log pathname
--without-stream_access_module disable ngx_stream_access_module
[root@zzzcentos1 nginx-1.18.0]#
--with是需要手动安装的模块;--without是默认安装的模块
拒绝真机访问
server{
listen 80;
server_name www.lucky.com;
root /data/html/;
location /test {
root /data/;
deny 192.168.246.1;
}
}
去真机验证
去虚拟机验证:
补充:
匹配了之后就不往下匹配了 ,所以范围小的往上