基于不一样的IP、不一样的端口以及不用得域名实现不一样的虚拟主机,依赖于核心模块ngx_http_core_module实现。css
[root@CentOS7 ~]#mkdir /apps/nginx/conf.d [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } } [root@CentOS7 ~]#mkdir /data/nginx/html/pc -pv mkdir: 已建立目录 "/data/nginx" mkdir: 已建立目录 "/data/nginx/html" mkdir: 已建立目录 "/data/nginx/html/pc" [root@CentOS7 ~]#echo "pc web" >>/data/nginx/html/pc/index.html [root@CentOS7 ~]#tail -2 /apps/nginx/conf/nginx.conf include /apps/nginx/conf.d/*.conf; }
[root@CentOS7 ~]#/apps/nginx/sbin/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@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
测试机添加一条主机解析 [root@CentOS-Test ~]#cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.36.104 www.darius.com 访问测试 [root@CentOS-Test ~]#curl www.darius.com pc web
root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+locationhtml
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location /about { root /data/nginx/html/pc; # #必需要在pc目录中建立一个about目录才能够访问,不然报错。 index index.html; } } [root@CentOS7 ~]#mkdir /data/nginx/html/pc/about [root@CentOS7 ~]#echo "/data/nginx/html/pc/about" >>/data/nginx/html/pc/about/index.html
重启测试linux
[root@CentOS7 ~]#/apps/nginx/sbin/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@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
访问nginx
[root@CentOS-Test ~]#curl -L www.darius.com/about /data/nginx/html/pc/about
alias:定义路径别名,会把访问的路径从新定义到其指定的路径web
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location /about { # 使用alias的时候uri后面若是加了斜杠则下面的路径配置必须加斜杠,不然403 #root /data/nginx/html/pc; alias /data/nginx/html/pc; # 当访问about的时候,会显示alias定义的/data/nginx/html/pc里面的内容。 index index.html; } } [root@CentOS7 ~]#/apps/nginx/sbin/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@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
访问测试正则表达式
[root@CentOS-Test ~]#curl -L www.darius.com/about pc web
在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri,uri是用户请求的字符串,即域名后面的web文件路径,而后使用该location模块中的正则url和字符串,若是匹配成功就结束搜索,并使用此location处理此请求。算法
语法规则: location [=|~|~*|^~] /uri/ { … } = #用于标准uri前,须要请求字串与uri精确匹配,若是匹配成功就中止向下匹配并当即处理请求。 ~ #用于标准uri前,表示包含正则表达式而且区分大小写 ~* #用于标准uri前,表示包含正则表达式而且不区分大写 !~ #用于标准uri前,表示包含正则表达式而且区分大小写不匹配 !~* #用于标准uri前,表示包含正则表达式而且不区分大小写不匹配 ^~ #用于标准uri前,表示包含正则表达式而且匹配以什么开头 $ #用于标准uri前,表示包含正则表达式而且匹配以什么结尾 \ #用于标准uri前,表示包含正则表达式而且转义字符。能够转. * ?等 * #用于标准uri前,表示包含正则表达式而且表明任意长度的任意字符
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location = /1.jpg { root /data/nginx/images; index index.html; } } [root@CentOS7 ~]#mkdir /data/nginx/images [root@CentOS7 ~]#rz -E rz waiting to receive. [root@CentOS7 ~]#mv 1.jpg /data/nginx/images/ # 上传1.jpg到/data/nginx/images [root@CentOS7 ~]#/apps/nginx/sbin/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@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload # 重启nginx
访问测试vim
[root@CentOS7 ~]#mv /data/nginx/images/1.jpg /data/nginx/images/Darius.jpg [root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location ~ /D.*\.jpg { root /data/nginx/images; index index.html; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
测试页浏览器
对用户请求的uri作模糊匹配,也就是uri中不管都是大写、都是小写或者大小写混合,此模式也都会匹配,一般使用此模式匹配用户request中的静态资源并继续作下一步操做。缓存
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; location / { root /data/nginx/html/pc; } location ~* /D.*\.jpg { root /data/nginx/images; index index.html; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
#上传一个和images目录不同内容的的图片1.j到/data/nginx/images1 location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ { root /data/nginx/images1; index index.html; } 重启Nginx并访问测试
[root@CentOS7-2 ~]#echo "images" >/data/nginx/images123/index.html [root@CentOS7-2 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location ^~ /images { root /data/nginx; index index.html; } } [root@CentOS7-2 ~]#nginx -s stop [root@CentOS7-2 ~]#nginx 访问测试(以images开头的文件,匹配location规则) [root@CentOS7-2 ~]#curl -L www.darius.com/images123 images
匹配优先级:=, ^~, ~/~*,/ location优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/) 注: 完整路径就是 /static 这样的完整的URL 。 部分就是 使用location ^~ /static 定义了开始,可是后面还有多是 /statici-mage
直接匹配网站根会加速Nginx访问处理: location = / { ......; } l ocation / { ......; } 静 态资源配置: location ^~ /static/ { ......; } # 或者 location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { ......; } 多 应用配置 location ~* /app1 { ......; } l ocation ~* /app2 { ......; }
访问控制基于模块ngx_http_access_module实现,能够经过匹配客户端源IP地址进行限制。
server { listen 80; server_name www.darius.com; error_log logs/www_darius_com.log; location / { alias /data/nginx/html/pc; index index.html; deny 192.168.36.110; allow 192.168.36.0/24; deny all; #先容许小部分,再拒绝大部分 } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
测试
[root@CentOS-Test ~]#curl www.darius.com # 拒绝192.168.36.110主机 <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.2</center> </body> </html> [root@CentOS7 ~]#curl www.darius.com # 容许192.168.36.0/24网段主机 pc web
[root@CentOS7 ~]#yum install -y httpd-tools -y [root@CentOS7 ~]#htpasswd -cbm /apps/nginx/conf.d/.htpasswd user1 123456 # 生成认证用户 Adding password for user user1 [root@CentOS7 ~]#htpasswd -bm /apps/nginx/conf.d/.htpasswd user2 123456 Adding password for user user2 [root@CentOS7 ~]#tail /apps/nginx/conf.d/.htpasswd user1:$apr1$CsaWdjbv$EwlG9UR6hEg/RYKhP0HS/1 user2:$apr1$5/nkjgHY$Sj.vqTB6M4wqapmm.jTu3.
修改配置文件
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com.log; location / { root /data/nginx/html/pc; index index.html; auth_basic "login password"; auth_basic_user_file /apps/nginx/conf.d/.htpasswd; } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload # 重启nginx服务
测试
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_page 500 502 503 504 404 /error.html; # 默认目录下建立error.html页面 error_log logs/www_darius_com_error.log; # 错误日志 access_log logs/www_darius_com_access.log; # 访问日志 location = /error.html { root html; } location / { root /data/nginx/html/pc; index index.html; } } [root@CentOS7 ~]#echo "ERROR" >/apps/nginx/html/error.html # 建立error页面 [root@CentOS7 ~]#/apps/nginx/sbin/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@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
测试
[root@CentOS-Test ~]#curl www.darius.com pc web [root@CentOS-Test ~]#curl www.darius.com/aa # 访问不存在的页面,显示定义的错误页面 ERROR
try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),若是全部文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数能够引发一个内部重定向,以前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,不然会出现内部500错误。
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf [root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location / { root /data/nginx/html/pc; index index.html; try_files $uri $uri/index.html $uri.html =489; # 重启nginx,当访问http://www.darius.com/about/xx.html等不存在的uri显示返回数据的状态码 } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
访问测试
[root@CentOS-Test ~]#curl --head www.darius.com/about/xx.html HTTP/1.1 489 # 489就是自定义的状态返回码 Server: nginx/1.14.2 Date: Thu, 30 May 2019 07:56:54 GMT Content-Length: 0 Connection: keep-alive
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf server { listen 80; server_name www.darius.com; error_log logs/www_darius_com_error.log; access_log logs/www_darius_com_access.log; location / { root /data/nginx/html/pc; index index.html; try_files $uri $uri/index.html $uri.html /about/default.html; # 重启nginx并测试,当访问到http://www.darius.com/about/xx.html等不存在的uri会显示default } } [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload [root@CentOS7 ~]#echo "default" >> /data/nginx/html/pc/about/default.html
访问测试
[root@CentOS-Test ~]#curl www.darius.com/about/xx.html default
keepalive_timeout number; #设定保持链接超时时长,0表示禁止长链接,默认为75s,一般配置在http字段做为站点全局配置 keepalive_requests number; #在一次长链接上所容许请求的资源的最大数量,默认为100次
keepalive_requests 3; keepalive_timeout 65 60; 开启长链接后,返回客户端的会话保持时间为60s,单次长链接累计请求达到指定次数请求或65秒就会被断开,后面的60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。 Keep-Alive:timeout=60 #浏览器收到的服务器返回的报文 若是设置为0表示关闭会话保持功能,将以下显示: Connection:close #浏览器收到的服务器返回的报文
location /download { autoindex on; # 自动索引功能 autoindex_exact_size on; # 计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb) autoindex_localtime on; # 显示本机时间而非GMT(格林威治)时间 limit_rate 10k; # 限制响应给客户端的传输速率,单位是bytes/second,默认值0表示无限制限速与不限速的对比 root /data/nginx/html/pc; } [root@CentOS7 ~]#/apps/nginx/sbin/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@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
建立测试页面进行测试
将光盘镜像挂载到此目录下进行测试 [root@CentOS7 ~]#mount /dev/sr0 /data/nginx/html/pc/download/ mount: /dev/sr0 写保护,将以只读方式挂载 [root@CentOS7 ~]#ll /data/nginx/html/pc/download/ 总用量 1656 -rw-rw-r-- 1 root root 14 11月 26 2018 CentOS_BuildTag drwxr-xr-x 3 root root 2048 11月 26 2018 EFI -rw-rw-r-- 1 root root 227 8月 30 2017 EULA -rw-rw-r-- 1 root root 18009 12月 10 2015 GPL drwxr-xr-x 3 root root 2048 11月 26 2018 images drwxr-xr-x 2 root root 2048 11月 26 2018 isolinux drwxr-xr-x 2 root root 2048 11月 26 2018 LiveOS drwxrwxr-x 2 root root 1656832 11月 25 2018 Packages drwxrwxr-x 2 root root 4096 11月 26 2018 repodata -rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-7 -rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7 -r--r--r-- 1 root root 2883 11月 26 2018 TRANS.TBL
测试页
client_max_body_size 1m; #设置容许客户端上传单个文件的最大值,默认值为1m 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位做为文件名: [root@s3 ~]# md5sum /data/nginx/html/pc/index.html 95f6f65f498c74938064851b1bb 96 3d 4 /data/nginx/html/pc/index.html 1级目录占1位16进制,即2^4=16个目录 0-f 2级目录占2位16进制,即2^8=256个目录 00-ff 3级目录占2位16进制,即2^8=256个目录 00-ff 配置示例: client_max_body_size 10m; client_body_buffer_size 16k; client_body_temp_path /apps/nginx/temp 1 2 2; #reload Nginx会自动建立temp目录
keepalive_disable none | browser ...; # 对哪一种浏览器禁用长链接 limit_except method ... { ... } # 限制客户端使用除了指定的请求方法以外的其它方法,仅用于location method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND,PROPPATCH, LOCK, UNLOCK, PATCH 示例: location /upload { root /data/magedu/pc; index index.html; limit_except GET { # 除了GET以外的其余请求方法,仅容许192.168.36.110主机使用 allow 192.168.36.110; deny all; } }
aio on | off #是否启用asynchronous file I/O(AIO)功能,须要编译开启 linux 2.6以上内核提供如下几个系统调用来支持aio: 一、SYS_io_setup:创建aio 的context 二、SYS_io_submit: 提交I/O操做请求 三、SYS_io_getevents:获取已完成的I/O事件 四、SYS_io_cancel:取消I/O操做请求 五、SYS_io_destroy:毁销aio的context
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_errors on | off; 是否缓存查找时发生错误的文件一类的信息 默认值为off
open_file_cache_min_uses number; open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项 默认值为1
open_file_cache_valid time; 缓存项有效性的检查验证频率,默认值为60s 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; # 缓存错误信息
server_tokens off; # 隐藏Nginx server版本 [root@CentOS7 ~]#grep "server_tokens" /apps/nginx/conf/nginx.conf server_tokens off; [root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload 访问测试 [root@CentOS-Test ~]#curl --head www.darius.com HTTP/1.1 200 OK Server: nginx # 版本号已隐藏 Date: Thu, 30 May 2019 08:27:49 GMT Content-Type: text/html Content-Length: 7 Last-Modified: Thu, 30 May 2019 03:06:03 GMT Connection: keep-alive ETag: "5cef489b-7" Accept-Ranges: bytes