N
N
Nikolai2017-05-23 10:26:08
Nginx
Nikolai, 2017-05-23 10:26:08

How to upload files to server via nginx?

nginx -V
nginx version: nginx/1.11.9
built by gcc 4.9.2 
built with OpenSSL 1.1.0b  26 Sep 2016
TLS SNI support enabled
--with-http_perl_module

I'm trying to set up file uploads to the server via nginx as follows:
server {
        listen 443 ssl;

        server_name example.com;

        ssl_certificate key.pem;
        ssl_certificate_key privkey.pem;
        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_dhparam dhparams.pem;
        ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
        ssl_stapling on;
        ssl_stapling_verify on;
        add_header Strict-Transport-Security max-age=15768000;

        root /var/www/html;
        error_log /var/log/nginx/example.com-error.log;
        access_log /var/log/nginx/example.com-access.log main;

        index index.html;

        location /upload {
        #auth_basic                 "Restricted Upload";
        #auth_basic_user_file       /etc/nginx/conf.d/basic.htpasswd;
        limit_except POST          { deny all; }

        client_body_temp_path      /tmp/;
        client_body_in_file_only   on;
        client_body_buffer_size    128K;
        client_max_body_size       10M;

        proxy_pass_request_headers on;
        proxy_set_header           X-FILE $request_body_file;
        proxy_set_body             off;
        proxy_redirect             off;
        proxy_pass                 http://localhost:8001/;
        }
}
server {
        listen 127.0.0.1:8001;
        root /opt/uploadfiles/files;
        error_log /var/log/nginx/upload-error.log;
        access_log /var/log/nginx/upload-access.log main;
        location / {
                index index.html;
        }
}

As a result, the curl check shows a 403 error.
curl --data-binary '@filename' https://example.com/upload
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

The rights to the directory are
ls -l /opt/uploadfiles/ 
drwxrwxrwx 2 www-data www-data  4096 May 22 13:00 files

What else needs to be added to the nginx configuration?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Erelecano Oioraen, 2017-05-23
@KolyaniuS

nginx natively knows how to use WevDAV.
Read the documentation.

B
Boris Korobkov, 2017-05-23
@BorisKorobkov

Who do you think should upload the file? All arrows lead to index.html, but it doesn't load.
If you need to upload using Nginx, then https://www.nginx.com/resources/wiki/modules/upload/
If PHP, then php.net/manual/ru/features.file-upload.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question