Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx with gunicorn with double authorization

I want to use http auth but also, a reverse proxy using gunicorn.

For http auth I use:

location = admin.html {
 auth_basic 'Login Required'
 auth_basic__use_file etc/nginx/.htpasswd;
}

for gunicorn, proxy reverse I found:

try_files $uri @gunicorn;

How can I combine both ?

like image 967
user3541631 Avatar asked Dec 03 '18 20:12

user3541631


People also ask

How do I use nginx with Gunicorn?

Now your nginx is ready to serve requests and it will forward it to your application but for this make sure you have a gunicorn server running. Go to your browser and type <ec2-instance-dns> this will send a request to nginx and then it will forward that request to your server running with gunicorn.

How to run Django on Gunicorn?

Nowadays Django is becoming more powerful in designing web applications. Running a local server of Django is not a recommended way in production because it’s just a test server not a production ready server. So to run Django in production is to run with Gunicorn and use Nginx as a reverse proxy so it gives more security to our application.

How to run Python code in the background using Nginx?

To handle this issues we’ll use a web server ( Nginx ), a Web Server Gateway Interface ( Gunicorn ), and demonize our execution so that the app will be running on the background. Let’s first install Nginx and Gunicorn in our virtual environment Now, you can use this command to bind http://IP_ADDRESS:5000 and your Python logic together

How do I configure Gunicorn to serve my application?

Your application is now written with an entry point established and you can proceed to configuring Gunicorn. Next, you can check that Gunicorn can serve the application correctly by passing it the name of your entry point. This is constructed as the name of the module (minus the .py extension), plus the name of the callable within the application.


1 Answers

Do you mean you want to use nginx as a reverse proxy server for django with additional level of authorization? You simply move your auth_basic and auth_basic_user_file directives from location block to server block:

upstream gunicorn_server {
    server unix:</path/to/socket/pseudo/file>;
}

server {
    listen ...;
    server_name ...;
    auth_basic "Login Required";
    auth_basic_user_file etc/nginx/.htpasswd;
    ... # other parameters
    location / {
        try_files $uri @gunicorn;
    }
    location @gunicorn {
        proxy_pass http://gunicorn_server;
    }
}

Update

Assuming there is an "admin" area which includes both /admin.html and /admin/any/other/uri to additionaly protect this area with HTTP Basic Auth you can use following configuration:

upstream gunicorn_server {
    server unix:</path/to/socket/pseudo/file>;
}

server {
    listen ...;
    server_name ...;
    ... # other parameters
    location / {
        try_files $uri @gunicorn;
    }
    location /admin {
        auth_basic "Login Required";
        auth_basic_user_file etc/nginx/.htpasswd;
        try_files $uri @gunicorn;
    }
    location @gunicorn {
        proxy_pass http://gunicorn_server;
    }
}

To protect a single file admin.html replace location /admin { with location = /admin.html {.

like image 116
Ivan Shatsky Avatar answered Sep 26 '22 00:09

Ivan Shatsky