nginxinc / nginx-ldap-auth

Example of LDAP authentication using ngx_http_auth_request_module
BSD 2-Clause "Simplified" License
686 stars 202 forks source link

Unable to push logs of py daemon to a separate file #97

Closed rajdeep-2001 closed 2 years ago

rajdeep-2001 commented 2 years ago

We are running this module on Nginx Plus AMI EC2 instance i.e. no containerisation. Nginx conf is fine. However, unable to push logs to a separate file and have that propagated to Cloudwatch. $LOG env var is not working OR please suggest how to set $LOG var to push the standard output and error logs to a different file. Updating the documentation with LOG setup would be really useful. Much appreciated!!

Py daemon has been setup as a systemd unit. systemd v219

Unit file:

[Unit]
Description=LDAP authentication helper daemon for Nginx.
Documentation=https://github.com/nginxinc/nginx-ldap-auth
After=network.target network-online.target

[Service]
Type=simple
Environment="LOG=/var/log/nginx/ldap_auth_daemon.log"         # does not work
ExecStart=/bin/python3 /etc/nginx-ldap/nginx-ldap-auth-daemon.py
StandardOutput=append:/var/log/nginx/ldap_auth_daemon.log  # does not work probably bcoz of systemd version.
StandardError=append:/var/log/nginx/ldap_auth_daemon.log.    # does not work probably bcoz of systemd version.
KillMode=process
KillSignal=SIGINT
Restart=on-failure

[Install]
WantedBy=multi-user.target

nginx-ldap-auth.default

# these are used with systemd too
# so please keep options names inside variables

LOG=/var/log/nginx/ldap_auth_daemon.log                  # does not work.
tippexs commented 2 years ago

What OS are you using? The ideal way to forward stdout to a file using systemd is the way you already explained above. Another one is to redirect to log stream to a file using ExecStart=/bin/python3 /etc/nginx-ldap/nginx-ldap-auth-daemon.py 2>&1 >> /var/log/your.log.

This will append the stdout as well as the stderr to a custom logfile.

rajdeep-2001 commented 2 years ago

We are using Amazon linux2 Nginx Plus AMI as a subscription from the market place. os-release

NAME="Amazon Linux" VERSION="2" ID="amzn" ID_LIKE="centos rhel fedora" VERSION_ID="2" PRETTY_NAME="Amazon Linux 2" HOME_URL="https://amazonlinux.com/"

rajdeep-2001 commented 2 years ago

Thanks and as you suggested, below redirection works:- ExecStart=/bin/sh -c 'exec /bin/python3 /etc/nginx-ldap/nginx-ldap-auth-daemon.py 2>&1 >> /var/log/nginx/ldap_auth_daemon.log'

However, the service/process writes to the above redirected log file only when the systemd service is stopped i.e. it does not write in real-time.

rajdeep-2001 commented 2 years ago

Sorry for the delay, this issue is fixed, was a buffer issue. Can be closed. Thanks👍