13916729435 发表于 2017-12-23 14:38:10

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

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

# 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   {
  autoindexon;                        #自动显示目录
  autoindex_exact_sizeoff;            #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB;即人性化方式显示文件大小否则以byte显示
  autoindex_localtime on;               #显示的文件时间为文件的服务器时间;即按服务器时间显示
  limit_rate_after 10m;               #10m之后下载速度为10k
  limit_rate 10k;
  alias /opt/html/redhat;   #虚拟目录
  }
  

  
}
  

  

  查看下站点根目录下的文件:
# ls /var/www/html/
  aaheheshibotest.html
  重启nginx服务

# /usr/local/nginx/sbin/nginx -s>  然后就可以访问了:

  如上的设置,要想设置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")
# htpasswd -c /usr/local/nginx/conf/auth_password wangshibo      //会被要求输入两次密码
# cat /usr/local/nginx/conf/auth_password
  wangshibo:$awe1$I2FIVtPG$I51oSU4eatH.tJdnmxtg67
  Nginx配置中添加auth认证配置
# 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服务

# /usr/local/nginx/sbin/nginx -s>  这时候访问站点的soft目录时就会被要求输入用户名和密码:

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

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

location ^~ /soft/ {  location ~ \.php$ {                           //或者是location ~ .*\.(php|php5)?$ {
  root   /var/www/html;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_read_timeout 300;
  fastcgi_indexindex.php;
  fastcgi_paramSCRIPT_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):
  比如:
# cat /var/www/html/aa/haha
  this is test file


  在浏览器里直接点击站点下的文件(比如上面的haha文件)就会下载下来了(点击文件,除了html格式的文件能直接读出来,其他文件都是直接下载)。
  也可以在linux终端命令行里使用wget进行文件传输,比如在远程机器上下载上面站点的haha文件:
# 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
  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
  查看,发现已经传过来了
# ls /tmp/testmd/
  haha
# cat /tmp/testmd/haha
  this is test file
  -----------------------------------------------------------------------版本隐藏设置-------------------------------------------------------------------------------------------
  

----------------------------------------------------------------------------  
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>  
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>  
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 能看到更多的模块支持,然后编译安装即可。
  
# /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 已经处理完正在等候下一次请求指令的驻留连接.
  
页: [1]
查看完整版本: nginx下目录浏览及其验证功能、版本隐藏等配置记录