东胜区建设局网站,godaddy的wordpress,建设专业网站哪家比较好,北京怀柔做网站管理运营的公司Nginx配置文件详解 1、Nginx配置文件1.1主配置文件详解1.2子配置文件 2、全局配置部分2.1修改启动的工作进程数#xff08;worker process) 优化2.2cpu与worker process绑定2.3 PID 路径修改2.4 修改工作进程的优先级2.5调试工作进程打开的文件的个数2.6关闭master-worker工作… Nginx配置文件详解 1、Nginx配置文件1.1主配置文件详解1.2子配置文件 2、全局配置部分2.1修改启动的工作进程数worker process) 优化2.2cpu与worker process绑定2.3 PID 路径修改2.4 修改工作进程的优先级2.5调试工作进程打开的文件的个数2.6关闭master-worker工作模式仅测试用 3、events部分4、http设置http部分4.1http部分详解4.2mime4.3 server 下的 root指令4.4构建虚拟主机4.4.1基于域名的虚拟主机4.4.2基于端口4.4.3基于ip地址 4.5路径别名alias4.6 location模块4.7基于四层的访问控制—access模块4.8验证模块4.8.1htpasswd命令4.8.2 配置验证模块 4.9 关闭或修改版本信息4.9.1关闭版本信息显示4.9.2 修改nginx 版本信息 4.10 自定义 错误页面4.11 日志位置存放4.12 检测文件是否存在-----try_files指令4.12.1 原理部分4.12.2 配置实例 4.13 长连接相关-keepalive指令4.13.1 原理4.13.2 keepalive_timeout4.13.3 keepalive_requests 4.14 作为下载服务器配置4.15 用户上传资料4.16 其他设置 1、Nginx配置文件
1.1主配置文件详解
主配置文件nginx.conf
编译安装的主配置文件在自己自定义的文件夹下编译安装的主文件配置在/etc/nginx/nginx.conf
//主配置文件格式main block主配置段即全局配置段对http,mail都有效//配置Nginx服务器的事件模块相关参数
events {...
}
//http/https 协议相关配置段
http {...
} //默认配置文件不包括下面两个模块
//mail 协议相关配置段
mail {...
}
//stream 服务器相关配置段
stream {负载均衡...
}vim /apps/nginx/conf/nginx.conf
//查看编译安装的nginx的配置文件1.2子配置文件 子配置文件: vim /apps/nginx/conf/nginx.conf.default 子配置文件一般在主配置文件的http部分。 http块中可以包含多个子配置文件常见的子配置文件server块用于配置HTTP服务器的具体行为包括监听的端口、虚拟主机的配置、请求处理逻辑等。location块用于指定不同URL请求的处理方式例如静态文件的服务、反向代理等。upstream块用于配置反向代理的目标服务器列表。include指令用于引入其他的子配置文件可以将一些通用的配置项单独放在一个文件中然后通过include指令引入。2、全局配置部分
2.1修改启动的工作进程数worker process) 优化 通过使用 auto 参数Nginx 可以根据系统的负载情况智能地分配工作进程以提供更好的性能和资源利用率 修改主配置文件中的 work_process项
cd /apps/nginx/conf/
vim nginx.conf 重载
nginx -s reloadps axo pid,cmd,psr,ni|grep nginx
//查看工作进程的数量2.2cpu与worker process绑定 在 Nginx 配置文件中worker_cpu_affinity 指令用于控制 worker 进程与 CPU的亲和性affinity关系 未绑定之前cpu乱跑 vim nginx.conf //进入配置文件修改以下内容
user nginx;
worker_processes auto;
worker_cpu_affinity 01 10;
#绑定到 第一 和 第二块cpu上nginx -t //检查语法#补充说明# worker_cpu_affinity 00000001 00000010; 这意味着每个 worker 进程将绑定到对应的 CPU核心上编号为 1 和 2。通过这种方式可以手动调整 worker 进程与 CPU 核心之间的关系以便更好地利用硬件资源 2.3 PID 路径修改
修改主配置文件
vim nginx.conf2.4 修改工作进程的优先级
工作进程优先级-20~20(19)
修改主配置文件
vim nginx.confuser nginx;
worker_processes 2;
worker_cpu_affinity auto;
error_log /var/log/nginx/error.log;
#添加优先级
worker_priority -20;nginx -t //查看语法是否错误
nginx -s reload //语法无误后 重载
ps axo pid,cmd,psr,ni|grep nginx|sort -n
//查看工作进程的优先级2.5调试工作进程打开的文件的个数 所有worker进程能打开的文件数量上限,包括:Nginx的所有连接例如与代理服务器的连接等而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.最好与ulimit -n 或者limits.conf的值保持一致, 先修改主配置文件
vim nginx.conf
添加
worker_rlimit_nofile 65536再修改系统默认项 方式一临时修改 ulimit -n 70000
#修改单个进程能打开的最大文件数为 70000
#仅应用于当前会话即不会永久修改限制
ulimit -a
#显示当前用户的所有资源限制信息方式二永久修改修改pam认证模块 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 8192000nproc最大进程数限制的软限制和硬限制都设置为 1000000当前用户在单个会话中可以创建的最大进程数为 1000000nofile打开文件描述符限制的软限制和硬限制都设置为 1000000这意味着当前用户在单个会话中可以使用的最大文件描述符数将被限制为 1000000。软限制是软性限制用户可以根据需要进行调整而硬限制是硬性限制一旦设定用户无法超过该限制memlock锁定内存限制的软限制和硬限制都设置为 32000这意味着当前用户在单个会话中可以锁定的最大内存量为 32000KBmsgqueue消息队列限制的软限制和硬限制都设置为 8192000这意味着当前用户在单个会话中可以使用的最大消息队列大小为 8192000字节reboot #重启生效ulimil -a //查看一次2.6关闭master-worker工作模式仅测试用
master_process off|on;
#是否开启Nginx的master-worker工作模式仅用于开发调试场景,默认为on3、events部分 在Nginx的主配置文件中events部分用于配置Nginx服务器的事件模块相关参数控制Nginx服务器在处理连接请求时的行为。 常见的events配置参数
worker_connections指定每个工作进程可以同时处理的最大连接数。multi_accept指定是否一次接受多个连接。默认情况下Nginx在每个循环中只接受一个连接但设置multi_accept为on后可以同时接受多个连接。use指定Nginx使用的事件模块。常见的事件模块有epoll、kqueue和eventport等。
events {worker_connections 65536; #设置单个工作进程的最大并发连接数use epoll;#使用epoll事件驱动Nginx支持众多的事件驱动比如:select、poll、epoll只能设置在events模块中设置。accept_mutex on; #on为同一时刻一个请求轮流由work进程处理,而防止被同时唤醒所有worker,避免多个睡眠进程被唤醒的设置默认为off新请求会唤醒所有worker进程,此过程也称为惊群因此nginx刚安装完以后要进行适当的优化。建议设置为onmulti_accept on; #ON时Nginx服务器的每个工作进程可以同时接受多个新的网络连接此指令默认为off即默认为一个工作进程只能一次接受一个新的网络连接打开后几个同时接受多个。建议设置为on
}4、http设置http部分
4.1http部分详解
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 # $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:timeout65,可以和第一个值不同#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 Apaches document root# concurs with nginxs 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 { #自定义虚拟server4.2mime 在Nginx中“mime” 是一种配置指令用于设置 MIME 类型与文件扩展名的映射关系。 vim /etc/nginx/mime.types
//查看当前Nginx服务器配置的MIME类型列表4.3 server 下的 root指令 在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文件夹中查找并提供相应的静态文件4.4构建虚拟主机
4.4.1基于域名的虚拟主机
vim /apps/nginx/conf/nginx.conf
//进入主配置文件//添加子配置文件此文件位置要写在http中
include /apps/nginx/conf.d/*.conf;cd /data
mkdir -p html/{pc,mobile}
//在/data/html 下创建两个文件夹 mobile pcrootlocalhost html]# cd mobile
[rootlocalhost mobile]#
[rootlocalhost mobile]# echo mws index.html
[rootlocalhost mobile]#
[rootlocalhost mobile]# cat index.html
mws
[rootlocalhost mobile]# cd ../pc
[rootlocalhost pc]#
[rootlocalhost pc]# echo fsj index.html
[rootlocalhost pc]#
[rootlocalhost pc]# cat index.html
fsjvim /apps/nginx/conf.d
//进入子配置文件
建立两个域名文件
vim pc.conf
server {listen 80;server_name www.fsj.comroot /data/nginx/html/pc/;
}vim m.conf
server {listen 80;server_name www.mws.comroot /data/nginx/html/mobile/;
}PC2
4.4.2基于端口
操作和上面一样只需要设置不通的端口号就行
vim /apps/ngins/conf.d/m.conf
//进入子配置文件
server{listen 192.168.91.100:8080;server_name www.mws.com;root /data/nginx/html/mobile;
} vim /apps/ngins/conf.d/pc.conf
//进入子配置文件
server{listen 192.168.91.100:80;server_name www.fsj.com;root /data/nginx/html/pc;
}[rootlocalhost 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
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]# nginx -s reloadPC2
4.4.3基于ip地址
设置一个新的网卡并配置ip地址
server{listen 192.168.91.100:server_name www.m.com;root /data/nginx/html/m;}server{listen 192.168.91.150server_name www.pc.com;root /data/nginx/html/pc;
}[rootlocalhost 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
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost conf.d]# nginx -s reload
//重新加载pc2
vim /etc/hosts
将下面的内容加入里面
192.168.190.200 www.mws.com
192.168.190.150 www.fsj.comcurl 192.168.192.200显示mws
curl 192.168.192.150显示fsj
4.5路径别名alias 在 Nginx 中alias 用于创建一个路径别名的指令。 别名可以用于将文件或目录从一个位置映射到另一个位置提供更灵活的访问控制。 服务端 //编辑子配置文件vim pc.conf使用alias
server {listen 80;server_name www.fsj.com;location /test{alias /data/html/pc/index.html;#相当于替换,访问/test/就是访问/data/html/pc/index.html;}
} [rootlocalhost conf.d]#
[rootlocalhost conf.d]#
[rootlocalhost 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
[rootlocalhost conf.d]#
[rootlocalhost conf.d]# nginx -s reload
[rootlocalhost conf.d]# PC2
curl 192.168.190.200/test4.6 location模块 在Nginx中location 是一个用于匹配请求 URL 路径的指令。它在 Nginx 配置文件中使用在不同的 location 块中定义不同的行为或处理规则。 //官方帮助文档
http://nginx.org/en/docs/http/ngx_http_core_module.html#location #语法规则
location [ | ~ | ~* | ^~ ] uri { ... } #用于标准uri前需要请求字串与uri精确匹配大小敏感,如果匹配成功就停止向下匹配并立即处理请求
^~ #用于标准uri前表示包含正则表达式,并且匹配以指定的正则表达式开头,对URI的最左边部分做匹配检查不区分字符大小写
~ #用于标准uri前表示包含正则表达式,并且区分大小写
~* #用于标准uri前表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri\ #用于标准uri前表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号#匹配优先级从高到低
, ^~, ~/~*, 不带符号例子
location / {[ configuration A ]
}
location / {[ configuration B ]
}
location /documents/ {[ configuration C ]
}
location ^~ /images/ {[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {[ configuration E ]
}访问路径是 /
访问路径是 /index.html
访问路径是 /documents/document.html
访问路径是 /images/1.gif
访问路径是 /documents/1.jpg4.7基于四层的访问控制—access模块 Nginx的access模块允许用户定义基于IP地址、请求方法、URI等条件的访问规则以控制客户端对NGINX服务器上特定资源的访问。 1IP地址访问控制允许或拒绝特定IP地址或IP地址范围的访问。 拒绝特定IP地址的访问
location / {deny 192.168.2.102;
}允许特定IP地址段的访问location / {allow 192.168.2.0/24;deny all;
}(2请求方法访问控制允许或拒绝特定HTTP请求方法如GET、POST等的访问。 仅允许GET请求
if ($request_method ! GET) {return 403;
}(3URI访问控制允许或拒绝特定URI或URI模式的访问。
拒绝特定URI的访问location /admin {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;
}4.8验证模块
4.8.1htpasswd命令
安装
yum install httpd-tools -y 常用命令//第一次生成文件
htpasswd -c 文件路径 姓名 交互式生成密码
htpasswd -bc 文件路径 姓名 密码 直接将密码跟在-c 代表新建用户名和密码对应的文件
-b 将密码跟在用户名后//非第一次生成文件
htpasswd 文件路径 姓名 交互式生成密码
htpasswd -b 文件路径 姓名 密码 直接将密码跟在后面 4.8.2 配置验证模块
#编辑配置文件
vim computer.confserver {listen 80;server_name www.byyd.com;location / {root /data/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 fsj 123456
#创建一个.htpasswd文件并添加一个使用Basic认证的用户名和密码cat /apps/nginx/conf.d/.httpuser 打开虚拟机内置浏览器
访问 192.168.192.200/admin4.9 关闭或修改版本信息
4.9.1关闭版本信息显示
cd /apps/nginx/conf/
vim nginx.conf 在http中添加
Server_tokens off保存退出后
nginx -s reload #应用修改curl -I http://192.168.2.100/
#将请求发送到IP地址为192.168.91.100的服务器上的根路径,并返回服务器的响应头信息
#响应头信息包括服务器类型、内容类型、响应日期和连接状态等。4.9.2 修改nginx 版本信息
cd /test11 #源码包所在路径
vim ./nginx-1.18.0/src/core/nginx.h将#define NGINX VERSION ”1.18.0“修改为”xxx“
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_string[] Server: xxx CRLF##然后重新编译安装 ./configure --prefix/apps/nginx --usernginx --groupnginx --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 4.10 自定义 错误页面 我们 可以改变 默认的错误页面同时也可以用指定的响应状态码进行响应, 可用位置http, server, location, if in location 格式
error_page code ... [[response]] uri;
页面错误代码
error_page 固定写法
code 响应码可以将响应码转换
uri 访问连接实际操作
www.pc.com/xxxxxxxx 404
www.pc.com/40x.html
server {listen 80;server_name www.kgc.com;root /data/nginx/html/pc;error_page 404 /40x.html;#当出现404 错误 就去 root /data/nginx/html/pc/error/ 这个文件夹找40x.html 这个文件location /40x.html {root /data/nginx/html/pc/error/;}location / {root /data/nginx/html/pc;}location /admin{auth_basic admin site;auth_basic_user_file /apps/nginx/conf.d/.httpuser;}
}
[rootlocalhost nginx]#mkdir /data/nginx/html/pc/error/
#建立目录
[rootlocalhost nginx]#cd /data/nginx/html/pc/error/
#切换目录
[rootlocalhost nginx]#vim 40x.html
#新建页面此处页面名字需要和 配置文件中的一致自定义 错误码
server {listen 80;server_name www.kgc.com;root /data/nginx/html/pc;error_page 404 302 /40x.html;#把错误码 404 指定成302 注意此处的 40x.html 需要真实存在建立的页面必须一致location /40x.html {root /data/nginx/html/pc/error/;}location / {root /data/nginx/html/pc;}location /admin{auth_basic admin site;auth_basic_user_file /apps/nginx/conf.d/.httpuser;}
}#出了错 直接跳转到显示主站点
server {listen 80;server_name www.kgc.com;root /data/nginx/html/pc;error_page 404 302 /index.html;#把错误码 404 指定成302 并跳到主页面/index.html4.11 日志位置存放 通过修改日志的路径可以实现日志分离即不同网站的日志单独存放。 mkdir /data/logs #新建存放日志的目录#编辑子配置文件指定日志存放位置
vim /apps/nginx/conf.d/mobile.confserver{listen 80;server_name www.mobile.com;root /data/nginx/m/;error_log /data/logs/m_error.log;access_log /data/logs/m_access.log;
}vim /apps/nginx/conf.d/computer.confserver{listen 80;server_name www.computer.com;root /data/nginx/pc;error_log /data/logs/pc_error.log;access_log /data/logs/pc_access.log;
}nginx -t
nginx -s reload #语法检查无误后重载4.12 检测文件是否存在-----try_files指令 Nginx 的 try_files 指令用于指定在资源文件不存在时如何处理请求。 默认开启可用于server和location部分 4.12.1 原理部分
基本语法
#方式一
try_files file ... uri;file 表示要尝试的文件路径
uri 则表示当文件不存在时转发请求的路径。#举个例子
location / {try_files $uri $uri/ /index.html /fallback.html;
}#释义
尝试找到与请求的 URI 对应的文件。
如果文件不存在则尝试在 URI 后面加上斜杠 (/) 后再查找一个文件。
如果仍然找不到文件则尝试访问 /index.html。
如果连 /index.html 也不存在则将请求转发给 /fallback.html。#方式二
try_files file ... code;code 表示文件不存在时 返回的状态码
#只会返回指定的 HTTP 响应码而不会转发请求到指定的 uri#举个例子
location / {try_files $uri $uri/ 404;
}#释义
尝试找到与请求的 URI 对应的文件。
如果文件不存在则尝试在 URI 后面加上斜杠 (/) 后再查找一个文件。
如果仍然找不到文件则返回 404 响应码。4.12.2 配置实例
服务端
#新建寻找失败跳转页面
mkdir /data/nginx/html/pc/about
echo default page
/data/nginx/html/pc/about/default.html#修改配置文件
vim /apps/nginx/conf.d/computer.conf
server{
listen 80;
server_name www.byydc.com;
root /data/nginx/pc;
location / {
root /data/nginx/html/pc;
try_files $uri $uri.html $uri/index.html
/about/default.html;}
}#重新加载
nginx -t
nginx -s reload、#建立测试文件
cd /data/nginx/html/pc/;touch test
echo find it test 客户端
curl www.byydc.com/test #查找一个存在的文件来测试 4.13 长连接相关-keepalive指令
4.13.1 原理 HTTP Keep-Alive 功能用于实现长连接允许客户端和服务器之间的 TCP 连接在发送完一个请求后保持打开状态以便在同一连接上发送多个请求和响应。 可以加在全局或者 server 。 keepalive 配置指令仅对 HTTP/1.0 和 HTTP/1.1 版本的连接有效。 对于 HTTP/2 连接keepalive 功能是默认启用的并且无需额外配置。 4.13.2 keepalive_timeout
keepalive_timeout timeout [header_timeout];
#设定保持连接超时时长keepalive_timeout 用于定义长连接超时时间。 当一个客户端与服务器之间的连接完成一个请求后的等待时间。 如果在这个时间内没有收到新的请求服务器会关闭连接。 这个时间是以秒为单位的默认值是 75 秒。 4.13.3 keepalive_requests
keepalive_requests number;
#在一次长连接上所允许请求的资源的最大数量keepalive_requests 用于设置一个连接上可以处理的最大请求数量。 当达到指定数量后服务器会关闭该连接并且客户端需要重新建立新连接。 默认情况下keepalive_requests 的值是 100。 如果将 keepalive_requests 设置为 0则表示在完成每个请求后立即关闭连接禁用了 Keep-Alive 功能。 4.14 作为下载服务器配置 ngx_http_autoindex_module 模块处理以斜杠字符 “/” 结尾的请求并生成目录列表,可以做为下载服务 配置使用
官方文档:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html[rootlocalhost nginx-1.18.0]#./configure --help |grep auto
#自带--without-http_autoindex_module disable ngx_http_autoindex_moduleautoindex on | off;
#自动文件索引功能默为off
autoindex_exact_size on | off;
#计算文件确切大小单位bytesoff 显示大概大小单位K、M)默认on
autoindex_localtime on | off ;
#显示本机时间而非GMT(格林威治)时间默认off
autoindex_format html | xml | json | jsonp;
#显示索引的页面文件风格默认html
limit_rate rate;
#限制响应客户端传输速率(除GET和HEAD以外的所有方法)单位B/s,即bytes/second默认值0,表示无限制,此指令由ngx_http_core_module提供
set $limit_rate
#变量提供 限制 变量优先级高例子实现 [rootcentos8 ~]# mkdir /opt/download
#注意:download不需要index.html文件
location /download {autoindex on;#开启下载服务器autoindex_exact_size on;#开启确切大小不建议开启autoindex_localtime on;#使用当地时间limit_rate 1024k;#所有人限速1024k默认单位是字节数set $limit_rate 2M;#谁先生效alias /opt/download;}直接访问页面
location /download {autoindex on;
}location /download {autoindex on;autoindex_exact_size on;autoindex_localtime on;limit_rate 1024k;alias /opt/download;}4.15 用户上传资料 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 4.16 其他设置 directio size | off; #操作完全和aio相反aio是读取文件而directio是写文件到磁盘启用直接I/O默认为关闭当文件大于等于给定大小时例如:directio 4m;同步直接写磁盘而非写缓存。 直接 写入 磁盘 还是等待一定数据量写入磁盘 open_file_cache off; #是否缓存打开过的文件信息
open_file_cache maxN [inactivetime];
#nginx可以缓存以下三种信息
(1) 文件元数据文件的描述符、文件大小和最近一次的修改时间
(2) 打开的目录结构
(3) 没有找到的或者没有权限访问的文件的相关信息
maxN#可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used最近最少使用)算法实现管理
inactivetime#缓存项的非活动时长在此处指定的时长内未被命中的或命中的次数少于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 max10000 inactive60s;
#最大缓存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, DELETEMKCOL, 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网段主机使用