2020-11-24 14:33:34 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-11-16 19:28:10 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2021-01-16 16:20:24 +00:00
|
|
|
: "${WORKER_CONNECTIONS:=2048}"
|
2022-01-16 04:20:53 +00:00
|
|
|
: "${APP_PORT:=8080}"
|
|
|
|
: "${API_PORT:=8080}"
|
2021-12-27 23:46:11 +00:00
|
|
|
: "${NGINX_RESOLVER:=127.0.0.11}"
|
|
|
|
: "${BACKEND_SERVICE:=tactical-backend}"
|
|
|
|
: "${FRONTEND_SERVICE:=tactical-frontend}"
|
|
|
|
: "${MESH_SERVICE:=tactical-meshcentral}"
|
|
|
|
: "${WEBSOCKETS_SERVICE:=tactical-websockets}"
|
2021-09-06 16:54:37 +00:00
|
|
|
: "${DEV:=0}"
|
2021-01-02 05:05:37 +00:00
|
|
|
|
2022-01-06 02:17:21 +00:00
|
|
|
: "${CERT_PRIV_PATH:=${TACTICAL_DIR}/certs/privkey.pem}"
|
|
|
|
: "${CERT_PUB_PATH:=${TACTICAL_DIR}/certs/fullchain.pem}"
|
2020-11-17 02:22:28 +00:00
|
|
|
|
|
|
|
mkdir -p "${TACTICAL_DIR}/certs"
|
|
|
|
|
2020-11-19 03:42:45 +00:00
|
|
|
# remove default config
|
|
|
|
rm -f /etc/nginx/conf.d/default.conf
|
|
|
|
|
2020-11-17 02:22:28 +00:00
|
|
|
# check for certificates in env variable
|
2020-11-24 14:33:34 +00:00
|
|
|
if [ ! -z "$CERT_PRIV_KEY" ] && [ ! -z "$CERT_PUB_KEY" ]; then
|
2020-11-23 15:28:30 +00:00
|
|
|
echo "${CERT_PRIV_KEY}" | base64 -d > ${CERT_PRIV_PATH}
|
|
|
|
echo "${CERT_PUB_KEY}" | base64 -d > ${CERT_PUB_PATH}
|
2020-11-17 02:22:28 +00:00
|
|
|
else
|
2020-11-19 03:42:45 +00:00
|
|
|
# generate a self signed cert
|
|
|
|
if [ ! -f "${CERT_PRIV_PATH}" ] || [ ! -f "${CERT_PUB_PATH}" ]; then
|
2020-11-23 15:33:50 +00:00
|
|
|
rootdomain=$(echo ${API_HOST} | cut -d "." -f2- )
|
2020-11-19 03:42:45 +00:00
|
|
|
openssl req -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out ${CERT_PUB_PATH} -keyout ${CERT_PRIV_PATH} -subj "/C=US/ST=Some-State/L=city/O=Internet Widgits Pty Ltd/CN=*.${rootdomain}"
|
|
|
|
fi
|
2020-11-17 02:22:28 +00:00
|
|
|
fi
|
2020-11-16 19:28:10 +00:00
|
|
|
|
2021-10-04 17:04:29 +00:00
|
|
|
# increase default nginx worker connections
|
2021-01-16 16:20:24 +00:00
|
|
|
/bin/bash -c "sed -i 's/worker_connections.*/worker_connections ${WORKER_CONNECTIONS};/g' /etc/nginx/nginx.conf"
|
|
|
|
|
2021-09-06 16:54:37 +00:00
|
|
|
|
2021-09-10 16:20:40 +00:00
|
|
|
if [[ $DEV -eq 1 ]]; then
|
2021-09-06 16:54:37 +00:00
|
|
|
API_NGINX="
|
|
|
|
#Using variable to disable start checks
|
2021-12-27 23:46:11 +00:00
|
|
|
set \$api http://${BACKEND_SERVICE}:${API_PORT};
|
2021-09-06 16:54:37 +00:00
|
|
|
proxy_pass \$api;
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
proxy_cache_bypass \$http_upgrade;
|
|
|
|
|
|
|
|
proxy_set_header Upgrade \$http_upgrade;
|
|
|
|
proxy_set_header Connection \"upgrade\";
|
|
|
|
proxy_set_header Host \$host;
|
|
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
|
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
|
|
proxy_set_header X-Forwarded-Host \$host;
|
|
|
|
proxy_set_header X-Forwarded-Port \$server_port;
|
|
|
|
"
|
|
|
|
else
|
|
|
|
API_NGINX="
|
|
|
|
#Using variable to disable start checks
|
2021-12-27 23:46:11 +00:00
|
|
|
set \$api ${BACKEND_SERVICE}:${API_PORT};
|
2021-09-06 16:54:37 +00:00
|
|
|
|
|
|
|
include uwsgi_params;
|
|
|
|
uwsgi_pass \$api;
|
|
|
|
"
|
|
|
|
fi
|
|
|
|
|
2020-11-17 02:22:28 +00:00
|
|
|
nginx_config="$(cat << EOF
|
2020-11-16 19:28:10 +00:00
|
|
|
# backend config
|
|
|
|
server {
|
2021-12-27 23:46:11 +00:00
|
|
|
resolver ${NGINX_RESOLVER} valid=30s;
|
2020-11-16 19:28:10 +00:00
|
|
|
|
|
|
|
server_name ${API_HOST};
|
|
|
|
|
|
|
|
location / {
|
2021-09-06 16:54:37 +00:00
|
|
|
${API_NGINX}
|
2020-11-16 19:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
location /static/ {
|
|
|
|
root ${TACTICAL_DIR}/api;
|
|
|
|
}
|
|
|
|
|
|
|
|
location /private/ {
|
|
|
|
internal;
|
|
|
|
add_header "Access-Control-Allow-Origin" "https://${APP_HOST}";
|
|
|
|
alias ${TACTICAL_DIR}/api/tacticalrmm/private/;
|
|
|
|
}
|
|
|
|
|
2021-04-03 21:50:18 +00:00
|
|
|
location ~ ^/ws/ {
|
2021-12-27 23:46:11 +00:00
|
|
|
set \$api http://${WEBSOCKETS_SERVICE}:8383;
|
2021-04-03 21:50:18 +00:00
|
|
|
proxy_pass \$api;
|
|
|
|
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
proxy_set_header Upgrade \$http_upgrade;
|
|
|
|
proxy_set_header Connection "upgrade";
|
|
|
|
|
|
|
|
proxy_redirect off;
|
|
|
|
proxy_set_header Host \$host;
|
|
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
|
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Forwarded-Host \$server_name;
|
2021-01-16 02:05:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 19:28:10 +00:00
|
|
|
client_max_body_size 300M;
|
|
|
|
|
2022-01-16 04:20:53 +00:00
|
|
|
listen 4443 ssl;
|
2020-11-17 02:22:28 +00:00
|
|
|
ssl_certificate ${CERT_PUB_PATH};
|
|
|
|
ssl_certificate_key ${CERT_PRIV_PATH};
|
2020-11-16 19:28:10 +00:00
|
|
|
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
2022-01-16 04:20:53 +00:00
|
|
|
listen 8080;
|
2020-11-16 19:28:10 +00:00
|
|
|
server_name ${API_HOST};
|
2020-11-19 03:42:45 +00:00
|
|
|
return 301 https://\$server_name\$request_uri;
|
2020-11-16 19:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# frontend config
|
|
|
|
server {
|
2021-12-27 23:46:11 +00:00
|
|
|
resolver ${NGINX_RESOLVER} valid=30s;
|
2020-11-16 19:28:10 +00:00
|
|
|
|
|
|
|
server_name ${APP_HOST};
|
|
|
|
|
|
|
|
location / {
|
|
|
|
#Using variable to disable start checks
|
2021-12-27 23:46:11 +00:00
|
|
|
set \$app http://${FRONTEND_SERVICE}:${APP_PORT};
|
2020-11-16 19:28:10 +00:00
|
|
|
|
2020-11-19 03:42:45 +00:00
|
|
|
proxy_pass \$app;
|
2020-11-16 19:28:10 +00:00
|
|
|
proxy_http_version 1.1;
|
2020-11-19 03:42:45 +00:00
|
|
|
proxy_cache_bypass \$http_upgrade;
|
2020-11-16 19:28:10 +00:00
|
|
|
|
2020-11-19 03:42:45 +00:00
|
|
|
proxy_set_header Upgrade \$http_upgrade;
|
2020-11-16 19:28:10 +00:00
|
|
|
proxy_set_header Connection "upgrade";
|
2020-11-19 03:42:45 +00:00
|
|
|
proxy_set_header Host \$host;
|
|
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
|
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
|
|
proxy_set_header X-Forwarded-Host \$host;
|
|
|
|
proxy_set_header X-Forwarded-Port \$server_port;
|
2020-11-16 19:28:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 04:20:53 +00:00
|
|
|
listen 4443 ssl;
|
2020-11-17 02:22:28 +00:00
|
|
|
ssl_certificate ${CERT_PUB_PATH};
|
|
|
|
ssl_certificate_key ${CERT_PRIV_PATH};
|
2020-11-16 19:28:10 +00:00
|
|
|
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
2022-01-16 04:20:53 +00:00
|
|
|
listen 8080;
|
2020-11-16 19:28:10 +00:00
|
|
|
server_name ${APP_HOST};
|
2020-11-19 03:42:45 +00:00
|
|
|
return 301 https://\$server_name\$request_uri;
|
2020-11-16 19:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# meshcentral config
|
|
|
|
server {
|
2021-12-27 23:46:11 +00:00
|
|
|
resolver ${NGINX_RESOLVER} valid=30s;
|
2020-11-16 19:28:10 +00:00
|
|
|
|
2022-01-16 04:20:53 +00:00
|
|
|
listen 4443 ssl;
|
2020-11-16 19:28:10 +00:00
|
|
|
proxy_send_timeout 330s;
|
|
|
|
proxy_read_timeout 330s;
|
|
|
|
server_name ${MESH_HOST};
|
2020-11-17 02:22:28 +00:00
|
|
|
ssl_certificate ${CERT_PUB_PATH};
|
|
|
|
ssl_certificate_key ${CERT_PRIV_PATH};
|
2020-11-16 19:28:10 +00:00
|
|
|
ssl_session_cache shared:WEBSSL:10m;
|
|
|
|
ssl_ciphers HIGH:!aNULL:!MD5;
|
|
|
|
ssl_prefer_server_ciphers on;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
#Using variable to disable start checks
|
2021-12-29 14:26:15 +00:00
|
|
|
set \$meshcentral http://${MESH_SERVICE}:443;
|
2020-11-16 19:28:10 +00:00
|
|
|
|
2020-11-19 03:42:45 +00:00
|
|
|
proxy_pass \$meshcentral;
|
2020-11-16 19:28:10 +00:00
|
|
|
proxy_http_version 1.1;
|
|
|
|
|
2020-11-19 03:42:45 +00:00
|
|
|
proxy_set_header Upgrade \$http_upgrade;
|
2020-11-16 19:28:10 +00:00
|
|
|
proxy_set_header Connection "upgrade";
|
|
|
|
|
2020-11-19 03:42:45 +00:00
|
|
|
proxy_set_header Host \$host;
|
|
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
|
|
|
proxy_set_header X-Forwarded-Host \$host:\$server_port;
|
|
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
2020-11-16 19:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
2021-12-27 23:46:11 +00:00
|
|
|
resolver ${NGINX_RESOLVER} valid=30s;
|
2020-11-16 19:28:10 +00:00
|
|
|
|
2022-01-16 04:20:53 +00:00
|
|
|
listen 8080;
|
2020-11-16 19:28:10 +00:00
|
|
|
server_name ${MESH_HOST};
|
2020-11-19 03:42:45 +00:00
|
|
|
return 301 https://\$server_name\$request_uri;
|
2020-11-16 19:28:10 +00:00
|
|
|
}
|
2020-11-17 02:22:28 +00:00
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
|
2020-11-19 03:42:45 +00:00
|
|
|
echo "${nginx_config}" > /etc/nginx/conf.d/default.conf
|