0. 一个Nginx静态资源Web服务器的配置文件示例:
worker_processes 1;
events {
worker_connections 1024;
}
http {
#include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" ';
gzip on;
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
server {
listen 127.0.0.1:8000;
server_name localhost;
# set $limit_rate 1k;
access_log logs/host.access.log main;
location /test/ {
alias conf/;
autoindex on;
}
location / {
root html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
}
}
1. 虚拟主机与请求的分发:
listen
用于设置监听的端口;
server_name
用于设置主机名称;
location 配置块:
location 会尝试根据用户请求中的URI来匹配上面的 /uri 表达式,如果可以匹配,就选择 location { } 块中的配置来处理用户请求。
location的匹配规则:
=
表示把URI作为字符串,以便与参数中的 uri 做完全匹配,例如:
location = / {
# 只有当用户请求是 / 时,才会使用该location下的配置
}
~
表示匹配URI时是字母大小写敏感的;~*
表示匹配URI时忽略字母大小写问题;^~
表示匹配URI时只需要其前半部分与URI参数匹配即可,例如:
location ^~ /images/ {
# 以 /images/ 开始的请求都会匹配上,例如 /images/download/
}
@
表示仅用于Nginx服务内部请求之间的重定向,带有 @ 的location不直接处理用户请求。
URI参数里是可以使用正则表达式的,例如:
location ~* \.(gif | jpg | jpeg) $ {
# 匹配以 .gif、 .jpg、 .jpeg 结尾的请求
}
2. 文件路径的定义:
root 方式设置资源路径:
location /download/ {
root /opt/web/html/;
}
# 资源文件路径:/opt/web/html/download/
如果用户请求中的 URI 参数与location中的 /uri 表达式匹配,则使用location { } 配置块处理用户请求。
root关键字可用于设定服务器上的资源文件相对于用户请求URI的路径,在上面的例子中用户请求的URI是/download/
,则Nginx会转向访问 /opt/web/html/download
路径下的资源。
alias 方式设置资源路径:
location /conf/ {
alias /usr/local/nginx/conf/;
}
# 实际访问的 : /usr/local/nginx/conf/
同root,alias关键字不会在用户请求中的URI与服务器资源路径做拼接,而是直接访问alias设定的资源路径下的文件。
比较 root 与 alias 两种方式:
root:
location /B/ {
root /A/;
}
# 实际访问:/A/B/
# B是location设置的/uri参数,
# A+B 是用户访问的文件在服务器主机上的实际位置
alias:
location /B/ {
alias /A/;
}
# 实际访问的: /A/
index 方式访问主页:
用于访问html页面。
location / {
root path;
index index.html;
}
# 收到用户请求的URI是 / 的请求后,Nginx会尝试访问 path/index.html 文件
根据HTTP返回码重定向页面:
location / {
error_page 404 /404.html; # 将404错误重定向到/404.html 文件
}
location / {
error_page 502 @fallback; # 将502错误重定向到@fallback
}
location @fallback {
proxy_pass http://backend; # 将@fallback 反向代理 到 http://backend 上有服务器中处理
}
3. gzip:对文件传输进行压缩:
对HTTP传输的文件进行 gzip
压缩,可以提升静态资源的传输效率。
http {
gzip on; # 打开gzip压缩
gzip_min_length 1; # 字节小于1的文件不进行压缩。有些报文的长度较小,在一个TCP报文中就能够发送出去,此时就没必要再对其进行压缩而浪费CPU资源了
gzip_comp_level 2; # gzip压缩等级为2
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
# gzip_types 用于设置对哪些文件进行压缩。如果某些文件已经进行过rar等压缩,就没必要再进行gzip压缩了
}
4. auto_index:生成服务器文件资源目录信息:
通过 auto_index 配置可将服务器上的某个目录文件信息分享给用户,让用户来决定访问哪些文件。
由模块 ngx_http_autoindex_module
提供功能:
ngx_http_autoindex_module 模块处理以斜杠(‘ / ’)结尾的请求,并生成目录列表。
开启方法:(location中匹配的URI参数必须要以 / 结尾)
location / {
autoindex on;
}
5. limit_rate:访问限速
通过设置 $limit_rate
变量的值来进行访问限速,当用户访问某些大文件时限制速度,以期望给某些必要的小文件分配以足够的带宽,如CSS、JSS等文件。
设置方法:
location / {
set $limit_rate 1k;
}
通过 set 命令 加 $limit_rate 变量,来限制Nginx向用户发送响应的速度(1K Bytes / s)。
在Nginx官网的 ngx_core_module
-> Embedded Variables
中提供了各种类型的内置变量,其中:
$limit_rate:
setting this variable enables response rate limiting;
Syntax: limit_rate 'rate';
Default: limit_rate 0;
Context: http, server, location, if in location
6. access_log:记录access日志:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" ';
server {
access_log logs/access.log main; # 此处使用前面定义的名为“main”格式的access_log,存储路径为logs/access.log
location / {
}
}
}
通过 log_format
配置项来设置access_log格式:
$remote_addr
:表示远端的IP地址,即浏览器客户端的IP地址;
$time_local
:表示本地时间;
$status
:表示状态码,如 200、401等;
Nginx中的各个模块如 ngx_http_core_module 等的各个内置变量都可以在 access_log 中打印出来,以及第三方模块中的$变量都可以从access_log中打印。
设定好一个log格式后要对其进行命名,例如上面的 access_log 的名字为 main
,
因为我们可能对不同域名做不同格式的日志,所以可能存在多个日志格式。