静态Web服务器的主要功能由ngx_http_core_module模块(HTTP框架的主要成员)实现
除了基本配置项外,一个典型的静态Web服务器还会包含多个server块和location块
nginxhttp { gzip on; upstream { ... } ... server { listen localhost:80; location /webstatic { if ...{ ... } root /opt/webresource; ... } } }
Nginx为配置一个完整的静态Web服务器提供了非常多的功能,大致分为一下8类
经常存在多个主机域名对应着同一个IP地址的情况,nginx.conf中 可以按照 server_name 来定义虚拟主机,处理不同域名的请求
语法:listen address
默认:listen 80;
配置块:server
nginxlisten 127.0.0.1:8000; listen 127.0.0.1; # 不加端口时,默认监听80端口 listen 8000; listen *:8000; listen localhost:8000;
nginxlisten [::]:8000; listen [fe80::1]; listen [LLLa8c9:1234]:80
nginxlisten 443 default_server ssl; listen 127.0.0.1 default_server accept_filter=dataready backlog=1024;
语法:server_name name[...];
默认:server_name"";
配置块:server
nginxserver_name www.baidu.com www.taobao.com
nginx 处理一个 http 请求时,会从 header 头中取出 host,然后与每个 server 中的 server_name 进行匹配
有可能一个 host 与多个 server 块钟的 server_name 都匹配,优先级如下:
(这个优先级是带通配符散列表的实现依据)
如果Host与所有的server_name都不匹配,这时将会按下列顺序选择处理的server块:
server_name后跟着空字符串(如server_name"";),那么表示匹配没有Host这个HTTP头部的请求。
注意:Nginx正是使用server_name配置项针对特定Host域名的请求提供不同的服务,以此实现虚拟主机功能。
语法:server_names_hash_bucket_size size;
默认:server_names_hash_bucket_size 32|64|128;
配置块:http、server、location
语法:server_names_hash_max_size size;
默认:server_names_hash_max_size 512;
配置块:http、server、location
(5)重定向主机名称的处理
语法:server_name_in_redirect on|off;
默认:server_name_in_redirect on;
配置块:http、server或者location
语法:location[=||*|^~|@]/uri/{...}
配置块:server
location 会尝试根据用户请求中的URI来匹配上面的/uri表达式,如果可以匹配,就选择location{}块中的配置来处理用户请求
匹配规则:
nginxlocation = / { # 只有用户请求是 / 时,才会使用该 location 下的配置 }
nginxlocation ^~ /images/ { # 以 /images/ 开始的请求都会匹配上 }
nginxlocation /index/ { error_page 404 @index_error; } location @index_error { ..... } #以 /index/ 开头的请求,如果链接的状态为 404。则会匹配到 @index_error 这条规则上。
nginxlocation ~* \.(gif|jpg|jpeg)$ { # 匹配以 .gif .jpg .jpeg 结尾的请求 }
语法:root path;
默认:root html;
配置块:http、server、location、if
nginxlocation /download/ { root /opt/web/html/; } # 如果有一个请求的URI是/download/index/test.html,那么Web服务器将会返回服务器上 /opt/web/html/download/index/test.html 文件
语法:alias path;
配置块:location
alias 也是用来设置文件资源路径的
与root的不同点主要在于如何解读紧跟location后面的uri参数
nginxlocation /conf { alias /usr/local/nginx/conf/; }
nginxlocation /conf { root /usr/local/nginx/; }
alias 还可以添加正则表达式
nginxlocation ~ ^/test/(\w+)\.(w+)$ { alias /usr/local/nginx/$2/$1.$2; # 这样,请求在访问/test/nginx.conf时,Nginx会返回/usr/local/nginx/conf/nginx.conf文件中的内容 }
语法:index file...;
默认:index index.html;
配置块:http、server、location
这里用ngx_http_index_module模块提供的index配置实现
index后可以跟多个文件参数,Nginx将会按照顺序来访问这些文件
nginxlocation / { root path; index /index.html /html/index.php /index.php; # Nginx首先会尝试访问 path/index.php 文件 # 如果不能访问试图返回 path/html/index.php 文件的内容,依此类推 }
语法:error_page code[code...][=|=answer-code]uri|@named_location
配置块:http、server、location、if
对于某个请求返回错误码时,如果匹配上了error_page中设置的code,则重定向到新的URI中。
nginxerror_page 404 /404.html; error_page 502 503 504 /50x.html; error_page 403 http://example.com/forbidden.html; error_page 404 = @fetch;
虽然重定向了URI,但是 HTTP 错误码还是与原来的相同。
可以通过 “=” 修改错误码。
nginxerror_page 404 =200 /emty.gif; error_page 404 =403 /forbidden.gif;
还可以不指定确切的返回错误码,而是由重定向后实际处理的真实结果来决定(只写 “=”)
nginxerror_page 404 = /empty.gif;
不想修改uri,可以把请求重定向到另一个 location 中
nginxlocation / { error_page 404 @fallback; } location @fallback { proxy_pass http://backend; }
语法:recursive_error_pages[on|off];
默认:recursive_error_pages off;
配置块:http、server、location
语法:try_files path1[path2]uri;
配置块:server、location
try_files 后面跟至少一个路径,而且必须有 uri 参数
尝试按照顺序访问每一个 path,如果可以读取,返回对应文件,否则继续向下读取。如果所有的 path 都找不到有效文件,则重定向到最后的参数 uri 上
nginxtry_files /system/maintenance.html $uri $uri/index.html $uri.html @other; location @other { proxy_pass http://backend; } # 上面这段代码表示如果前面的路径,如/system/maintenance.html等,都找不到,就会反向代理到http://backend服务上
可以使用指定错误码的方式与 error_page 配合使用
nginxlocation / { try_files $uri $uri/ /error.phpc=404 =404; }
语法:client_body_in_file_only on|clean|off;
默认:client_body_in_file_only off;
配置块:http、server、location
语法:client_body_in_single_buffer on|off;
默认:client_body_in_single_buffer off;
配置块:http、server、location
语法:client_header_buffer_size size;
默认:client_header_buffer_size 1k;
配置块:http、server
语法:large_client_header_buffers number size;
默认:large_client_header_buffers 48k;
配置块:http、server
语法:client_body_buffer_size size;
默认:client_body_buffer_size 8k/16k;
配置块:http、server、location
语法:client_body_temp_path dir-path[level1[level2[level3]]]
默认:client_body_temp_path client_body_temp;
配置块:http、server、location
本文作者:Yui_HTT
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!