设为首页 收藏本站
查看: 626|回复: 0

[经验分享] nginx 列出目录文件,提供下载最简单的实现方法

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-2-9 09:30:48 | 显示全部楼层 |阅读模式
闲来无聊,学一下nginx的配置,简单的列出目录的文件,提供文件的下载
查看系统版本
[iyunv@localhost nginx-1.7.9]# cat /etc/centos-release
CentOS release 6.5 (Final)
源码解压,-C指定解压的目录
[iyunv@localhost nginx-1.7.9]tar -xvf  nginx-1.7.9.tar.gx -C /opt/
[iyunv@localhost nginx-1.7.9]cd /opt/nginx-1.7.9
新建一个目录,注备用来安装nginx
[iyunv@localhost nginx-1.7.9]# mkdir /usr/local/nginx -p
[iyunv@localhost nginx-1.7.9]#
配置,注意这里的进入了源码解压的目录的,用--prefix指定安装目录,需要指定别的选择可以用--help来查看
[iyunv@localhost nginx-1.7.9]# ./configure --prefix=/usr/local/nginx/
checking for getaddrinfo() ... found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
出错,提示没有PCRE 库
[iyunv@localhost nginx]# yum list | grep pcre
pcre.x86_64                               7.8-6.el6                      @anaconda-CentOS-201311272149.x86_64/6.5
pcre-devel.x86_64                         7.8-6.el6                      @base
ghc-pcre-light.x86_64                     0.4-7.el6                      epel   
ghc-pcre-light-devel.x86_64               0.4-7.el6                      epel   
pcre.i686                                 7.8-6.el6                      base   
pcre-devel.i686                           7.8-6.el6                      base   
pcre-static.x86_64                        7.8-6.el6                      base   
[iyunv@localhost nginx]#
一般这样列出来的,我们就安装提示需要的那个包或者是那个包的devel版本的
[iyunv@localhost nginx-1.7.9]# yum -y install pcre-devel
安装好PCRE库后,再次执行配置命令
[iyunv@localhost nginx-1.7.9]# ./configure --prefix=/usr/local/nginx/
checking for sha1 in system md library ... not found
checking for sha1 in system OpenSSL crypto library ... not found
checking for zlib library ... not found
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
又有提示需要zlib库
[iyunv@localhost nginx-1.7.9]# yum list | grep gzip
gzip.x86_64                               1.3.12-19.el6_4                @anaconda-CentOS-201311272149.x86_64/6.5
gzip.x86_64                               1.3.12-22.el6                  base   
perl-PerlIO-gzip.x86_64                   0.18-6.el6                     epel   
python-gzipstream.noarch                  1.4.3-1.el6                    epel   
[iyunv@localhost nginx-1.7.9]#
[iyunv@localhost nginx-1.7.9]#
[iyunv@localhost nginx-1.7.9]# yum -y install gzip
[iyunv@localhost nginx-1.7.9]# ./configure --prefix=/usr/local/nginx/
checking for sha1 in system OpenSSL crypto library ... not found
checking for zlib library ... not found
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
[iyunv@localhost nginx-1.7.9]# yum -y install zlib
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* epel: mirrors.123host.vn
* extras: mirrors.163.com
* updates: mirrors.163.com
Setting up Install Process
Package zlib-1.2.3-29.el6.x86_64 already installed and latest version
Nothing to do
表示这个库已经安装了,我们装另一个
上面说过,一般我们安装提示需要的那个包或者是那个包的devel版本的就行了
[iyunv@localhost nginx-1.7.9]# yum -y install zlib-devel
没一次执行完一条命令,可以查看命令有没有执行成功,不管执行了什么命令,echo $?时,如果返回0表示命令执行成功,否则执行失败。

[iyunv@localhost nginx-1.7.9]# echo $?
0
上面几个库可以用一条命令来安装的,把多个库的名字加在命令后面就可以了

配置完了,安装
[iyunv@localhost nginx-1.7.9]# make &&make install
[iyunv@localhost nginx-1.7.9]# echo $?
0
安装成功
另外一种检验方法
[iyunv@localhost nginx-1.7.9]# ngi
安装完成之后,输入命令的前几个字母,按tab键,如果没补全,有可能没安装成功,但是这不是一点的,有可能是路径没包含在$PATH里面
用上面的命令更新检索库
[iyunv@localhost nginx-1.7.9]# updatedb
如果还是无法补全,用which命令查看我们需要的命令的路径
[iyunv@localhost nginx]# which nginx
/usr/local/nginx/sbin/nginx
或者这样找:
[iyunv@localhost nginx-1.7.9]# locate nginx | grep bin
/opt/nginx-1.7.9/objs/src/http/ngx_http_upstream_round_robin.o
/opt/nginx-1.7.9/src/http/ngx_http_upstream_round_robin.c
/opt/nginx-1.7.9/src/http/ngx_http_upstream_round_robin.h
/usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx

