# ADMINISTER CUPS AT http://box/print -- USERNAME 'Admin' & PASSWORD 'changeme'
# (OR ANY MEMBER OF LINUX GROUP 'lpadmin') PER cups/tasks/install.yml


# 2021-07-13: Let's redirect to CUPS' own web server for now, as proxying
# (commented out below) has many glitches, e.g. CUPS' https connections etc.

location ~ ^/print(|/.*)$ {    # '~' -> '~*' for case-insensitive regex

    # 2021-07-13: Work around CUPS failure to serve http://box[.lan]:631 "since
    # 2009" -- e.g. '400 Bad Request' error 'Request from "localhost" using
    # invalid Host: field "box[.lan]:631".' in /var/log/cups/error_log, DESPITE
    # adding 'HostNameLookups On', 'ServerAlias *' etc to /etc/cups/cupsd.conf
    # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=530027

    if ($host ~ '{{ iiab_hostname }}(|.{{ iiab_domain }})') {
        return 301 http://localhost:631;    # Or http://127.0.0.1:631
    }

    if ($host ~ 'box(|.lan)') {    # /etc/hosts may have BOTH above AND box.lan
        return 301 http://localhost:631;
    }

    return 301 http://$host:631;   # For 192.168.0.x, 10.10.10.10, 172.18.96.1, 10.8.0.y ETC
}


# https://anthe.studio/blog/en/cups-nginx-reverse-proxy
# https://toggen.com.au/it-tips/reverse-proxy-cups-in-nginx/
# https://www.robpeck.com/2020/09/proxying-cups-ipp-using-nginx/

# location = /print {
#     return 301 /print/;    # "Moved Permanently" redirect
#     #rewrite /print /print/;    # Faster, if links are fixed!
# }

## location ~ ^/print(|/.*)$ {
##     proxy_pass https://127.0.0.1:631$1;    # Fails: trailing slash nec here
# location ~ ^/print/(.*) {
#     proxy_pass https://127.0.0.1:631/$1;
#
#     #proxy_http_version 1.1;
#     #proxy_set_header Accept-Encoding "";
#     #proxy_set_header Upgrade $http_upgrade;
#     #proxy_set_header Connection 'upgrade';
#     proxy_set_header Host '127.0.0.1';
#     proxy_cache_bypass $http_upgrade;
#
#     proxy_set_header X-Real-IP $remote_addr;
#     #proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
#     #proxy_set_header X-Forwarded-Host $server_name;
#
#     sub_filter ' href="/' ' href="/print/';
#     sub_filter ' action="/' ' action="/print/';
#     sub_filter ' src="/' ' src="/print/';
#     #sub_filter 'ACTION="/' 'ACTION="/print/';
#     #sub_filter 'URL=/' 'URL=/print/';
#     sub_filter_types *;
#     sub_filter_once off;
# }

# location ~ /cups/(.*) {
#     proxy_pass http://127.0.0.1:631/$1;
#     proxy_set_header Host '127.0.0.1';
#     proxy_cache_bypass $http_upgrade;
#     proxy_set_header X-Real-IP $remote_addr;
#
#     sub_filter ' href="/' ' href="/cups/';
#     sub_filter ' action="/' ' action="/cups/';
#     sub_filter ' src="/' ' src="/cups/';
#     sub_filter_types *;
#     sub_filter_once off;
# }
