1.您所需要的准备的文件
可以是一些想html的帮助文档等,我选择的是编译器中STL手册的帮助文档,一般来说有一个index.html和其他一些html文件来展示内容.
2.步骤
1.新建clib_man的目录,将帮助文档放入这个目录中;
2.修改配置文件将location中的设置clib_man/这个目录;
3.nginx -s reload,重新加载配置文件;
4.重新打开浏览器可以看到(此时我的clib_man/目录下是有index.html文件的).
至此,静态的资源服务器就搭建完成了.
3.配置文件
3.添加auto_index功能
ngx_http_autoindex_module 模块可以列出目录中的文件. 一般当ngx_http_index_module模块找不到默认主页的时候,会把请求转给 ngx_http_autoindex_module模块去处理.
Nginx默认是不允许列出整个目录的,如需此功能,打开nginx.conf文件,在location server 或 http段中加入
autoindex on;
另外两个参数最好也加上去:
autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
改为on后,显示的文件时间为文件的服务器时间
记得执行nginx -s reload,重新加载配置文件.
http://nginx.org/en/docs/http/ngx_http_autoindex_module.html中可以看到官方说明.
在上面,将文件夹中的index.xml移除或者重命名则可以给出列表:
4.添加压缩功能
gzip on;#打开压缩功能
记得执行nginx -s reload,重新加载配置文件.
官方说明文档:
http://nginx.org/en/docs/http/ngx_http_gzip_module.html
5.添加限速功能
记得执行nginx -s reload,重新加载配置文件.
官方说明文档:
http://nginx.org/en/docs/http/ngx_http_core_module.html#limit_rate
6.压缩及限速功能测试
7.日志模块设置功能
记得执行nginx -s reload,重新加载配置文件.
如上面的配置文件中的显示,
官方说明路径:
http://nginx.org/en/docs/syslog.html
8.日志显示
9.留个配置文件的备份
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#命名日志格式,可提供不同的域名下的不同日志格式的记录,配合access_log指令使用,内嵌变量和提供的第三方模块变量都可以配置到日志中
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forworded_for"';
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;#打开压缩的功能
gzip off;
gzip_min_length 1;#表示小于1字节的内容就不再进行压缩了
gzip_comp_level 2;#表示压缩的等级为2
gzip_types text/plain application/X-javascript text/css application/xml text/javascript application/x-http-php img/jpeg image/gif image/png;#针对列出的类型的文件才做压缩
server {
listen 8081;
server_name localhost;
location / {
#root html;
alias clib_man/;
autoindex on;#将autoindex模块打开,当找不到index文件的时候,会去以文件列表的形式打开文件夹
autoindex_exact_size on;
autoindex_localtime on;
#每秒传输1千字节到浏览器中,此时会很慢的,缓存禁用的时候可以测试出现象
# set $limit_rate 1k;#设置nginx向客户浏览器发送响应的速度,这个limit_rate是nginx的内嵌变量,限制访问速度,在http://nginx.org/en/docs/http/ngx_http_core_module.html#variables或源码中可搜索到说明
access_log logs/server_access.log main;#表示access_log所在位置的所属请求会采用main这种格式记录到server_access.log中
}
location /images/ {
root /usr/local/niginx/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}