nginx配置静态文件访问
本文章需要提前安装好nginx,参考:nginx安装配置
本demo日志文件存放到了系统的 /opt/logs/ 目录下
一、nginx配置
1.1 nginx.conf配置
vi /usr/local/nginx/conf/nginx.conf
--------------------------------------------------------------------------
#添加nginx拦截配置
location ~* /logs {
root /opt/;
#新增配置 只浏览不下载
# add_header Content-Type text/plain;
# default_type text/html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
add_header Cache-Control no-store;
}
--------------------------------------------------------------------------
1.2 mime.types配置允许的格式。
因为我们的需求是在线访问 xx.log文件,所以这里添加 text/log log;配置。
1.3 密码访问策略(可不配置)
yum -y install httpd-tools
cd /usr/local/nginx
#生成密码
htpasswd -c passwd test
请输入密码:test
- nginx.conf配置文件中配置密码验证:
#添加nginx拦截配置
location ~* /logs {
root /opt/;
#新增配置 只浏览不下载
# add_header Content-Type text/plain;
# default_type text/html;
auth_basic "please input password";
auth_basic_user_file /usr/local/nginx/conf/loguser; #认证账号密码保存的文件
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
add_header Cache-Control no-store;
}
- 重启nginx使之生效
/usr/local/nginx/sbin/nginx -s reload
二、校验
日志文件:
访问: