Nginx 实现web服务 小节4

125 阅读3分钟

@[TOC](Nginx 实现web服务 小节4)

ngx_http_core_module (六) (参考下面示例)

alias path;(别名)

路径别名,文档映射的另一种机制;仅能用于location上下文

示例:注意: /bbs 后建议不要加 /

http://www.a.com/bbs/index.html
location /bbs {      注意: /bbs 后建议不要加 /
   alias /web/forum/;
} --> /web/forum/index.html 
location /bbs/ {
   root /web/forum/;
} --> /web/forum/bbs/index.html
  • 注意:location中使用root指令和alias指令的意义不同
  • root,给定的路径对应于location中的/uri 左侧的/
  • alias,给定的路径对应于location中的/uri 的完整路径

示例6.1:root

nginx

[root@nginx html]# pwd
/usr/share/nginx/html
[root@nginx html]# cat /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1/;
#    location ~* \.(jpg|gif|html|txt|js|css)$ {    <--注释掉、因为比'/about'优先级高
#        root /opt/static;                               <--
#    }                                                         <--
    location ~ \.(php|jsp|asp) {
        root /opt/dynamic;
    }
    location /about {           <--访问'/about'时  (注意:'/about'后建议不要加'/')
        root /opt/testdir;      <--是访问'/opt/testdir'中的'/about'
    }                                  <--
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}


[root@nginx html]# mkdir /opt/testdir/about
[root@nginx html]# echo /opt/testdir/about/index.html > /opt/testdir/about/index.html
[root@nginx html]# nginx

centos6

[root@centos6 ~]$ curl www.a.net/about/
/opt/testdir/about/index.html

示例6.2:别名

nginx

[root@nginx html]# vim /etc/nginx/conf.d/test.conf 

charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1/;
#    location ~* \.(jpg|gif|html|txt|js|css)$ {
#        root /opt/static;
#    }
    location ~ \.(php|jsp|asp) {
        root /opt/dynamic;
    }
    location /about {
        alias /opt/testdir;    <--别名
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;

[root@nginx html]# echo /opt/testdir/index.html > /opt/testdir/index.html 
[root@nginx html]# nginx

centos6

[root@centos6 ~]$ curl www.a.net/about/
/opt/testdir/index.html

ngx_http_core_module (七) (参考下面示例)

  1. index file ...;

指定默认网页文件,此指令由ngx_http_index_module模块提供

  1. error_page code ... [=[response]] uri;

定义错误页,以指定的响应状态码进行响应

可用位置:http, server, location, if in location

示例:

error_page 404 /404.html;
    location = /40x.html {
}
error_page 404 =200 /404.html
error_page 500 502 503 504 /50x.html;
    location = /50x.html {
}
  1. 生产案例:将错误页面集中放在'/data/nginx/html'
listen 80;
server_name www.a.net; 
error_page 500 502 503 504 404 /error.html;
location = /error.html {
    root /data/nginx/html;
}

示例7.1:指定页面

nginx

[root@nginx html]# vim /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1/;
    location /about {    <--
        root /opt/testdir;  <--
        index test.html;   <--
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

#创建测试页面
[root@nginx html]# echo /opt/testdir/about/test.html > /opt/testdir/about/test.html

[root@nginx html]# nginx

centos6

测试

[root@centos6 ~]$ curl www.a.net/about/
/opt/testdir/about/test.html

示例7.2.1:错误页面

nginx

[root@nginx html]# cat /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    location /about {
        root /opt/testdir;
        index test.html;
    }
    error_page 404 /404.html;   <--只要是404、就定位到'404.html'
    location = /404.html {          <--
    }                                        <-- 
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

#创建404测试页面
[root@nginx html]# echo a.net 404.html > /data/site1/404.html 

[root@nginx html]# nginx

centos6

输入一个不存在的页面、会报错

[root@centos6 ~]$ curl www.a.net/xx.html
a.net 404.html

示例7.2.2:错误页面

nginx

[root@nginx html]# cat /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    location /about {
        root /opt/testdir;
        index test.html;
    }
    error_page 404 =200 /404.html;   <--只要是200、就定位到'404.html'
    location = /404.html {          <--
    }                                        <-- 
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

