【nginx】使用HTTP核心模块配置一个静态WEB服务器

来源:互联网 发布:南开大学网络教学平台 编辑:程序博客网 时间:2024/06/05 08:50

使用HTTP核心模块配置一个静态WEB服务器

1) 监听端口

listen address:port[default | default_server |[backlog=num |revbuf=size| sndbuf=size|accept_filter=filter |deferred|bind|ipv6only=[on|off] |ssl]]

 

默认listen 80,listen之后可以只加IP地址、端口、或者主机名:

listen 127.0.0.1:8000;

listen localhost;

listen 8000;

listen *:8000;

listen 443 default_server ssl;

 

2) 主机名称

server_name name[...];

默认 server_name "";

 

server_name之后可以跟多个主机名称

eg: server_name www.wowpai.top download.wowpai.top;

 

在开始处理HTTP请求时,Nginx会取出header头中的hostserver中每个server_

name进行比较如果多个server块都匹配需要按照优先级来选择处理的server

 

首先选择所有字符完全匹配的server_name www.wowpai.top

其次选择通配符在前面的server_name *.wowpai.top

其次选择通配符在后面的server_name www.wowpai.*

最后选择使用正则匹配的server_name ~^\.wowpai\.top$

 

如果以上都不能匹配将按照以下的server

优先选择在listen配置项后加入[default |default_server]server找到匹配listen端口的第一个server

 

如果server_name后面跟着字符串server_name ""表示匹配没有Host这个HTTP头部的请求

 

Nginx使用server_name配置项针对特定Host域名的请求提供不同的服务以实现虚拟主机的功能

 

3) server_names_hash_bucket_size

server_names_hash_bucket_size size;

默认 server_names_hash_bucket_size 32|64|128;

可配置块 http server location

 

为提高查找相应的server_name的能力,Nginx使用散列表来存储. size设置了每个散列块占用的字节数

 

4) server_names_hash_max_size

server_names_hash_max_size size;

默认 server_names_hash_max_size 512;

可配置块 http serverlocation

 

server_names_hash_max_size 影响散列表的冲突率,server_names_hash_max_size越大,冲突率越低,检索速度越快

 

5) 重定向主机名称的处理

server_name_in_redirect on|off;

默认 on

该配置需要配合server_name 使用在使用on打开的时候表示在重定向请求时会使用server_name里面配置的

 

第一个主机名代替原来的请求中的Host头部

当使用off关闭时,表示在重定向请求时使用请求本身的Host头部

 

7) location

location [=|~|~* |^~| @ ] | /uri/ {}

可配置块 server

 

location会尝试根据用户请求中的URI来匹配上面的/uri表达式如果可以[匹配就选择location{}块中的配置来处理用户请求

 

location的匹配规则

= 表示把URI作为字符 以便与参数中的uri做完全匹配

 

location = /{

#只有当用户请求/时 才会调用该location下的配置

}

 

~ 表示匹配URI时字母大小写敏感的

 

~* 表示匹配URI时忽略字母大小写,后面可以跟上正则表达式

location ~* \.(gif|jpg|jpeg){

#匹配这三种图片的资源请求

}

 

^~ 表示匹配URI只需要前半部分uri参数匹配即可

location ^~ /images/{

#/images/开始的请求都会匹配上

}

 

@ 表示仅用于Nginx服务内部请求之间的重定向,带有@location不直接处理用户请求没

有匹配的URI应该得到一个响应

 

location /{

#前面所有的匹配都未成功就意味着会被这个location匹配-----捕获

}

 

**location匹配的存在一定的优先级:先精确匹配然后模糊匹配,最后匹配/

 

8) 文件路径定义

1) root设置资源路径

root path;

默认 root html;

配置项: http server location if

 

location /download/ {

    root /opt/web/html/;

}

如果请求的URI/download/index/test.html那么WEB服务器应该返回的是服务器上

/opt/web/html/download/index/test.html

 

2) alias设置资源路径

alias path;

配置块:location

 

alias也是用来设置资源路径的root的不同点在于如何解读紧跟location后面的

参数。这将会致使aliasroot以不同的方式将用户的请求映射到真正的磁盘文件上

 

如果请求的URI/conf/nginx.conf实际上访问文件在/usr/local/nginx/conf/nginx.conf

location /conf{

        alias /usr/local/nginx/conf/;

}

 

location /conf{

    root /usr/local/nginx/;

}

 

使用alias时在URI向实际文件映射的过程中,已经把location后配置的/conf这部分字符串丢弃掉.最终映射成path/nginx.conf文件

 

root则不一样最终直接映射成path/conf/nginx.conf

 

3) 设置首页

index file ...;

默认 index index.html;

配置块 http server

 

location location /{

root html;

        index index.html index.htm index.php;

}

优先返回index.php,没有的话返回index.htm,如果还没有,再尝试放回index.html

 

4) 根据HTTP返回码重定向

rror_page code[code ...] [= | = answer-code] uri|@name_location

配置块 http server location if

 

如果某个请求返回错误码时匹配上了error_page中设置的code则重定向到新的URI

中。虽然重定向了URI,但返回的错误码还是和原来的相同。可以通过'='来改变返回的错误码

 

error_page 404 =200 /empty.html

如果不修改 URI,只是想让这样的请求重定向到另一个location中处理

 

location /{

    error_page 404 @failback;

}

 

location @failback{

    proxy_pass http://127.0.0.1:8081;

}

 

5) 是否允许定义error_page

 

recursive_error_pages [on|off];

默认recursive_error_pages off;

配置块 http server location

确定是否允许定义error_page

 

9) try_files

try_files path1 [path2] uri;

配置块 server location

 

try_files 后面可以跟上多个path且最后一定要跟上uri

 

按照顺序遍历每个path,如果可以有效的读取就直接返回这个path并结束请求。否则继续向后遍历,最后就重定向到uri

location /{

    #try_files $uri $uri/ /$uri.html $uri/index.html @other;

    try_files $uri $uri/ /error/php?c=404 =404;

}

location @other{

    proxy_pass http://backend;

}

 

4、对客户端请求的限制

HTTP方法名限制用户请求

limit_execpt method ...{

    ...

}

配置块 location

 

方法名有 PUT HEAD POST DELETE MKCOL COPY MOVE OPTIONS PROPFIND PROPPATCH LOCK UNLOCK PATCH

 

limit GET{

    allow 192.168.1.110/32;

    deny all;

}

禁止GET HEAD方法,其他方法允许

 

HTTP请求包体的最大值

client_max_body_size size;

默认 client_max_body_size 1m;

配置块 http server location

 

用户打算上传一个超过10G的文件,发超过定义client_max_size的值,回复错误

 

对请求的限速

imit_rate speed;

limit_rate 0;

配置块 http server location if 限制客户端请求限制每秒的传输的字节数,0表示不限制

 

server{

    if ( $slow)

    {

        set $limit_rate 4k;

        }

}

 

 

阅读全文
0 0
原创粉丝点击