nginx下目录浏览及其验证功能、版本隐藏等配置记录

简介:

工作中常常有写不能有网页下载东西的需求,在Apache下搭建完成后直接导入文件即可达到下载/显示文件的效果;
而Nginx的目录列表功能默认是关闭的,如果需要打开Nginx的目录列表功能,需要手动配置,还可以进行访问验证;
nginx目录列表功能需要用到下面这个模块:
ngx_http_autoindex_module  
此模块用于自动生成目录列表,只在 ngx_http_index_module模块未找到索引文件时发出请求.

下面就对nginx的目录浏览及验证访问功能的操作进行梳理:

1)设置目录浏览
打开nginx的配置文件,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@wangshibo ~] # vim /usr/local/nginx/conf/vhost/test.conf
  server {
    listen 80;
    server_name localhost;             // 访问http: //ip ,发现访问的站点目录还是默认的;可以将此处的localhost直接改成服务器ip地址
    root  /var/www/html ;
    index index.html;
 
    location / {
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    }
 
    location  /images  {
       root    /var/www/html/shibo ;
       autoindex on;
         }
 
    location  /bo    {
       autoindex  on;                         #自动显示目录
       autoindex_exact_size  off;             #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB;即人性化方式显示文件大小否则以byte显示
       autoindex_localtime on;                #显示的文件时间为文件的服务器时间;即按服务器时间显示
       limit_rate_after 10m;                  #10m之后下载速度为10k
       limit_rate 10k;
       alias  /opt/html/redhat ;    #虚拟目录
       }
 
}

查看下站点根目录下的文件:
[root@wangshibo ~]# ls /var/www/html/
aa hehe shibo test.html
重启nginx服务
[root@wangshibo ~]# /usr/local/nginx/sbin/nginx -s reload

然后就可以访问了:

如上的设置,要想设置nginx的目录浏览功能,必须要打开下面这个参数
autoindex on;

此外,另外两个参数最好也加上去:
autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
改为on后,显示的文件时间为文件的服务器时间

2)设置访问验证
创建类htpasswd文件(如果没有htpasswd命令,可通过"yum install -y *htpasswd*"或"yum install -y httpd")
[root@wangshibo ~]# htpasswd -c /usr/local/nginx/conf/auth_password wangshibo //会被要求输入两次密码
[root@wangshibo ~]# cat /usr/local/nginx/conf/auth_password
wangshibo:$awe1$I2FIVtPG$I51oSU4eatH.tJdnmxtg67

Nginx配置中添加auth认证配置
[root@wangshibo ~]# vim /usr/local/nginx/conf/vhost/test.conf
......
location ^~ /soft/ {
     root /var/www/html;              //此处为soft的上一级目录。注意root和alias虚拟目录设置区别
     autoindex on;
     autoindex_exact_size off;
     autoindex_localtime on;
     auth_basic "MyPath Authorized";                    //为提示信息,可以自行修改;会出现在第一次访问Nginx站点的弹出框内
     auth_basic_user_file /usr/local/nginx/conf/auth_password;                  //最好跟htpasswd文件的全路径
}

重启nginx服务
[root@wangshibo ~]# /usr/local/nginx/sbin/nginx -s reload

这时候访问站点的soft目录时就会被要求输入用户名和密码:

如果用户名和密码输入错误会提示401错误(大名鼎鼎的http基本认证)

需要特别注意的是:
加上认证之后该目录下的php文件将不会被解析,会运行下载。
如果要使其能够解析php可以,可将上面的配置改为:

1
2
3
4
5
6
7
8
9
10
11
12
location ^~  /soft/  {
    location ~ \.php$ {                            // 或者是location ~ .*\.(php|php5)?$ {
       root    /var/www/html ;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_read_timeout 300;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;;
       include        fastcgi.conf;
   }
       auth_basic  "Authorized users only" ;
       auth_basic_user_file  /usr/local/nginx/conf/auth_password ;
}

nginx运行目录浏览后,就可以利用wget进行文件远程传输了(只针对文件,因为在http下只能文件访问,直接跟url访问目录是404)
比如:
[root@wangshibo ~]# cat /var/www/html/aa/haha
this is test file

在浏览器里直接点击站点下的文件(比如上面的haha文件)就会下载下来了(点击文件,除了html格式的文件能直接读出来,其他文件都是直接下载)。
也可以在linux终端命令行里使用wget进行文件传输,比如在远程机器上下载上面站点的haha文件:
[root@bastion-IDC ~]# wget http://113.110.186.117/aa/haha -P /tmp/testmd
--2017-01-03 15:14:18-- http://113.110.186.117/aa/haha
Connecting to 113.110.186.117... connected.
HTTP request sent, awaiting response... 200 OK
Length: 18 [application/octet-stream]
Saving to: “/tmp/testmd/haha”

100%[=====================================================================================================>] 18 --.-K/s in 0s

2017-01-03 15:14:18 (2.60 MB/s) - “/tmp/testmd/haha” saved [18/18]

查看,发现已经传过来了
[root@bastion-IDC ~]# ls /tmp/testmd/
haha
[root@bastion-IDC ~]# cat /tmp/testmd/haha
this is test file

-----------------------------------------------------------------------版本隐藏设置-------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
----------------------------------------------------------------------------
1)在nginx下禁止使用ip访问,将使用ip访问的流量重定向到公司的官网上。
在vhost下重新编辑一个default.conf 文件,如下:
server {
     listen 80 default_server;
#    server_name _;
#    return 500;
     rewrite ^(.*) https: //www .wangshibo.com permanent;
}
 
然后重启nginx即可!
----------------------------------
下面就是直接紧张ip访问了
server {
     listen 80 default_server;
     server_name _;
     return  500;
}
----------------------------------
 
2)隐藏nginx的版本号
直接在nginx.conf文件中的http{}里面添加:
server_tokens off;
 
重启nginx服务即可!
curl -i http: //www .wangshibo.com    测试访问就会发现nginx的header信息中已没有版本信息了(-i参数)
 
3)隐藏tomcat的版本号
# /data/tomcat8/bin/version.sh        #查看版本号信息
Using CATALINA_BASE:    /data/tomcat8
Using CATALINA_HOME:    /data/tomcat8
Using CATALINA_TMPDIR:  /data/tomcat8/temp
Using JRE_HOME:         /usr/lib/jvm/java-1 .7.0-openjdk.x86_64
Using CLASSPATH:        /data/tomcat8/bin/bootstrap .jar: /data/tomcat8/bin/tomcat-juli .jar
Server version: Apache Tomcat /8 .5.15
Server built:   May 5 2017 11:03:04 UTC
Server number:  8.5.15.0
OS Name:        Linux
OS Version:     2.6.32-696.3.2.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_141-mockbuild_2017_05_09_14_20-b00
JVM Vendor:     Oracle Corporation
 
# cp -r /data/tomcat8 /data/tomcat8.bak
# cd /data/tomcat8/lib
# jar xf catalina.jar
# vim org/apache/catalina/util/ServerInfo.properties
server.info=Apache Tomcat         // 修改成这样
server.number=                  // 清空
server.built=                   // 清空
 
# jar cf catalina.jar org     //再次打包覆盖
# ll catalina.jar
# /data/tomcat8/bin/version.sh        //发现tomcat的版本信息已被隐藏
Using CATALINA_BASE:    /data/tomcat8
Using CATALINA_HOME:    /data/tomcat8
Using CATALINA_TMPDIR:  /data/tomcat8/temp
Using JRE_HOME:         /usr/lib/jvm/java-1 .7.0-openjdk.x86_64
Using CLASSPATH:        /data/tomcat8/bin/bootstrap .jar: /data/tomcat8/bin/tomcat-juli .jar
Server version: Apache Tomcat          
Server built:  
Server number: 
OS Name:        Linux
OS Version:     2.6.32-696.3.2.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_141-mockbuild_2017_05_09_14_20-b00
JVM Vendor:     Oracle Corporation
 
4)nginx-status开启状态查看功能及参数说明
本模块在编译的时候默认是不编译的,如果是从源码编译安装的nginx,那么需要在. /configure 编译的时候加上对应的模块--with-http_stub_status_module
使用 . /configure  --help 能看到更多的模块支持,然后编译安装即可。
[root@www vhosts] # /usr/local/nginx/sbin/nginx -V      //使用这个命令可以查看在编译安装的时候使用了哪些模块
nginx version: nginx /1 .8.1
built by  gcc  4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1g 7 Apr 2014 (running with OpenSSL 1.0.1e-fips 11 Feb 2013)
TLS SNI support enabled
configure arguments: --prefix= /usr/local/nginx  --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
 
接下来需要在nginx的配置文件中增加这个配置项。打开nginx的配置文件 nginx.conf,在server段里面增加如下的内容:
  
        location  /nginx-status  {
        stub_status on;
        access_log off;
        #allow 127.0.0.1;                       
        #deny all;
}
 
重启nginx服务后,访问:
# curl http://127.0.0.1/nginx-status
Active connections: 11921
server accepts handled requests
113    113     116
Reading: 0 Writing: 7 Waiting: 42
 
active connections – 活跃的连接数量
server accepts handled requests — 总共处理了113个连接 , 成功创建113次握手, 总共处理了116个请求
reading — 读取客户端的连接数.
writing — 响应数据到客户端的数量
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.
***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************
本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/6244812.html ,如需转载请自行联系原作者
相关文章
|
16天前
|
应用服务中间件 Linux 网络安全
CentOS 7.4源码编译nginx1.12 并且隐藏nginx的版本
CentOS 7.4源码编译nginx1.12 并且隐藏nginx的版本
15 0
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
28 0
|
23天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
95 0
|
4天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
14 0
|
6天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
6天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
14 0
|
17天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
25 0
|
18天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
32 0
|
27天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0
|
28天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
29 0