1. 程式人生 > 實用技巧 >nginx載入webdav模組實現http協議上傳檔案

nginx載入webdav模組實現http協議上傳檔案

1 簡介

WebDAV (Web-based Distributed Authoring and Versioning) 一種基於 HTTP 1.1協議的通訊協議。它擴充套件了HTTP 1.1,在GET、POST、HEAD等幾個HTTP標準方法以外添加了一些新的方法,使應用程式可直接對Web Server直接讀寫,並支援寫檔案鎖定(Locking)及解鎖(Unlock),還可以支援檔案的版本控制,本章使用nginx載入模組webdav實現此功能。

2 環境

2.1 環境資訊

ipaddress=10.168.0.154

hostname=webdav_nginx.cmdschool.org

os=CentOS 6.8

2.2 yum源配置

yum-yinstallgccgcc-c++makeexpat-devel
yum-yinstallrpm-build

2.3 建立構建使用者

useradd -u 1001 builder

2.4 關閉selinux

setenforce0
sed-i's/SELINUX=enforcing/SELINUX=disabled/g'/etc/selinux/config

3 構建rpm包部分

3.1 下載安裝包

su-builder
cd~
wgethttp://nginx.org/packages/centos/6/SRPMS/nginx-1.8.1-1.el6.ngx.src.rpm
wget-Onginx-dav-ext-module.ziphttps://codeload.github.com/arut/nginx-dav-ext-module/zip/master

3.2 解壓並測試構建環境

3.2.1 構建環境測試命令

rpm-ivhnginx-1.8.1-1.el6.ngx.src.rpm
rpmbuild-bbrpmbuild/SPECS/nginx.spec

3.2.2 異常情況

如果看到類似提示:

error:Failedbuilddependencies:
openssl-devel>=1.0.1isneededbynginx-1.8.1-1.el6.ngx.x86_64
zlib-develisneededbynginx-1.8.1-1.el6.ngx.x86_64
pcre-develisneededbynginx-1.8.1-1.el6.ngx.x86_64

請先決絕包的依賴關係然後重試:

exit
yum-yinstallopenssl-develzlib-develpcre-devel

3.2.3 正常情況

如果末尾類似的提示表示構建環境測試通過或稱重新打包完成:

+umask022
+cd/home/builder/rpmbuild/BUILD
+cdnginx-1.8.1
+/bin/rm-rf/home/builder/rpmbuild/BUILDROOT/nginx-1.8.1-1.el6.ngx.x86_64
+exit0

3.3 配置webdav模組

unzipnginx-dav-ext-module.zip
cp-anginx-dav-ext-module-master/rpmbuild/BUILD/nginx-dav-ext-module

3.4 修改構建檔案

3.4.1 建立配置檔案副本

cdrpmbuild/SPECS
cpnginx.specnginx.spec.orig

3.4.2 修改配置檔案引數

vimnginx.spec

修改如下行:

--with-http_dav_module\

增加引數後行如下:

--with-http_dav_module--add-module=../nginx-dav-ext-module\

3.5 重新構建rpm包

diff-uNnginx.spec.orignginx.spec>nginx-dav-ext.patch
patch-p0<nginx-dav-ext.patch
rpmbuild-bbnginx.spec
exit

4 配置webdav服務

4.1 安裝nginx包

cd/home/builder/rpmbuild/RPMS/x86_64/
yum-yinstallnginx-1.8.1-1.el6.ngx.x86_64.rpm

4.2 確認模組啟用

nginx-V

顯示如下:

nginxversion:nginx/1.8.1
builtbygcc4.4.720120313(RedHat4.4.7-17)(GCC)
builtwithOpenSSL1.0.1e-fips11Feb2013
TLSSNIsupportenabled
configurearguments:--prefix=/etc/nginx--sbin-path=/usr/sbin/nginx--conf-path=/etc/nginx/nginx.conf--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx.pid--lock-path=/var/run/nginx.lock--http-client-body-temp-path=/var/cache/nginx/client_temp--http-proxy-temp-path=/var/cache/nginx/proxy_temp--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp--http-scgi-temp-path=/var/cache/nginx/scgi_temp--user=nginx--group=nginx--with-http_ssl_module--with-http_realip_module--with-http_addition_module--with-http_sub_module--with-http_dav_module--add-module=../nginx-dav-ext-module--with-http_flv_module--with-http_mp4_module--with-http_gunzip_module--with-http_gzip_static_module--with-http_random_index_module--with-http_secure_link_module--with-http_stub_status_module--with-http_auth_request_module--with-mail--with-mail_ssl_module--with-file-aio--with-ipv6--with-http_spdy_module--with-cc-opt='-O2-g-pipe-Wall-Wp,-D_FORTIFY_SOURCE=2-fexceptions-fstack-protector--param=ssp-buffer-size=4-m64-mtune=generic'

可見配置的引數後面有載入模組:

--add-module=../nginx-dav-ext-module

4.3 建立上傳目錄

mkdir-p/home/swapzone/
chownnginx:nginx/home/swapzone/
chmod700/home/swapzone/

4.4 建立配置檔案

vim/etc/nginx/conf.d/webdav.conf

修改內容如下:

server{
listen80;
server_namewebdav.cmdschool.org;
access_log/var/log/nginx/webdav.access.logmain;
location/{
root/home/swapzone;
autoindexon;
dav_methodsPUTDELETEMKCOLCOPYMOVE;
dav_ext_methodsPROPFINDOPTIONS;
create_full_put_pathon;
dav_accessuser:rwgroup:rall:r;
auth_basic"AuthorizedUsersOnly";
auth_basic_user_file/etc/nginx/.htpasswd;
}
error_page500502503504/50x.html;
location=/50x.html{
root/usr/share/nginx/html;
}
}

4.5 重啟服務並配置服務預設啟動

/etc/init.d/nginxrestart
chkconfignginxon

4.6 配置身份驗證

4.6.1安裝httpd工具

yum-yinstallhttpd-tools

4.6.2 建立密碼檔案和密碼

1)建立密碼檔案和建立使用者密碼

htpasswd-c/etc/nginx/.htpasswduser1
htpasswd/etc/nginx/.htpasswduser2

2)密碼檔案許可權配置

chownnginx:nginx/etc/nginx/.htpasswd
chmod600/etc/nginx/.htpasswd

4.7 配置防火牆

4.7.1 修改防火牆配置檔案

vim/etc/sysconfig/iptables

加入如下配置:

-AINPUT-mstate--stateNEW-mtcp-ptcp--dport80-jACCEPT

4.7.2 重啟並使配置生效

/etc/init.d/iptablesrestart

5 客戶端

5.1 下載客戶端

官方首頁:https://cyberduck.io/

5.2 配置名稱解析

wKiom1fWcKDgAATZAACQ0TgFYHc600.png

5.3 安裝並測試

wKiom1fWb37jQVECAAC97ECKfg8896.png

wKiom1fXW1rDDj2KAAEACzeNFzc249.png

=====================================

參閱資料

https://github.com/arut/nginx-dav-ext-module

http://nginx.org/en/docs/http/ngx_http_dav_module.html

http://blog.sina.com.cn/s/blog_704836f40102w5je.html


轉載於:https://blog.51cto.com/cmdschool/1852013