用下面的命令查看系统默认的路径
[iyunv@localhost nginx]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/nginx:/root/bin

很明显,我们的nginx命令不在系统默认的路径下,系统找不到,所以无法补全

添加系统环境变量,是系统能够找到我们安装的程序(命令)

[iyunv@localhost nginx-1.7.9]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin
我们的bash_profile本来就有PATH变量的了,我们只是在后面添加了可执行的nginx的路径

或者我们把可执行的nginx程序拷贝到PATH变量默认的任何一个目录也可以(上面echo $PATH出来的)

改完之后,使之立即生效,
[iyunv@localhost nginx-1.7.9]# source ~/.bash_profile
启动nginx程序,
[iyunv@localhost nginx-1.7.9]# nginx
如果要指定配置文件启动,就用-c参数
[iyunv@localhost nginx-1.7.9]# nginx -c /usr/local/nginx/conf/nginx.conf
如果提示80端口被占用了,查找一下
[iyunv@localhost nginx]# netstat -anlp | grep 80
看到是apache占用这个端口
[iyunv@localhost nginx-1.7.9]# ps -ef | grep apach
root       678 27379  0 15:09 pts/0    00:00:00 grep --color apach
apache   15563  1956  0 Dec21 ?        00:00:17 (wsgi:cobbler_w
apache   15564  1956  0 Dec21 ?        00:00:00 /usr/sbin/httpd
apache   15565  1956  0 Dec21 ?        00:00:01 /usr/sbin/httpd
apache   15566  1956  0 Dec21 ?        00:00:00 /usr/sbin/httpd
apache   15567  1956  0 Dec21 ?        00:00:05 /usr/sbin/httpd
apache   15568  1956  0 Dec21 ?        00:00:00 /usr/sbin/httpd
apache   15569  1956  0 Dec21 ?        00:00:01 /usr/sbin/httpd
apache   15570  1956  0 Dec21 ?        00:00:00 /usr/sbin/httpd
apache   15582  1956  0 Dec21 ?        00:00:00 /usr/sbin/httpd
apache   15583  1956  0 Dec21 ?        00:00:00 /usr/sbin/httpd
[iyunv@localhost nginx-1.7.9]#
把apache服务干掉
[iyunv@localhost nginx-1.7.9]# pkill httpd
服务没有了
[iyunv@localhost nginx-1.7.9]# ps -ef | grep apach
root       684 27379  0 15:09 pts/0    00:00:00 grep --color apach
下面是把配置文件及注释去掉的最简单的列出目录的文件
[iyunv@localhost nginx]# vim conf/nginx.conf
1 worker_processes  2;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
10 autoindex on;
11 autoindex_exact_size on;
12 autoindex_localtime on;
13     server {
14         listen       80;
15         server_name  localhost;
16         location /{
17             root   html;
18             index  index.html index.htm;
19         }
20         error_page   500 502 503 504  /50x.html;
21         location = /50x.html {
22             root   html;
23         }
24     }
25 }
只是添加了粗体那3行,其实11和12行都额可以不要的。
我们安装好后,默认的web跟目录是html目录的,比如我指定的安装目录是/usr/local/nginx/
默认的根目录就是/usr/local/nginx/html目录。

做完上面的配置,我们需要把根目录默认的index.html文件删掉或者改为别的名字
否则会默认打开 index.html文件,而不是把根目录的文件全部列出来

重新加载nginx配置
[iyunv@localhost nginx]# nginx -s reload

在浏览器地址栏输入虚拟机的IP,即可访问我们的下载列表。

我们提供的下载列表里的文件是以超链接的形式显示出来的,如果是浏览器可以打开的文件如txt文件,
点击之后不会下载,而是直接打开。

用firefox 浏览器,提示没flash插件,所以没配图。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-42202-1-1.html 上篇帖子: nginx高效原理及编译安装 下篇帖子: Nginx的CentOS开机自启脚本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表