📜 ⬆️ ⬇️

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 script handler on the receiving side, the server itself had to accept and process the file. There was also a task to implement it not on Apache, but on his colleague - 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 he, in turn, receives the file and puts it into the required daddy.
')
I will give further examples on installing, configuring and testing interactions, starting from debian-based OS.



Server side, Nginx


Download, compile and install the server with the required module :

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 \
--with-http_dav_module

make && make install


Configuring the configuration:
When compiling, we indicated that the configuration file will be in /etc/nginx/nginx.conf

We are registering a new server section, as it is necessary for webdav to work on another port - this is both safer and more convenient.

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;
}
}
}


I also want to note that with the configured module, the other WebDav directives specified in the Nginx configuration file are also implemented: DELETE, MKCOL, COPY, MOVE.

The side of the "client", the second server, PHP script


Let's stipulate that the $ namefile variable already contains the 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));

curl_close($curl);
fclose($fp);


Total


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

I hope that the written text was interesting to you, I will take my leave for this.

Source: https://habr.com/ru/post/127232/


All Articles