NWNU-Sun | 技术沉思录

代码是诗,bug是谜

   ::  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  74 随笔 :: 49 文章 :: 6 评论 :: 40583 阅读

Httpd配置文件上传

因为公司产品功能需要实现文件存储,所以调研下Apache httpd 如何开启上传功能

实现方式

目前存在两种方式:

  1. WebDAV方式
    2.CGI方式

WebDAV方式

Apache中默认已经集成了WebDAV模块,只需要在httpd.conf中开启即可。

介绍

WebDAV是一种基于HTTP协议的文件传输协议,它允许用户通过HTTP协议访问和管理文件系统。WebDAV协议可以让用户在Web浏览器中访问和管理文件,而不需要安装任何客户端软件。WebDAV协议还支持文件版本控制、文件锁定、文件共享等功能。

查看是否开启WebDAV

[root@centos conf]# httpd -M | grep dav
 dav_module (shared)
 dav_fs_module (shared)
 dav_lock_module (shared)
[root@centos conf]#

显示已经开启模块,实际上WebDAV模块是在conf.modules.d目录下的00-dav.conf文件中开启的

[root@centos conf.modules.d]# pwd
/etc/httpd/conf.modules.d
[root@centos conf.modules.d]# ls -l
total 28
-rw-r--r--. 1 root root 3739 May 30  2023 00-base.conf
-rw-r--r--. 1 root root  139 May 30  2023 00-dav.conf
-rw-r--r--. 1 root root   41 May 30  2023 00-lua.conf
-rw-r--r--. 1 root root  742 May 30  2023 00-mpm.conf
-rw-r--r--. 1 root root  957 May 30  2023 00-proxy.conf
-rw-r--r--. 1 root root   88 May 30  2023 00-systemd.conf
-rw-r--r--. 1 root root  451 May 30  2023 01-cgi.conf
[root@centos conf.modules.d]#

其中00-dav.conf文件中已经开启了WebDAV模块,主配置文件中采用Include引入了00-dav.conf文件

IncludeOptional conf.d/*.conf

配置WebDAV

在httpd.conf文件中修改目录配置

# Dav锁数据库的位置
DAVLockDB "/var/www/html/uploads/Dav.lock"

<Directory "/var/www/html/uploads">
    # 开启Dav
    Dav on
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

需改后重启服务

systemctl restart httpd

测试

WebDav标准上传使用PUT方法
curl命令格式

curl -X PUT -T 本地文件路径 -u 用户名:密码 http://服务器地址/远程路径/
curl -X PUT -T start.sh   http://192.168.100.188:80/dstore_repo/

分块上传大文件

上传测试文件,返回状态码为201则上传成功,注意:无论目录是什么最后的‘/’不能忽略不写。

curl -X PUT -T largefile.zip --limit-rate 1M http://example.com/dav/largefile.zip
# test
curl -X PUT -T hbis-xa.nginx.1.26.2.docker.img.gz  --limit-rate 1M http://192.168.100.188:80/dstore_repo/hbis-xa.nginx.1.26.2.docker.img.gz

CGI方式

这种方式需要在httpd.conf中开启cgi模块,并且在cgi.conf中配置cgi解释器。

介绍

CGI(Common Gateway Interface)是一种用于在Web服务器和应用程序之间传输数据的协议。CGI是一种标准协议,它允许Web服务器将请求转发给应用程序,并将应用程序的输出返回给Web服务器。CGI协议的主要目的是将Web服务器和应用程序解耦,使得Web服务器可以处理多个应用程序。

原理

Apache服务器通过CGI(Common Gateway Interface,通用网关接口)或FastCGI技术来处理文件上传请求。当用户通过Web表单提交文件时,Apache服务器将文件数据发送到CGI或FastCGI脚本进行处理。

查看是否开启cgi

[root@centos conf]# httpd -M | grep cgi
cgi_module (shared)

显示已经开启模块

配置

修改http.conf中的Files为如下

<Files ~ "\.(cgi|fcgi|pl|py|htaccess)$">  
    Order allow,deny  
    Allow from all  
</Files>  

ScriptAlias /upload "/var/www/html/script.py"  

这样,Apache服务器允许上传的文件扩s展名包括cgi、fcgi、pl、py和htaccess。

创建Html表单

vi /var/www/html/index.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
</head>
<body>
    <h1>文件上传</h1>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <label for="file">选择文件:</label>
        <input type="file" name="file" id="file" required>
        <br><br>
        <input type="submit" value="上传">
    </form>
</body>
</html>

创建cgi脚本

vi /var/www/script.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import cgi
import cgitb

# 启用 CGI 错误跟踪
cgitb.enable()

def upload_file():
    # 解析表单数据
    form = cgi.FieldStorage()

    # 检查是否有文件上传
    if "file" not in form:
        print "Content-Type: text/plain"  # 输出 HTTP 头
        print  # 确保在头部之后有一个空行
        print "Error: No file uploaded."
        return

    # 获取文件对象
    file_item = form["file"]
    upload_path = "/var/www/html/uploads"

    # 检查上传目录是否存在,如果不存在则创建
    if not os.path.exists(upload_path):
        os.makedirs(upload_path)

    # 保存文件
    try:
        file_name = os.path.basename(file_item.filename)
        with open(os.path.join(upload_path, file_name), "wb") as f:
            f.write(file_item.file.read())
        print "Content-Type: text/plain"
        print  # 确保在头部之后有一个空行
        print "File {} uploaded successfully.".format(file_name)
    except Exception as e:
        print "Content-Type: text/plain"
        print  # 确保在头部之后有一个空行
        print "Error: {}".format(str(e))

if __name__ == "__main__":
    upload_file()

将脚本文件上传到Apache服务器的虚拟目录,并赋予执行权限。

遗留:大文件会失败

认证示例

以下是一个使用Basic认证的示例配置,其中只有lisiwanwu可以访问/var/www/html/uploads/目录下的资源。

用户和密码文件生成:

mkdir -p /usr/local/apache/

创建用户

htpasswd -cb /usr/local/apache/a_com.pass lisi 123456
htpasswd -b /usr/local/apache/a_com.pass wanwu 123456

修改Apache配置文件:

<Directory "/var/www/html/uploads">
    Options Indexes FollowSymLinks
    AllowOverride Authconfig
    AuthType Basic
    AuthName "Login Notice"
    AuthUserFile /usr/local/apache/a_com.pass
    Require user lisi  wanwu
</Directory>
  • ServerRoot:如果AuthUserFileAuthGroupFile使用相对路径,那么它们是相对于ServerRoot目录的。
  • htpasswd命令:用来创建和管理用户认证文件。-c选项用于创建新文件,-b选项用于提供密码。
posted on   匿名者nwnu  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示