[root@nginx html]# nginx

centos6

[root@centos6 ~]$ curl -I  www.a.net/2xx.html/
HTTP/1.1 200 OK    <--200
Server: nginx
Date: Sat, 06 Aug 2022 18:41:26 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 15
Last-Modified: Sat, 06 Aug 2022 17:25:54 GMT
Connection: keep-alive
ETag: "62eea422-f"
Accept-Ranges: bytes

#返回的页面信息
[root@centos6 ~]$ curl www.a.net/2xx.html/
a.net 404.html

ngx_http_core_module (八) (参考下面示例)

try_files file ... uri;

  • try_files file ... =code;
  • 按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误
location /images/ { 
   try_files $uri /images/default.jpg;
}   说明:/images/default.jpg 为 URI
location / { 
   try_files $uri $uri/index.html $uri.html =404; 
}

示例8.1:

去$uri下面的路径'/data/images'找、找不到会返回'/images/default.jpg'

[root@nginx images]# cat /etc/nginx/conf.d/test.conf 
charset utf-8;
server_tokens off;
server {
    server_name    www.a.net;
    root    /data/site1;
    location /about {
        root /opt/testdir;
        index test.html;
    }
    location /images {                                     <--
        alias /data/images;                               <--
        try_files $uri /images/default.jpg;            <--
    }                                                              <--
    error_page 404 =200 /404.html;
    location = /404.html {
    }
}

server {
    server_name    *.a.tech;
    root    /data/site2/;
}

[root@nginx ~]# mkdir /data/images/

#鱼
[root@nginx ~]# cp /usr/share/pixmaps/faces/legacy/fish.jpg /data/images/a.jpg
#咖啡
[root@nginx ~]# cp /usr/share/pixmaps/faces/coffee.jpg /data/images/b.jpg
#天空
[root@nginx ~]# cp /usr/share/pixmaps/faces/legacy/sky.jpg /data/images/default.jpg

[root@nginx ~]# nginx

windows

测试

修改'C:\Windows\System32\drivers\etc\hosts' -- 添加'192.168.37.7 www.a.net' -- 保存

图片.png

图片.png

图片.png

ngx_http_core_module (九)

  1. 定义客户端请求的相关配置
  2. keepalive_timeout timeout [header_timeout];

设定保持连接超时时长,0表示禁止长连接,默认为75s

示例:在响应头显示此首部字段

keepalive_timeout 60 60;

  1. keepalive_requests number;

在一次长连接上所允许请求的资源的最大数量,默认为100

  1. keepalive_disable none | browser ...;

对哪种浏览器禁用长连接

  1. send_timeout time;

向客户端发送响应报文的超时时长,此处是指两次写操作之间的间隔时长,而非整个响应过程的传输时长

示例: nginx

[root@nginx ~]# vim /etc/nginx/nginx.conf
...
    tcp_nodelay         on;
    keepalive_timeout   65 65;   <--此处结尾再添加'65'值、告诉用户。这个65也可以说假的。这个值是多少完全取决第一个数字
    types_hash_max_size 4096;
...

[root@nginx ~]# nginx

centos6

[root@centos6 ~]$ curl -I www.a.net/
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 07 Aug 2022 05:00:34 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 23
Last-Modified: Sat, 06 Aug 2022 07:35:39 GMT
Connection: keep-alive
Keep-Alive: timeout=65      <--用户可以看到长链接时长
ETag: "62ee19cb-17"
Accept-Ranges: bytes

ngx_http_core_module

  1. client_max_body_size size;

指定请求报文中实体的最大值,设为0,则不限制,默认1m,超过报413错误

  1. client_body_buffer_size size;

用于接收每个客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大 小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置

  1. client_body_temp_path path [level1 [level2 [level3]]];

设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量

目录名为16进制的数字;用hash之后的值从后往前截取第1、2、3级作为文件名

client_body_temp_path /var/tmp/client_body 1 2 2

1 1级目录占1位16进制,即2^4=16个目录 0-f

2 2级目录占2位16进制,即2^8=256个目录 00-ff

2 3级目录占2位16进制,即2^8=256个目录 00-ff