|
| 1 | +#!/usr/bin/env sh |
| 2 | +set -e |
| 3 | + |
| 4 | +# Get the maximum upload file size for Nginx, default to 0: unlimited |
| 5 | +USE_NGINX_MAX_UPLOAD=${NGINX_MAX_UPLOAD:-0} |
| 6 | + |
| 7 | +# Get the number of workers for Nginx, default to 1 |
| 8 | +USE_NGINX_WORKER_PROCESSES=${NGINX_WORKER_PROCESSES:-1} |
| 9 | + |
| 10 | +# Set the max number of connections per worker for Nginx, if requested |
| 11 | +# Cannot exceed worker_rlimit_nofile, see NGINX_WORKER_OPEN_FILES below |
| 12 | +NGINX_WORKER_CONNECTIONS=${NGINX_WORKER_CONNECTIONS:-1024} |
| 13 | + |
| 14 | +# Get the listen port for Nginx, default to 80 |
| 15 | +USE_LISTEN_PORT=${LISTEN_PORT:-80} |
| 16 | + |
| 17 | +# Get html root path |
| 18 | +USE_HTML_ROOT=${USE_HTML_ROOT:-/html} |
| 19 | + |
| 20 | +if [ -f /app/nginx.conf ]; then |
| 21 | + cp /app/nginx.conf /etc/nginx/nginx.conf |
| 22 | +else |
| 23 | + content='user nginx;\n' |
| 24 | + # Set the number of worker processes in Nginx |
| 25 | + content=$content"worker_processes ${USE_NGINX_WORKER_PROCESSES};\n" |
| 26 | + content=$content'error_log /var/log/nginx/error.log warn;\n' |
| 27 | + content=$content'pid /var/run/nginx.pid;\n' |
| 28 | + content=$content'events {\n' |
| 29 | + content=$content" worker_connections ${NGINX_WORKER_CONNECTIONS};\n" |
| 30 | + content=$content'}\n' |
| 31 | + content=$content'http {\n' |
| 32 | + content=$content' include /etc/nginx/mime.types;\n' |
| 33 | + content=$content' default_type application/octet-stream;\n' |
| 34 | + content=$content' log_format main '"'\$remote_addr - \$remote_user [\$time_local] \"\$request\" '\n" |
| 35 | + content=$content' '"'\$status \$body_bytes_sent \"\$http_referer\" '\n" |
| 36 | + content=$content' '"'\"\$http_user_agent\" \"\$http_x_forwarded_for\"';\n" |
| 37 | + content=$content' access_log /var/log/nginx/access.log main;\n' |
| 38 | + content=$content' sendfile on;\n' |
| 39 | + content=$content' keepalive_timeout 65;\n' |
| 40 | + content=$content' include /etc/nginx/conf.d/*.conf;\n' |
| 41 | + content=$content'}\n' |
| 42 | + content=$content'daemon off;\n' |
| 43 | + # Set the max number of open file descriptors for Nginx workers, if requested |
| 44 | + if [ -n "${NGINX_WORKER_OPEN_FILES}" ] ; then |
| 45 | + content=$content"worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};\n" |
| 46 | + fi |
| 47 | + # Save generated /etc/nginx/nginx.conf |
| 48 | + printf "$content" > /etc/nginx/nginx.conf |
| 49 | + |
| 50 | + content_server='server {\n' |
| 51 | + content_server=$content_server" listen ${USE_LISTEN_PORT} default_server;\n" |
| 52 | + content_server=$content_server" listen [::]:${USE_LISTEN_PORT} default_server;\n" |
| 53 | + content_server=$content_server" server_name _;\n" |
| 54 | + content_server=$content_server" root ${USE_HTML_ROOT};\n" |
| 55 | + content_server=$content_server" index index.html index.htm index.nginx-debian.html;\n" |
| 56 | + content_server=$content_server' location ^~ /api {\n' |
| 57 | + content_server=$content_server' proxy_pass http://127.0.0.1:9000;\n' |
| 58 | + content_server=$content_server' proxy_set_header Upgrade $http_upgrade;\n' |
| 59 | + content_server=$content_server' proxy_set_header Connection "Upgrade";\n' |
| 60 | + content_server=$content_server' proxy_http_version 1.1;\n' |
| 61 | + content_server=$content_server' }\n' |
| 62 | + content_server=$content_server'}\n' |
| 63 | + # Save generated server /etc/nginx/conf.d/nginx.conf |
| 64 | + printf "$content_server" > /etc/nginx/conf.d/nginx.conf |
| 65 | + |
| 66 | + # Generate Nginx config for maximum upload file size |
| 67 | + printf "client_max_body_size $USE_NGINX_MAX_UPLOAD;\n" > /etc/nginx/conf.d/upload.conf |
| 68 | + |
| 69 | + # Remove default Nginx config from Alpine |
| 70 | + printf "" > /etc/nginx/conf.d/default.conf |
| 71 | +fi |
| 72 | + |
| 73 | +# For Alpine: |
| 74 | +# Explicitly add installed Python packages and uWSGI Python packages to PYTHONPATH |
| 75 | +# Otherwise uWSGI can't import Flask |
| 76 | +if [ -n "$ALPINEPYTHON" ] ; then |
| 77 | + export PYTHONPATH=$PYTHONPATH:/usr/local/lib/$ALPINEPYTHON/site-packages:/usr/lib/$ALPINEPYTHON/site-packages |
| 78 | +fi |
| 79 | +exec "$@" |
0 commit comments