nginx的主要功能虽然是反向代理、负载均衡、虚拟主机功能,但是也可以作为静态资源服务器。现在大型网站都是静态资源(css,js,图片等)都是与应用服务器分离的,单独放在另一些服务器上,即静态资源服务器,nginx就是最好的选择之一
不多说 ,下边给出简单的配置:
server {
listen 80;
server_name 192.168.172.76;
#charset koi8-r;
#access_log logs/host.access.log main;
location ^~ /static/ {
root /opt/resource;
autoindex on;#开启目录访问,默认off,不允许访问
autoindex_exact_size off;#关闭具体大小,显示kb或mb或gb;默认on,显示具体大小,单位byte
autoindex_localtime on;#显示服务器的文件修改时间,默认off,不显示
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
注意:static路径为/opt/resource/static,即static在/opt/resource/下,static下为静态文件
访问路径http://192.168.1.76/static,点击对应资源,可以查看图片或者下载文件。