I want to ensure that communication on ports other than 80 is valid HTTP. Since nginx can also be used as a transparent forward proxy, I installed it on a firewall running pf on FreeBSD 12.
# make -C /usr/ports/www/nginx config install cleanUncheck any options but these:
http { ... server { listen 127.0.0.1:8011 accept_filter=httpready reuseport; proxy_pass_header server; proxy_pass_header date; resolver 127.0.0.1; location / { proxy_pass $scheme://$http_host$request_uri; } } ... }Using this configuration, nginx will preserve the Server: and Date: header fields in the response from the origin server. Additionally, it will make use of the accf_http kernel module which buffers new connections until it has detected a complete HTTP request.
# echo 'accf_http_load="YES"' >> /boot/loader.conf
# sysrc nginx_enable="YES"
rdr on $LANIF inet proto tcp from port >= 1024 to ! ($LANIF) port 8080 -> (lo0) port 8011 pass out quick on $WANIF inet proto tcp from port >= 1024 to port 8080 user wwwReplace $LANIF by the name of the interface where your local network is connected to, i.e. where your clients reside on. Likewise, replace $WANIF by the name of your external interface.
# sysrc pf_enable="YES"
# kldload accf_http # service nginx start # service pf start
If you now use this firewall as your default gateway, you can open webpages that are served on port 8080 whilst non HTTP traffic to port 8080 is rejected by nginx.