paivana

HTTP paywall reverse proxy
Log | Files | Refs | Submodules | README | LICENSE

nginx-paivana (3066B)


      1 # RFC 7239 node identifier for the peer we accepted from.  nginx has
      2 # no built-in variable for this: an IPv6 address has to be bracketed
      3 # and therefore quoted (RFC 7239 §6), and a peer with no address is
      4 # "unknown" (§6.3).
      5 map $remote_addr $paivana_forwarded_elem {
      6   ~^[0-9.]+$        "for=$remote_addr";
      7   ~^[0-9A-Fa-f:.]+$ "for=\"[$remote_addr]\"";
      8   default           "for=unknown";
      9 }
     10 
     11 # Optional edge-side concurrency control.  Paivana cannot enforce a useful
     12 # per-client-address limit through its Unix socket, because every connection
     13 # has the same local peer.  If your client population is not concentrated
     14 # behind large NATs, uncomment this zone and the `limit_conn' below.  Twenty is
     15 # an example operational policy, not a Paivana resource formula: lower values
     16 # reject abusive concurrency sooner but can also reject legitimate users who
     17 # share one public address.  If another proxy is in front, configure nginx's
     18 # trusted real-IP handling before keying this zone on $binary_remote_addr.
     19 # limit_conn_zone $binary_remote_addr zone=paivana_clients:10m;
     20 
     21 server {
     22   listen 80;
     23   listen [::]:80;
     24 
     25   # server_name example.com
     26 
     27   location / {
     28     # limit_conn paivana_clients 20;
     29     proxy_pass http://unix:/run/paivana/httpd/paivana-http.sock;
     30     proxy_redirect off;
     31     proxy_set_header Host $host;
     32 
     33     # paivana-httpd is started with -f (see paivana-httpd.service), so
     34     # it takes the client address for the access cookie from the
     35     # headers set here.  It believes the RIGHTMOST element of the
     36     # chain, which is whatever the hop it accepted from wrote -- so
     37     # what matters is that this server writes these headers at all.
     38     # $remote_addr is the peer we actually accepted; the danger is not
     39     # $proxy_add_x_forwarded_for (appending is safe, since our own
     40     # element still ends up rightmost) but leaving a header unset and
     41     # letting nginx forward the client's copy of it verbatim.
     42     #
     43     # If this nginx is itself behind another proxy, switch to
     44     # $proxy_add_x_forwarded_for, set real_ip_header /
     45     # set_real_ip_from for that hop, and list it in paivana's
     46     # TRUSTED_PROXIES so the walk may step past it.
     47     proxy_set_header X-Forwarded-For   $remote_addr;
     48     proxy_set_header X-Forwarded-Proto $scheme;
     49     proxy_set_header X-Forwarded-Host  $host;
     50     proxy_set_header X-Forwarded-Port  $server_port;
     51 
     52     # RFC 7239.  paivana-httpd prefers this over the X-Forwarded-*
     53     # headers above, which are kept for origins that only speak those.
     54     # Setting it is not optional: nginx forwards a client-supplied
     55     # Forwarded header verbatim, and since paivana prefers this header
     56     # over X-Forwarded-For, omitting this line would hand the client
     57     # the element paivana believes -- however carefully the
     58     # X-Forwarded-For above is set.  Behind another proxy, replace this
     59     # with the appending form from nginx.org's "Using the Forwarded
     60     # header", which validates $http_forwarded before extending it.
     61     proxy_set_header Forwarded "$paivana_forwarded_elem;proto=$scheme;host=$host";
     62   }
     63 }