0

I have nginx setup as reverse-proxy for node.js. For file uploads I'm asking NGINX to forward the request to node.js but for file download, I want that to be done by NGINX. I can get it to work using a GET request.

But I'd really like to authenticate the session via POST request and then serve the file. Please advise on how to achieve this.

Here is my "default" config file for NGINX located under /etc/nginx/sites-available :

server {
    listen 3000;
    server_name X.Y.Z;
    root /mnt/Files/;
    error_log     /home/nginx/logs/error.log debug;
    access_log  /home/nginx/logs/access.log;
    client_max_body_size 20M;

    location /download/* {
        root /mnt/Files/;
        access_log   on;
        autoindex on;

        set $var1 = $1
        try_files $uri $uri/ =404;

    }

    location /upload {
        proxy_pass http://127.0.0.1:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}

Example form I will submit will have the following fields:

username: user1 password: pass1 subFolder: file:

3

1 Answer 1

1

You can not put so much logic inside nginx config file.

first you need to retrieve post parameters.

then you need to validate that against some DB like mysql or mongoDB.

and then return 401( un-authorised).

I don't think nginx can do all this.

I suggest do all this as part of express static feature.

var express = require('express');

var app = express();

var auth = express.basicAuth(function(user,password) {
  return 'john' === user && '1234' === password;
});

app.use('/media', auth);//should be before static 
app.use('/media', express.static(__dirname + '/media'));
1
  • Thank you! I thought so too, but wanted the community to weigh in.
    – Curious101
    Jul 15, 2016 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.