196 lines
6.1 KiB
Bash
Executable File
196 lines
6.1 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
unset _NGX_WORKER_PROCESSES _NGX_WORKER_PRIORITY _NGX_WORKER_RLIMIT_NOFILE _NGX_WORKER_CONNECTIONS _NGX_WORKER_AIO_REQUESTS
|
|
## here should be SANE defaults (!)
|
|
_NGX_WORKER_PROCESSES=2
|
|
_NGX_WORKER_PRIORITY=0
|
|
_NGX_WORKER_RLIMIT_NOFILE=16384
|
|
_NGX_WORKER_CONNECTIONS=4096
|
|
_NGX_WORKER_AIO_REQUESTS=64
|
|
|
|
[ -n "${NGX_WORKER_PROCESSES:-}" ] || NGX_WORKER_PROCESSES=${_NGX_WORKER_PROCESSES}
|
|
case "${NGX_WORKER_PROCESSES}" in
|
|
## allow values within [1;999]
|
|
[1-9] | [1-9][0-9] | [1-9][0-9][0-9] ) ;;
|
|
[Aa][Uu][Tt][Oo] )
|
|
## adjust
|
|
NGX_WORKER_PROCESSES=auto
|
|
log_always "NGX_WORKER_PROCESSES: \"auto\" isn't supported by container yet"
|
|
log_always "offloading decision to Angie (this could be a problem!)"
|
|
;;
|
|
0 )
|
|
log_always "NGX_WORKER_PROCESSES: \"0\" isn't supported by container yet"
|
|
log_always "setting NGX_WORKER_PROCESSES=${_NGX_WORKER_PROCESSES}"
|
|
NGX_WORKER_PROCESSES=${_NGX_WORKER_PROCESSES}
|
|
;;
|
|
* )
|
|
log_always "NGX_WORKER_PROCESSES: unrecognized value: ${NGX_WORKER_PROCESSES}"
|
|
log_always "setting NGX_WORKER_PROCESSES=${_NGX_WORKER_PROCESSES}"
|
|
NGX_WORKER_PROCESSES=${_NGX_WORKER_PROCESSES}
|
|
;;
|
|
esac
|
|
export NGX_WORKER_PROCESSES
|
|
|
|
if [ -z "${NGX_WORKER_CPU_AFFINITY:-}" ] ; then
|
|
unset NGX_WORKER_CPU_AFFINITY
|
|
else
|
|
## offload handling to Angie
|
|
set -a
|
|
NGX_WORKER_CPU_AFFINITY=$(normalize_list "${NGX_WORKER_CPU_AFFINITY}")
|
|
set +a
|
|
fi
|
|
|
|
[ -n "${NGX_WORKER_CONNECTIONS:-}" ] || NGX_WORKER_CONNECTIONS=${_NGX_WORKER_CONNECTIONS}
|
|
case "${NGX_WORKER_CONNECTIONS}" in
|
|
[0-9] | [1-9][0-9] )
|
|
log_always "NGX_WORKER_CONNECTIONS: too low: ${NGX_WORKER_CONNECTIONS}"
|
|
log_always "setting NGX_WORKER_CONNECTIONS=${_NGX_WORKER_CONNECTIONS}"
|
|
NGX_WORKER_CONNECTIONS=${_NGX_WORKER_CONNECTIONS}
|
|
;;
|
|
## allow values within [100;9999999]
|
|
[1-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9][0-9][0-9][0-9] ) ;;
|
|
* )
|
|
log_always "NGX_WORKER_CONNECTIONS: unrecognized value: ${NGX_WORKER_CONNECTIONS}"
|
|
log_always "setting NGX_WORKER_CONNECTIONS=${_NGX_WORKER_CONNECTIONS}"
|
|
NGX_WORKER_CONNECTIONS=${_NGX_WORKER_CONNECTIONS}
|
|
;;
|
|
esac
|
|
export NGX_WORKER_CONNECTIONS
|
|
|
|
if [ -z "${NGX_WORKER_PRIORITY:-}" ] ; then
|
|
unset NGX_WORKER_PRIORITY
|
|
else
|
|
case "${NGX_WORKER_PRIORITY}" in
|
|
-[1-9] | -1[0-9] | -20 ) ;;
|
|
[0-9] | 1[0-9] | 20 ) ;;
|
|
-0 )
|
|
log_always "NGX_WORKER_PRIORITY: likely an error: '-0'"
|
|
log_always "adjusting NGX_WORKER_PRIORITY=0"
|
|
NGX_WORKER_PRIORITY=0
|
|
;;
|
|
* )
|
|
log_always "NGX_WORKER_PRIORITY: unrecognized value: ${NGX_WORKER_PRIORITY}"
|
|
log_always "setting NGX_WORKER_PRIORITY=${_NGX_WORKER_PRIORITY}"
|
|
NGX_WORKER_PRIORITY=${_NGX_WORKER_PRIORITY}
|
|
;;
|
|
esac
|
|
export NGX_WORKER_PRIORITY
|
|
fi
|
|
|
|
if [ -z "${NGX_WORKER_RLIMIT_NOFILE:-}" ] ; then
|
|
unset NGX_WORKER_RLIMIT_NOFILE
|
|
else
|
|
case "${NGX_WORKER_RLIMIT_NOFILE}" in
|
|
[0-9] | [1-9][0-9] )
|
|
log_always "NGX_WORKER_RLIMIT_NOFILE: too low: ${NGX_WORKER_RLIMIT_NOFILE}"
|
|
log_always "setting NGX_WORKER_RLIMIT_NOFILE=${_NGX_WORKER_RLIMIT_NOFILE}"
|
|
NGX_WORKER_RLIMIT_NOFILE=${_NGX_WORKER_RLIMIT_NOFILE}
|
|
;;
|
|
## allow values within [100;9999999]
|
|
[1-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9][0-9][0-9][0-9] ) ;;
|
|
* )
|
|
log_always "NGX_WORKER_RLIMIT_NOFILE: unrecognized value: ${NGX_WORKER_RLIMIT_NOFILE}"
|
|
log_always "setting NGX_WORKER_RLIMIT_NOFILE=${_NGX_WORKER_RLIMIT_NOFILE}"
|
|
NGX_WORKER_RLIMIT_NOFILE=${_NGX_WORKER_RLIMIT_NOFILE}
|
|
;;
|
|
esac
|
|
export NGX_WORKER_RLIMIT_NOFILE
|
|
fi
|
|
|
|
if [ -z "${NGX_WORKER_AIO_REQUESTS:-}" ] ; then
|
|
unset NGX_WORKER_AIO_REQUESTS
|
|
else
|
|
case "${NGX_WORKER_AIO_REQUESTS}" in
|
|
[0-9] )
|
|
log_always "NGX_WORKER_AIO_REQUESTS: too low: ${NGX_WORKER_AIO_REQUESTS}"
|
|
log_always "setting NGX_WORKER_AIO_REQUESTS=${_NGX_WORKER_AIO_REQUESTS}"
|
|
NGX_WORKER_AIO_REQUESTS=${_NGX_WORKER_AIO_REQUESTS}
|
|
;;
|
|
## allow values within [10;99999]
|
|
[1-9][0-9] ) ;;
|
|
[1-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9] ) ;;
|
|
[1-9][0-9][0-9][0-9][0-9] ) ;;
|
|
* )
|
|
log_always "NGX_WORKER_AIO_REQUESTS: unrecognized value: ${NGX_WORKER_AIO_REQUESTS}"
|
|
log_always "setting NGX_WORKER_AIO_REQUESTS=${_NGX_WORKER_AIO_REQUESTS}"
|
|
NGX_WORKER_AIO_REQUESTS=${_NGX_WORKER_AIO_REQUESTS}
|
|
;;
|
|
esac
|
|
export NGX_WORKER_AIO_REQUESTS
|
|
fi
|
|
|
|
if [ -n "${NGX_WORKER_RLIMIT_NOFILE:-}" ] ; then
|
|
nofile_soft=$(ulimit -Sn)
|
|
nofile_hard=$(ulimit -Hn)
|
|
|
|
if [ "${nofile_hard}" = unlimited ] ; then
|
|
## minor hack (if applicable) :)
|
|
nofile_hard=$((NGX_WORKER_RLIMIT_NOFILE + 1))
|
|
fi
|
|
|
|
nofile_ok=0
|
|
while : ; do
|
|
[ ${nofile_hard} -ge ${NGX_WORKER_RLIMIT_NOFILE} ] || break
|
|
[ ${nofile_soft} -ge ${NGX_WORKER_RLIMIT_NOFILE} ] || break
|
|
|
|
nofile_ok=1
|
|
break ; done
|
|
|
|
if [ ${nofile_ok} = 0 ] ; then
|
|
log_always "adjusting 'nofile' limits"
|
|
|
|
log_always "Limits before:"
|
|
sed -En '1p;/open files/p' < /proc/$$/limits >&2
|
|
|
|
if [ ${nofile_hard} -lt ${NGX_WORKER_RLIMIT_NOFILE} ] ; then
|
|
ulimit -Hn "${NGX_WORKER_RLIMIT_NOFILE}"
|
|
nofile_hard=$(ulimit -Hn)
|
|
fi
|
|
if [ ${nofile_hard} -lt ${NGX_WORKER_RLIMIT_NOFILE} ] ; then
|
|
log_always "lowering NGX_WORKER_RLIMIT_NOFILE to ${nofile_hard} due to hard limit"
|
|
NGX_WORKER_RLIMIT_NOFILE=${nofile_hard}
|
|
fi
|
|
|
|
if [ ${nofile_soft} -lt ${NGX_WORKER_RLIMIT_NOFILE} ] ; then
|
|
ulimit -Sn "${NGX_WORKER_RLIMIT_NOFILE}"
|
|
fi
|
|
|
|
log_always "Limits after:"
|
|
sed -En '1p;/open files/p' < /proc/$$/limits >&2
|
|
fi
|
|
unset nofile_soft nofile_hard nofile_ok
|
|
|
|
export NGX_WORKER_RLIMIT_NOFILE
|
|
fi
|
|
|
|
if [ -z "${NGX_WORKER_RLIMIT_NOFILE:-}" ] ; then
|
|
nofile_limit=$(ulimit -Hn)
|
|
nofile_kind="'ulimit:nofile'"
|
|
else
|
|
nofile_limit=${NGX_WORKER_RLIMIT_NOFILE}
|
|
nofile_kind='NGX_WORKER_RLIMIT_NOFILE'
|
|
fi
|
|
if [ ${nofile_limit} -lt ${NGX_WORKER_CONNECTIONS} ] ; then
|
|
log_always "WARNING: ${nofile_kind} is less than NGX_WORKER_CONNECTIONS (${nofile_limit} < ${NGX_WORKER_CONNECTIONS})"
|
|
else
|
|
ratio=$(float_div "${nofile_limit}" "${NGX_WORKER_CONNECTIONS}")
|
|
case "${ratio}" in
|
|
1 | 1.* )
|
|
log_always "WARNING: \"${nofile_kind}/NGX_WORKER_CONNECTIONS\" ratio is too low (=${ratio})"
|
|
;;
|
|
esac
|
|
unset ratio
|
|
fi
|
|
unset nofile_limit nofile_kind
|
|
|
|
unset _NGX_WORKER_PROCESSES _NGX_WORKER_PRIORITY _NGX_WORKER_RLIMIT_NOFILE _NGX_WORKER_CONNECTIONS _NGX_WORKER_AIO_REQUESTS
|