美文网首页
httpd/nginx文件共享服务

httpd/nginx文件共享服务

作者: linux_python | 来源:发表于2020-12-30 16:57 被阅读0次

    LINUX基于http协议实现文件共享

    声明:以下环境最小化安装系统,centos7.4版本,使用阿里云yum源,使用yum命令简单粗暴安装,毕竟只是实验比较,apache使用80端口,nginx修改为8080端口,什么修改都没操作情况下开始下面的测试验证。

    一、使用apache做为web服务器

    1、使用apche作为web服务,如果共享文件放在默认站点根目录下,无需修改配置,可以直接访问

    示例:

    [root@server /var/www/html]#pwd
    /var/www/html
    [root@server /var/www/html]#ll
    total 0
    drwxr-xr-x 2 root root 18 Jul 22 19:47 test
    [root@server /var/www/html]#ll test/
    total 0
    -rw-r--r-- 1 root root 0 Jul 22 19:47 http
    [root@server /var/www/html]#
    
    

    访问方式时:http://web-ip/访问目录

    image
    注意:如果访问目录,目录下中文名称的文件乱码,在http.comf配置文件最后一行如下内容,保存重启服务
    
     IndexOptions Charset=utf-8
    
    apache 的目录索引样式用的mod_autoindex模块 一般默认为开启状态,如果编译安装一定要编译安装这个模块
    
    

    2、如果共享文件放在其它目录下,需要添加配置文件

    [root@server /home/webroot/http]#ll -d ../http/
    drwsr-sr-x 3 apache apache 34 Jul 22 20:26 ../http/
    [root@server /home/webroot/http]#ll
    total 0
    -rw-r--r-- 1 root apache  0 Jul 22 19:45 http
    drwxr-sr-x 2 root apache 18 Jul 22 19:45 httptest
    [root@server /home/webroot/http]#tree ../http/
    ../http/
    ├── http
    └── httptest
        └── 1111
    1 directory, 2 files
    [root@server /home/webroot/http]#ll httptest/
    total 0
    -rw-r--r-- 1 root apache 0 Jul 22 19:45 1111
    [root@server /home/webroot/http]#
    
    

    http.conf配置文件添加如下配置,加载配置或重启服务即可

    #给目录/home/webroot/http创建别名 /http
    Alias /http "/home/webroot/http"
    <Directory "/home/webroot/http">
    #默认是开启索引浏览 如果不想开启前面加个- ,-Indexes就是禁止目录浏览
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    #默认按文件的创建时间排序;
        IndexOrderDefault Ascending Date  
    #防止服务器的信息泄露 这个不用管,off掉就行
        ServerSignature Off 
    </Directory>
    
    
    image

    二、使用nginx做为web服务器

    1、使用nginx作为web服务,如果共享文件放在默认站点根目录下,添加如下配置,可以直接访问

    [root@server /usr/share/nginx/html]#ll
    total 20
    -rw-r--r-- 1 root root 3650 Mar  6 17:26 404.html
    -rw-r--r-- 1 root root 3693 Mar  6 17:26 50x.html
    -rw-r--r-- 1 root root 3700 Mar  6 17:26 index.html
    -rw-r--r-- 1 root root  368 Mar  6 17:26 nginx-logo.png
    -rw-r--r-- 1 root root 2811 Mar  6 17:26 poweredby.png
    drwxr-xr-x 2 root root   19 Jul 22 19:47 test
    [root@server /usr/share/nginx/html]#ll test/
    total 0
    -rw-r--r-- 1 root root 0 Jul 22 19:47 nginx
    [root@server /usr/share/nginx/html]#
    
        location / {
                autoindex on;    //显示索引
                autoindex_exact_size on;    //显示文件大小
                autoindex_localtime on;    //显示文件时间
                charset utf-8;          //解决中文显示乱码问题
        }
    
    

    主要是locaion里面这四行添加,打开浏览器访问如下

    image

    2、如果共享文件放在其它目录下,需要添加配置文件

    [root@server /home/webroot/nginx]#ll -d  ../nginx/
    drwxr-sr-x 3 nginx nginx 36 Jul 22 19:44 ../nginx/
    [root@server /home/webroot/nginx]#pwd
     /home/webroot/nginx
    [root@server /home/webroot/nginx]#ll
    total 0
    -rw-r--r-- 1 root nginx  0 Jul 22 19:44 nginx
    drwxr-sr-x 2 root nginx 18 Jul 22 19:46 nginxtest
    [root@server /home/webroot/nginx]#tree ../nginx/
    ../nginx/
    ├── nginx
    └── nginxtest
        └── 2222
    1 directory, 2 files
    [root@server /home/webroot/nginx]#ll nginxtest/
    total 0
    -rw-r--r-- 1 root nginx 0 Jul 22 19:46 2222
    [root@server /home/webroot/nginx]#
    
    

    nginc.conf配置文件添加如下配置,加载配置或重启服务即可

        location /nginx {
                root    /home/webroot;
                autoindex on;
                autoindex_exact_size on;
                autoindex_localtime on;
                charset utf-8;
         }
    
    
    image

    相关文章

      网友评论

          本文标题:httpd/nginx文件共享服务

          本文链接:https://www.haomeiwen.com/subject/ykihoktx.html