-1

I would like to configure nginx this way, that only extern page visitors (not 192.169.1.0/24) need to authenticate using client-side SSL certificates.

I already found, that extern and intern users can be distinguished by the geo module:

http {
  geo $isintern {
    default 0;
    192.169.1.0/24 1;
}

via: https://stackoverflow.com/questions/14811532/client-authentication-when-using-nginx-proxy-pass

How do I have to configure the server? I got to know, that one should try to circumvent if statements. Finally I would like to redirect people with access to an 403/401 error page.

upstream serverapp {
    server unix:/opt/a.socket;
}

server {
    listen        443;
    ssl on;
    server_name example.com;

    ssl_certificate      /etc/nginx/certs/server.crt;
    ssl_certificate_key  /etc/nginx/certs/server.key;
    ssl_client_certificate /etc/nginx/certs/ca.crt;
    ssl_verify_client optional;

    if(!$isintern) {
        if($ssl_client_verify != SUCCESS) {
            return 403;
        }
    }

    error_page 403 401 /401.html

    location / {
      proxy_pass http://serverapp;
    }
}

The config above is not tested. How is it meant to be implemented?

Resources:

3
  • What do you mean by "How is it meant to be implemented?" You can use return and other directives of the HttpRewriteModule inside if.
    – Lukas
    Mar 18, 2013 at 0:35
  • 1
    192.169.1.0/24 would also allow externs from IP range 192.169.1.* to connect without SSL. This is best solved with a packet filter blocking this range (not with nginx).
    – cgwid
    Sep 4, 2015 at 19:26

1 Answer 1

-1

You need two server {} blocks, one for external and one for internal access. Nginx won't care if they point to the same root on the filesystem. Ideally make a separate .conf file for each and put it in a directory included by the main nginx.conf or other config file.

Each server {} block can have its own certificates, its own server_name directive for SNI to work properly, and you can limit access by IP of the internal site with location / { allow IP/netmask; allow IP/netmask; deny all; }

1
  • Have you tested this? I suspect it won't work as the two server {} blocks will conflict before the allow masks are even considered. I could be wrong of course.
    – zaTricky
    Oct 28, 2016 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .