分享一个通过 nginx[1] 搭建一个静态资源的 web 服务器,实践中将会涉及到三个部分的内容:
Embedded Variables
简单的就通过一个自定义的index.html
文件当做一个静态页面Demo,下面准备一下静态文件资源存储的位置。
mkdir /webserver/nginx18/ops/
echo "云原生生态圈" > /webserver/nginx18/ops/index.html
增加nginx配置文件,在/webserver/nginx18/conf/nginx.conf
里增加一个 server 配置段,然后通过 alias 映射到虚拟目录。注意访问时 location 中的/
对应的是 ops 后面的/
server {
listen 80;
server_name web.devopsman.cn;
location / {
alias ops/;
}
}
测试访问:
☸️ k8sdev? default ~ ? ? curl web.devopsman.cn
静态网站demo
☸️ k8sdev? default ~ ? ? curl -I web.devopsman.cn
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 11 Jul 2020 14:11:46 GMT
Content-Type: text/html
Content-Length: 17
Last-Modified: Sat, 11 Jul 2020 14:11:13 GMT
Connection: keep-alive
ETag: "5f09c881-11"
Accept-Ranges: bytes
nginx可以通过一种浏览器支持且压缩不受数据损失的gzip模块将样式文件、文本文件、图片等压缩之后在用户的请求之间传输,但是对于jpg、jpeg、png
这类本身存在压缩性质上的图片来说,压缩效果不是很明显,主要体现在文本文件的压缩上,因此对于服务的提供者来说,减少了不小的流量带宽的使用,同时也提高访问速度。
gzip on;
gzip_min_length 1; # 表示小于1k不压缩
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; # 启用压缩类型的文件
gzip_comp_level
的压缩比设置区间为0-9
,设置的压缩比越高,文件越小,但是对服务器CPU的消耗也就越多,一般出于这种选择压缩比为6
。
root@master:/webserver/nginx18/conf# curl -I http://web.devopsman.cn/test.html
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sun, 12 Jul 2020 13:45:22 GMT
Content-Type: text/html
Content-Length: 20480
Last-Modified: Sun, 12 Jul 2020 13:34:14 GMT
Connection: keep-alive
ETag: "5f0b1156-5000"
Accept-Ranges: bytes
root@master:/webserver/nginx18/conf# curl -H "Accept-Encoding: gzip" -I http://web.devopsman.cn/test.html
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sun, 12 Jul 2020 13:45:26 GMT
Content-Type: text/html
Last-Modified: Sun, 12 Jul 2020 13:34:14 GMT
Connection: keep-alive
ETag: W/"5f0b1156-5000"
Content-Encoding: gzip
通过以上测试的方式,既可以访问gzip_types
中设定的类型的资源的时候,除了图片之外,应该能成功的找到Content-Encoding: gzip
。
nginx 的日志格式,是通过 nginx 支持的环境变量将所需要的日志信息给组装在一起,在 nginx 里,除了内置的环境变量之外,还包括第三方模块的环境变量信息,比如,nginx 核心模块的支持的环境变量可以在Embedded Variables[3]找到,第三方支持哪些环境变量可以在对应的模块文档中找到,比如realip
模块支持的环境变量Embedded Variables[4],下面是是一个定义的案例,定义一个main
类型的日志格式。
log_format main '{'
'"@timestamp":"$time_iso8601",'
'"connection":"$connection",'
'"connection_requests":"$connection_requests",'
'"request":"$request",'
'"request_time":"$request_time",'
'"request_length":"$request_length",'
'"request_uri":"$request_uri",'
'"request_method":"$request_method",'
'"server_name":"$server_name",'
'"status":"$status",'
'"remote_user":"$remote_user",'
'"remote_addr":"$remote_addr",'
'"time_local":"$time_local",'
'"body_bytes_sent":"$body_bytes_sent",'
'"bytes_sent":"$bytes_sent",'
'"msec":"$msec",'
'"upstream_status":"$upstream_status",'
'"upstream_addr":"$upstream_addr",'
'"upstream_response_time":"$upstream_response_time",'
'"http_sessionKey":"$http_sessionKey",'
'"cookie_sk":"$cookie_sk",'
'"http_referer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"http_x_forwarded_for":"$http_x_forwarded_for"'
'}';
在定义好日志格式之后,通过access_log
指令配置日志的存储路径,在配置完日志之后,reload
一下就可自动创建日志文件,以上的环境变量含义都可以在官网上找到准确的释义:
access_log logs/web.devopsman.cn.access.log main;
此时,发送一个 http 的请求,就会按照日志的格式记录日志写入到日志文件中:
root@master:/webserver/nginx18/logs# tail -f web.devopsman.cn.access.log
{"@timestamp":"2020-07-11T10:26:55-04:00","connection":"17","connection_requests":"1","request":"HEAD / HTTP/1.1","request_time":"0.000","request_length":"81","request_uri":"/","request_method":"HEAD","server_name":"web.devopsman.cn","status":"200","remote_user":"-","remote_addr":"192.168.99.1","time_local":"11/Jul/2020:10:26:55 -0400","body_bytes_sent":"0","bytes_sent":"236","msec":"1594477615.080","upstream_status":"-","upstream_addr":"-","upstream_response_time":"-","http_sessionKey":"-","cookie_sk":"-","http_referer":"-","http_user_agent":"curl/7.64.1","http_x_forwarded_for":"-"}
[1]Nginx网站: https://nginx.org
[2]Nginx-module-gzip: http://nginx.org/en/docs/http/ngx_http_gzip_module.html
[3]Nignx-core Embedded Variables: http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
[4]nginx-realip Embedded variables: http://nginx.org/en/docs/stream/ngx_stream_realip_module.html#variables