一、I/O模型
二、nginx概述 官网:http://nginx.org 2.1 、nginx介绍 nginx: engine X,是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的, 开发工作最早从2002年开始,第一次公开发布时间是2004年10月4日,版本号是0.1.0 nginx有商业版和社区版,2019年3月11日F5 Networks 6.7亿美元的价格收购 nginx 是免费,开源,高性能的HTTP和反向代理服务器,邮件代理服务器,通用TCP/UDP代理服务器 解决apchace C10K问题(10K Connections)(nginx和apache使用的I/O模型实现方法不同) nginx的其它的二次发行版: Tengine:由淘宝网发起的Web服务器项目从2011年12月开始,Tengine成为一个开源项目,官网 http://tengine.taobao.org/ OpenResty:基于 Nginx 与 Lua 语言的高性能 Web 平台,官网:http://openresty.org/cn/[root@CentOS7 yum.repos.d]# curl -I www.taobao.com HTTP/1.1 301 Moved Permanently Server: Tengine Date: Thu, 21 Jan 2021 02:30:50 GMT Content-Type: text/html Content-Length: 278 Connection: keep-alive Location: https://www.taobao.com/ Via: cache3.cn2147[,0] Timing-Allow-Origin: * EagleId: 1bd3c51716111962506912372e [root@CentOS7 yum.repos.d]# curl -I -A IE www.sohu.com HTTP/1.1 307 Temporary Redirect Content-Type: text/html Content-Length: 180 Connection: keep-alive Server: nginx Date: Thu, 21 Jan 2021 02:35:13 GMT Location: https://www.sohu.com/ FSS-Cache: from 4242652.6733030.5335298 FSS-Proxy: Powered by 2538690.3325132.3631310 [root@CentOS7 yum.repos.d]# curl -I -A ie http://openresty.org/cn/ HTTP/1.1 200 OK Date: Thu, 21 Jan 2021 02:32:57 GMT Content-Type: text/html; charset=utf-8 Connection: keep-alive Set-Cookie: userid=00002a0000042dfb6ec851b0; Expires=Fri, 21-Jan-22 02:32:57 GMT; Max-Age=31536000; Domain=openresty.org Vary: Accept-Encoding Last-Modified: Mon, 10 Aug 2020 12:53:59 GMT Cache-Control: max-age=3600 Cache-Status: HIT Server: OpenResty Edge 2 Req-ID: 00002a0000042dfb6ec851b0 Edge-Cache-Age: 3627 Cache-Status: EXPIRED2.2 、nginx基本功能 基本功能:
- 静态资源的web服务器 (html,图片,js,css,txt等静态资源)
- http协议反向代理服务器
- FastCGI(LNMP),uWSGI(python),SCGI等协议反向代理动态资源请求
- pop3/imap4协议反向代理服务器
- tcp/udp协议的请求转发(反向代理)
- 模块化(非DSO),如zip,SSL模块 (nginx1.9.11版本后开始支持DSO-动态装载和卸载)
- 虚拟主机(server)
- 支持 keep-alive 和管道连接( 共享TCP连接发起并发的HTTP请求)(一个TCP连接中允许并发HTTP请求)
- 访问日志(支持基于日志缓冲提高其性能)
- url 重定向 rewrite
- 路径别名
- 基于IP及用户的访问控制
- 支持速率限制及并发数限制
- 重新配置和在线升级而无须中断客户的工作进程
- Memcached 的 GET 接口
- 模块化设计,较好的扩展性
- 高可靠性
- 支持热部署:不停机更新配置文件,升级版本,更换日志文件(实际中,作为web服务器,升级版本,更多的是通过前端调度器策略灰度升级)
- 低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存
- event-driven,aio,mmap,sendfile
2.4 、nginx程序架构
nginx的程序架构: master/worker结构:只有进程没有线程 一个master进程:负载加载和分析配置文件、管理worker进程、平滑升级 一个或多个worker进程:处理并响应用户请求 缓存相关的进程: cache loader:载入缓存对象 cache manager:管理缓存对象 2.5 、nginx模块 nginx高度模块化,但其模块早期不支持DSO机制;1.9.11版本支持动态装载和卸载 模块分类: 核心模块:core module (编译安装不用额外指定) 标准模块:3大类 (编译安装需额外指定 ) HTTP 模块: ngx_http_* HTTP Core modules 默认功能 HTTP Optional modules 需编译时指定 Mail 模块: ngx_mail_* Stream 模块 :ngx_stream_* 第三方模块:ngx_google_perftools_module 官网模块介绍:http://nginx.org/en/docs/ 核心默默:是nginx服务器正常运行不可或缺的的模块,提供错误日志记录、配置文件解析、事件驱动机制、远程管理等核心功能。官网: 标准HTTP模块:提供http协议解析相关的功能,例如:端口配置 、 网页编码设置 、 HTTP响应头设置 等等, 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如: Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、IMAP 协议和 SMTP协议的支持 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如:Json 支持、 Lua 支持等 三、安装 3.1安装方式: 1)官方yum源安装: http://nginx.org/packages/centos/7/x86_64/ 2)epel源安装:https://mirrors.aliyun.com/epel/7/x86_64/ 3)编译安装:官方下载源码包 示例:编译安装
[[email protected] opt]# wget http://nginx.org/download/nginx-1.18.0.tar.gz #获取源码包 [[email protected] opt]# tar xf nginx-1.18.0.tar.gz [[email protected] opt]# yum install gcc pcre-devel openssl-devel zlib-devel #安装依赖 [[email protected] nginx-1.18.0]# getent passwd nginx [[email protected] nginx-1.18.0]# useradd -r -s /sbin/nologin nginx #建议统一uid,gid [[email protected] nginx-1.18.0]# getent passwd nginx nginx:x:987:981::/home/nginx:/sbin/nologin [[email protected] nginx]# ./configure --help #查看configure
[[email protected] nginx]# ./configure \ #编译 --prefix=/apps/nginx \ --conf-path=/apps/nginx/conf/nginx.conf \ --error-log-path=/apps/nginx/log/nginx/error.log \ --http-log-path=/apps/nginx/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_dav_module \ --with-http_stub_status_module \ --with-threads \ --with-file-aio [[email protected] nginx-1.18.0]# echo $? 0 [[email protected] nginx-1.18.0]# make -j 2 && make #安装 [[email protected] nginx-1.18.0]# echo $? 0
[[email protected] nginx]# tree /apps/nginx/ #自动生成该目录 /apps/nginx/ ├── conf │ ├── fastcgi.conf │ ├── fastcgi.conf.default │ ├── fastcgi_params │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf │ ├── nginx.conf.default │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── html │ ├── 50x.html │ └── index.html ├── log │ └── nginx └── sbin └── nginx [[email protected] nginx]# ln -s /apps/nginx/sbin/nginx /usr/sbin/ #创建软连接,简介环境变量 [[email protected] nginx]# ll /usr/sbin/nginx lrwxrwxrwx 1 root root 22 Jan 21 13:16 /usr/sbin/nginx -> /apps/nginx/sbin/nginx
[[email protected] nginx]# nginx #启动服务;yum安装也支持此启动方式,但yum安装中systemctl启动服务管理无法管理此种启动方式管理的服务状态
nginx命令说明:
[[email protected] nginx]# nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit #检查配置文件语法
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /apps/nginx/)
-c filename : set configuration file (default: /apps/nginx/conf/nginx.conf)
-g directives : set global directives out of configuration file
[[email protected] nginx]# nginx -V #查看编译安装的预编译配置
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments:
--prefix=/apps/nginx
--conf-path=/apps/nginx/conf/nginx.conf
--error-log-path=/apps/nginx/log/nginx/error.log
--http-log-path=/apps/nginx/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_v2_module
--with-http_dav_module
--with-http_stub_status_module
--with-threads
--with-file-aio
[[email protected] nginx]# nginx -v #nginx版本
nginx version: nginx/1.18.0
3.2、nginx目录结构
四、性能优化(全局)
4.1 worker_processes number | auto; 默认: auto worker进程的数量;通常应该为当前主机的cpu的物理核心数 Context: main 示例:[[email protected] conf]# vi /apps/nginx/conf/nginx.conf worker_processes 2; [[email protected] conf]# ps -aux|grep nginx root 8268 0.0 0.2 46144 2028 ? Ss 14:15 0:00 nginx: master process nginx nginx 8359 0.0 0.2 48664 2132 ? S 14:18 0:00 nginx: worker process nginx 8360 0.0 0.2 48664 2384 ? S 14:18 0:00 nginx: worker process
4.2、worker_cpu_affinity cpumask ...;
默认:不绑定 worker进程和cpu核心绑定 Context: main worker_cpu_affinity auto [cpumask] 提高缓存命中率 CPU MASK: 00000001:0号CPU 00000010:1号CPU 10000000:7号CPU worker_cpu_affinity 0001 0010 0100 1000; worker_cpu_affinity 0101 1010; 示例:[[email protected] conf]# vi /apps/nginx/conf/nginx.conf worker_cpu_affinity 0001 0010; [[email protected] conf]# ps axo user,pid,cmd,psr|grep nginx root 7653 nginx: master process nginx 2 nginx 9062 nginx: worker process 0 nginx 9063 nginx: worker process 1
4.3、worker_priority number;
默认: 19 指定worker进程的nice值,设定worker进程优先级:[-20,20] nice [-20,19] Context: main 示例:[[email protected] conf]# ps axo user,pid,cmd,pri|grep nginx root 7653 nginx: master process nginx 19 nginx 9062 nginx: worker process 19 nginx 9063 nginx: worker process 19 [[email protected] conf]# vi /apps/nginx/conf/nginx.conf worker_priority 6; [[email protected] conf]# ps axo user,pid,cmd,pri|grep nginx root 7653 nginx: master process nginx 19 nginx 9357 nginx: worker process 13 nginx 9358 nginx: worker process 13
4.4、worker_rlimit_nofile number;
Context: main 所有worker进程能打开的文件数量上限,最好与ulimit -n 的值保持一致,如65535 示例:[[email protected] apps]# tail -f nginx/log/nginx/error.log #观察日志输出 2021/02/18 15:06:40 [crit] 7655#7655: accept4() failed (24: Too many open files) [root@client log]# ab -c 2000 -n 20000 10.0.0.126/index.html [[email protected] conf]# ulimit -a [[email protected] conf]# ulimit -n 66666 #临时有效;永久有效:/etc/security/limits.conf中的nofile [[email protected] conf]# vi /apps/nginx/conf/nginx.conf worker_rlimit_nofile 66666; #并不是越大越好,要考虑服务器性能;虽然nginx的epoll模型没有打开文件的上限,但是当页面很大时,也会收到影响,建议增加内核的ulimit -n [[email protected] conf]# nginx -s reload [root@client log]# ab -c 2000 -n 20000 10.0.0.126/index.html
4.5、事件驱动相关的配置
events {
... } 4.5.1、worker_connections #; Default: worker_connections 512;
Context: events
每个worker进程所能够打开的最大并发连接数,如10240,不是越大越好
总最大并发数:worker_processes * worker_connections 4.5.2、use method; Default:使用最适合的模型,linux-nginx:epoll;windows-nginx: select Context: events 指明并发连接请求的处理方法,默认自动选择最优方法 示例:use epoll; 4.5.3、accept_mutex on | off; Default: accept_mutex off; Context: events 处理新的连接请求的方法; on指由各个worker轮流处理新请求, Off指每个新请求的到达都会通知(唤醒)所有的worker进程,但只有一个进程可获得连接,造成“惊群”,影响性能, 默认值为off,可优化为on 4.5.4、multi_accept on|off; Default: multi_accept off; Context: events 此指令默认为off,即默认为一个worker进程只能一次接受一个新的网络连接, on表示每个woker进程可以同时接受所有新的网络连接(并发性)4.6、调试和定位问题
4.6.1、daemon on|off; Default: daemon on;Context: main 是否以守护进程方式运行, 默认是on,即守护进程方式,off 用于调试或docker环境 示例:
[[email protected] security]# vim /apps/nginx/conf/nginx.conf daemon off; [[email protected] security]# nginx #前台运行4.6.2、master_process on|off;
Default: master_process on;
Context: main
默认: [[email protected] ~]# ps aux|grep nginx root 8155 0.0 0.3 46004 3372 pts/0 S+ 21:04 0:00 nginx: master process nginx nginx 8156 0.0 0.2 48544 2016 pts/0 SN+ 21:04 0:00 nginx: worker process nginx 8157 0.0 0.2 48544 2016 pts/0 SN+ 21:04 0:00 nginx: worker process 修改: [[email protected] securivim /apps/nginx/conf/nginx.conf master_process off; #不建议off [[email protected] ~]# ps aux|grep nginx root 8202 0.0 0.3 46456 3632 pts/0 S+ 21:05 0:00 nginx4.6.3、error_log file [level] ;
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location
设置某个日志级别将导致记录指定和更严重日志级别的所有消息。
例如,默认级别错误将导致记录error, crit, alert, 和 emerg消息。如果省略此参数,则使用错误。
示例:
nginx-rpm: [root@CentOS7 ~]# vi /etc/nginx/nginx.conf error_log /var/log/nginx/error.log; [root@CentOS7 ~]# nginx -V --error-log-path=/var/log/nginx/error.log nginx-编译: [[email protected] nginx]# nginx -V --error-log-path=/apps/nginx/log/nginx/error.log
五、nginx配置之http协议段相关配置
http协议的相关配置: http { ... ... server { ... server_name root location [OPERATOR] /uri/ { ... } } server { ... } } 5.1、ngx_http_core_module 5.1.1 在响应报文中将指定的文件扩展名映射至MIME对应的类型:include /etc/nginx/mime.types; #nginx-rpm,Context: http, server, location default_type application/octet-stream; 除上面指定的类型外,就为默认的MIME类型,浏览器一般会提示下载(Default: default_type text/plain;Context: http, server, location) MIME参考文档: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types 示例:
[[email protected] conf]# vi /apps/nginx/conf/mime.types types { text/html html htm shtml; text/css css; text/xml xml; image/gif gif; ... } 类型/子类型 文件后缀 text/html html htm shtml;
[[email protected] conf]# vim /apps/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; #在mime.types没有可支持的媒体类型时,指定此默认类型(下载) ... } [[email protected] conf]# cp /etc/fstab /apps/nginx/html/test.xyz #mime.types没有".xyz"媒体类型 [[email protected] conf]# curl -I http://10.0.0.126/test.xyz #观察媒体类型 HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Sun, 21 Feb 2021 08:57:14 GMT Content-Type: application/octet-stream Content-Length: 737 Last-Modified: Sun, 21 Feb 2021 08:55:46 GMT Connection: keep-alive ETag: "60322012-2e1" Accept-Ranges: bytes
5.1.2、tcp_nodelay on | off;
在keepalived模式下的连接是否启用TCP_NODELAY选项,即Nagle算法 当为off时,延迟发送,每发送一个包就需要确认ACK,才发送下一个包 默认On时,不延迟发送,多个包才确认一次 Default: tcp_nodelay on;
Context: http, server, location
Default: tcp_nopush off;
Context: http, server, location
Default: sendfile off;
Context: http, server, location, if in location
[[email protected] conf]# curl -I http://10.0.0.126 HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Sun, 21 Feb 2021 09:22:47 GMT Content-Type: text/html Content-Length: 11 Last-Modified: Thu, 18 Feb 2021 06:20:57 GMT Connection: keep-alive ETag: "602e0749-b" Accept-Ranges: bytes [[email protected] conf]# vim /apps/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; charset utf-8;5.1.6、server_tokens on | off | build | string; 是否在响应报文的Server首部显示nginx版本
#include /apps/nginx/conf/conf.d/*.conf; #也可以单独设置配置文件 ... } [[email protected] conf]# curl -I http://10.0.0.126 HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Sun, 21 Feb 2021 09:24:39 GMT Content-Type: text/html; charset=utf-8 Content-Length: 11 Last-Modified: Thu, 18 Feb 2021 06:20:57 GMT Connection: keep-alive ETag: "602e0749-b" Accept-Ranges: bytes
Default: server_tokens on;
Context: http, server, location
自定义nginx版本信息:
如果想自定义响应报文的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改为自己想要的文字即可,如:wanginx 5.1.7、与套接字相关的配置: 5.1.7.1、server { ... } 配置一个虚拟主机 server { listen address[:PORT]|PORT; server_name SERVER_NAME; root /PATH/TO/DOCUMENT_ROOT; } Context: http,也可以在其他功能模块 listen PORT | address[:port] | unix:/PATH/TO/SOCKET_FILE listen 端口 | IP:[端口] | 套接字(只允许本地访问) listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]; default_server 设定为默认虚拟主机,无法匹配虚拟主机时使用 ssl 限制仅能够通过ssl连接提供服务 backlog=number 超过并发连接数后,新请求进入后援队列的长度 rcvbuf=size 接收缓冲区大小 sndbuf=size 发送缓冲区大小 虚拟主机配置方式: (1) 基于port; listen PORT; 指令监听在不同的端口 (2) 基于ip的虚拟主机 listen IP:PORT; IP 地址不同 (3) 基于hostname server_name fqdn; 指令指向不同的主机名 server_name name ...; 虚拟主机的主机名称后可跟多个由空白字符分隔的字符串 支持*通配任意长度的任意字符 server_name *.magedu.com www.magedu.* 支持~起始的字符做正则表达式模式匹配,性能原因慎用 server_name ~^www\d+\.magedu\.com$ 说明: \d 表示 [0-9] 匹配优先级机制从高到低 (1) 首先是字符串精确匹配 如:www.magedu.com (2) 左侧*通配符 如:*.magedu.com (3) 右侧*通配符 如:www.magedu.* (4) 正则表达式 如: ~^.*\.magedu\.com$ (5) default_server 定义路径相关的配置: root 设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径, Context :http, server, location, if in location server { ... root /data/www/vhost1; } 示例: http://www.magedu.com/images/logo.jpg --> /data/www/vhosts/images/logo.jpg[[email protected] conf]# vim /apps/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; include /apps/nginx/conf/conf.d/*.conf; #另规划配置文件 ... } 基于server_name创建2个虚拟站点: [[email protected] conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; } server { listen 80; server_name www.magedu.tech; root /data/site2/; } 本地hosts映射: [[email protected] conf]# cat /etc/hosts 10.0.0.126 www.magedu.net www.magedu.tech 创建虚拟站点根目录: [[email protected] conf]# mkdir /data/site1 [[email protected] conf]# mkdir /data/site2 [[email protected] conf]# echo /data/site1/index.html >//data/site1/index.html [[email protected] conf]# echo /data/site2/index.html >//data/site2/index.html 验证: [[email protected] conf]# curl http://www.magedu.net /data/site1/index.html [[email protected] conf]# curl http://www.magedu.tech /data/site2/index.html
5.1.7.2、location [ = | ~ | ~* | ^~ ] uri { ... }
Default: —
Context: server, location
简单location设置: [[email protected] conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /test { root /opt/testdir ; } } 创建虚拟站点根目录: [[email protected] conf]# mkdir /opt/testdir/test [[email protected] conf]# echo /opt/testdir/test/index.html > /opt/testdir/test/index.html 验证: [[email protected] conf]# curl http://10.0.0.126/test/ /opt/testdir/test/index.html= 对URI做精确匹配; location = / { ... } http://www.magedu.com/ 匹配 http://www.magedu.com/index.html 不匹配 ^~ : 对URI的最左边部分做匹配检查,不区分字符大小写 ~ : 对URI做正则表达式模式匹配,区分字符大小写 ~* : 对URI做正则表达式模式匹配,不区分字符大小写(nginx不区分大小写,但是文件系统还是区分大小写,最后还是区分大小写) 不带符号: 匹配起始于此uri的所有的uri \ 转义符: 可将 . * ?等转义为普通符号 匹配优先级从高到低: =, ^~, ~/~* , 不带符号
location设置: [[email protected] conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /test/index.html { root /opt/testdir; } location = /test/index.html { root /opt/testdir2; } } 创建虚拟站点根目录: [[email protected] conf]# mkdir /opt/testdir/test [[email protected] conf]# echo /opt/testdir/test/index.html > /opt/testdir/test/index.html [[email protected] conf]# mkdir /opt/testdir2/test [[email protected] conf]# echo /opt/testdir2/test/index.html > /opt/testdir2/test/index.html 验证: [[email protected] conf]# curl http://10.0.0.126/test/index.html /opt/testdir2/test/index.html [[email protected] conf]# curl http://10.0.0.126/test #location都没匹配上,找/data/site1/下的test/index.html,没有找到 <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx</center> </body> </html>
location模拟动静资源分离:
location设置: [[email protected] conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location ~* \.(jpg|gif|html|txt|js|css)$ { root /opt/static; } location ~ \.(php|jsp|asp)$ { root /opt/dynamic; } } 创建虚拟站点根目录: [[email protected] conf]# mkdir /opt/{static,dynamic} -p [[email protected] conf]# echo /opt/static/index.html >/opt/static/index.html [[email protected] conf]# echo /opt/dynamic/index.php >/opt/dynamic/index.php 验证: [[email protected] conf]# curl http://10.0.0.126/index.html /opt/static/index.html [[email protected] conf]# curl http://10.0.0.126/index.php /opt/dynamic/index.php
alias path;
Context: location 路径别名,文档映射的另一种机制;仅能用于location上下文 示例: http://www.magedu.com/bbs/index.html location /bbs { 注意: /bbs 后建议不要加 / alias /web/forum/; } --> /web/forum/index.html location /bbs/ { root /web/forum/; } --> /web/forum/bbs/index.html root 指定虚拟主机根目录,在定义location时,文件的绝对路径等于 root+location 注意:location中使用root指令和alias指令的意义不同 (a) root,给定的路径对应于location中的/uri 左侧的/ (b) alias,给定的路径对应于location中的/uri 的完整路径,location完全置换为root(Default: root html; Context: http, server, location, if in location) 示例:[[email protected] conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /about { root /opt/testdir; } } 创建虚拟站点根目录: [[email protected] conf]# mkdir /opt/testdir/about [[email protected] conf]# echo /opt/testdir/about/index.html > /opt/testdir/about/index.html 验证: [[email protected] conf]# curl http://10.0.0.126/about/ /opt/testdir/about/index.html -----------------------------alias-------------------------------------- [[email protected] conf]# vim /apps/nginx/conf/conf.d/test.conf server { listen 80; server_name www.magedu.net; root /data/site1/; location /about { alias /opt/testdir; } } 创建虚拟站点根目录: [[email protected] conf]# echo /opt/testdir/index.html >/opt/testdir/index.html 验证: [[email protected] conf]# curl http://10.0.0.126/about/ /opt/testdir/index.html
5.1.8、
error_page code ... [=[response]] uri; 定义错误页,以指定的响应状态码进行响应 Context:http, server, location, if in location 示例: error_page 404 /404.html; location = /40x.html { } error_page 404 =200 /404.html; #=200是404的返回状态变为200,防止错误劫持 error_page 500 502 503 504 /50x.html; location = /50x.html { } 示例:[[email protected] conf.d]# /etc/nginx/conf.d/test.conf server { listen 80; server_name www.magedu.tech; root /opt/testdir/; location /about {
root /opt/testdir/; } error_page 404 =200 /404.html; #防止错误劫持 location = /test.html { } } [[email protected] conf.d]# nginx -s reload [[email protected] conf.d]# nginx
5.1.9、try_files
try_files file ... uri; try_files file ... =code; 按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向 location /images/ { try_files $uri /images/default.jpg; } 说明:/images/default.jpg 为 URI location / { try_files $uri $uri/index.html $uri.html =404; }示例:
[[email protected] conf.d]# /etc/nginx/conf.d/test.conf server { listen 80; server_name www.magedu.tech; root /opt/testdir/; location /image { alias /opt/image/; try_files $uri $uri/default.jpg =404; } error_page 404 =200 /404.html; location = /test.html { } } [[email protected] conf.d]# nginx -s reload#default.jpg
[[email protected] conf.d]# ls /opt/image/
a.jpg default.jpg
#a.jpg
#404
标签:http,nginx,html,conf,root,centos7.6 From: https://www.cnblogs.com/cnblogsfc/p/14303528.html