WebDav and Nginx

Greetings, gentlemen.

Not so long ago, in one of my projects I needed to be able to transfer files using the PUT method, without a handler script on the receiving side, the server itself had to accept and process the file. It was also a task to implement this not on Apache, but on its counterpart - Nginx.

As a result of my research, I got such a scheme - the PHP script receives the file address and makes a request to the server, and it in turn receives the file and puts it in the required folder.

I will give further examples on installing, configuring, and testing interactions based on a debian-based OS.

Server Side, Nginx

Download, compile and install the server with the required module : Configure the configuration: When compiling, we specified that the configuration file will be in /etc/nginx/nginx.conf We write a new server section, since webdav needs to work on a different port - it is safer , and more convenient. I also want to note that with the configured module, the other WebDav directives specified in the Nginx configuration file also work out: DELETE, MKCOL, COPY, MOVE.

wget nginx.org/download/nginx-1.1.1.tar.gz
tar -xvf nginx-1.1.1.tar.gz
cd nginx-1.1.1

./configure --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--with-http_stub_status_module \
--with-http_ssl_module \

make && make install

server {
listen 7500; #порт на котором будет слушать nginx
server_name ip-адрес-сервера;
charset utf-8;

location /{
expires max;
root /путь/до/требуемой/папки; #по этому пути будут складываться полученный PUT'ом файлы
client_max_body_size 20m;
client_body_temp_path /usr/local/nginx/html/;
dav_methods PUT; #разрешенные методы, нам требуется только PUT

create_full_put_path on; #при отсутствии вложенных папок, при включенной директиве, nginx автоматически создаст иерархию
dav_access user:rw group:r all:r; #права на файлы

limit_except GET {
allow all;

Client side, second server, PHP script

Let us say that the variable $ namefile already contains a file name of the form file.zip obtained in one way or another.

$url = "ip-адрес-сервера:7500/$namefile";
$file = "/files/$namefile";

$fp = fopen($file, "r");
$curl = curl_init();

curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_INFILE, $fp);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file));



Now that everything is installed and configured, we can turn to the script, transfer the path to the file and the script will transfer our file to the server, which in turn without PHP, Perl or a handler written in another language can accept and place the file on another server.

I hope that the written text was interesting to you, for the sake I bow.

Also popular now: