71 lines
2.1 KiB
Bash
Executable File
71 lines
2.1 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
|
|
normalize_path() {
|
|
value="${1:-}"
|
|
value="/$(printf '%s' "$value" | sed -e 's#^/*##' -e 's#/*$##')"
|
|
if [ "$value" = "/" ]; then
|
|
printf ''
|
|
else
|
|
printf '%s' "$value"
|
|
fi
|
|
}
|
|
|
|
external_path="$(normalize_path "${AISHARE_PROXY_EXTERNAL_PATH:-/aishare}")"
|
|
upstream_path="$(normalize_path "${AISHARE_UPSTREAM_PATH:-/aishare}")"
|
|
upstream_origin="$(printf '%s' "${AISHARE_UPSTREAM_ORIGIN:?set AISHARE_UPSTREAM_ORIGIN}" | sed -e 's#/*$##')"
|
|
listen_port="${AISHARE_PROXY_LISTEN:-8080}"
|
|
server_name="${AISHARE_PROXY_SERVER_NAME:-_}"
|
|
client_max_body_size="${AISHARE_PROXY_CLIENT_MAX_BODY_SIZE:-20m}"
|
|
external_slash="${external_path}/"
|
|
upstream_slash="${upstream_path}/"
|
|
if [ -z "$external_path" ]; then external_slash="/"; fi
|
|
if [ -z "$upstream_path" ]; then upstream_slash="/"; fi
|
|
|
|
cat > /etc/nginx/conf.d/default.conf <<EOF
|
|
server {
|
|
listen ${listen_port};
|
|
server_name ${server_name};
|
|
|
|
client_max_body_size ${client_max_body_size};
|
|
|
|
proxy_http_version 1.1;
|
|
proxy_ssl_server_name on;
|
|
proxy_set_header Host \$proxy_host;
|
|
proxy_set_header X-Forwarded-Host \$http_host;
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
proxy_set_header X-Forwarded-Prefix ${external_path:-/};
|
|
proxy_set_header X-Real-Ip \$remote_addr;
|
|
EOF
|
|
|
|
write_proxy_block() {
|
|
location_line="$1"
|
|
rewrite_line="$2"
|
|
cat >> /etc/nginx/conf.d/default.conf <<EOF
|
|
|
|
${location_line} {
|
|
${rewrite_line}
|
|
proxy_redirect ${upstream_slash} ${external_slash};
|
|
proxy_redirect ${upstream_origin}${upstream_slash} \$scheme://\$http_host${external_slash};
|
|
proxy_pass ${upstream_origin};
|
|
}
|
|
EOF
|
|
}
|
|
|
|
if [ -z "$external_path" ]; then
|
|
write_proxy_block "location /" "rewrite ^/(.*)\$ ${upstream_path}/\$1 break;"
|
|
else
|
|
cat >> /etc/nginx/conf.d/default.conf <<EOF
|
|
|
|
location = ${external_path} {
|
|
return 301 ${external_path}/;
|
|
}
|
|
EOF
|
|
write_proxy_block "location ^~ ${external_path}/" "rewrite ^${external_path}/?(.*)\$ ${upstream_path}/\$1 break;"
|
|
fi
|
|
|
|
cat >> /etc/nginx/conf.d/default.conf <<'EOF'
|
|
}
|
|
EOF
|