changeset 2133:c3b4c19f2d8a

attempt to autodetect and use nginx confs dir
author Violet7
date Fri, 16 Jan 2026 17:29:05 -0800
parents 44f47df52976
children de3107eb911f
files host/doc/install.txt host/start_nginx.sh host/startup/nginx/nginx.conf.luan host/startup/nginx/nginx.default.conf.luan host/update2.sh src/luan/host/Https.luan
diffstat 6 files changed, 46 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/host/doc/install.txt	Fri Jan 16 16:56:38 2026 -0800
+++ b/host/doc/install.txt	Fri Jan 16 17:29:05 2026 -0800
@@ -34,9 +34,6 @@
    nginx config can exist in many places, on old macOS it is usually /usr/local/etc/nginx/nginx.conf
    can also exist at /usr/local/nginx/conf/nginx.conf or /etc/nginx/nginx.conf
 
-   add this line at the bottom of the `http` block, after `include servers/*` or `include conf.d/*` or similar
-   include /Users/administrator/luan/host/local/nginx.conf;
-
    if nginx provides a default server, it conflicts with the luanhost one. remove or disable it.
    on old macOS this usually does not exist and this step is not needed
    sudo rm -r /etc/nginx/sites-enabled/default
--- a/host/start_nginx.sh	Fri Jan 16 16:56:38 2026 -0800
+++ b/host/start_nginx.sh	Fri Jan 16 17:29:05 2026 -0800
@@ -1,3 +1,4 @@
 #!/bin/bash
 
-sudo nginx;
+sudo nginx -t
+sudo nginx
--- a/host/startup/nginx/nginx.conf.luan	Fri Jan 16 16:56:38 2026 -0800
+++ b/host/startup/nginx/nginx.conf.luan	Fri Jan 16 17:29:05 2026 -0800
@@ -1,4 +1,4 @@
-local rootDir, user, group = ...
+local luanhostDir, nginxConfigDir, user, group = ...
 
 %>
 	proxy_cache_path /usr/local/nginx_cache levels=1:2 keys_zone=nginx_cache:60m max_size=10g inactive=60m use_temp_path=off;
@@ -7,17 +7,17 @@
 		# nginx usually has one at /etc/nginx/sites-enabled/default
 		listen 80 default_server;
 		listen [::]:80 default_server;
-		include <%=rootDir%>/local/nginx.default.conf;
+		include <%=nginxConfigDir%>/nginx.default.conf;
 
 		location /.well-known/acme-challenge/ {
 			# $host/ssl does not exist for non-ssl sites and requests to here
 			# will fail with 404 for those sites, which is what we want
-			alias <%=rootDir%>/sites/$host/ssl/acme-challenge/;
+			alias <%=luanhostDir%>/sites/$host/ssl/acme-challenge/;
 			try_files $uri $uri/ =404;
 		}
 	}
 
 	# glob pattern returns no results for site dirs that don't have 
 	# the ssl/ subdir, so this is ok
-	include <%=rootDir%>/sites/*/ssl/nginx.ssl.conf;
+	include <%=luanhostDir%>/sites/*/ssl/nginx.ssl.conf;
 <%
--- a/host/startup/nginx/nginx.default.conf.luan	Fri Jan 16 16:56:38 2026 -0800
+++ b/host/startup/nginx/nginx.default.conf.luan	Fri Jan 16 17:29:05 2026 -0800
@@ -1,4 +1,4 @@
-local rootDir = ...
+local luanhostDir = ...
 
 %>
 	client_max_body_size 32m;
@@ -7,7 +7,7 @@
 	proxy_set_header Host $http_host;
 	proxy_set_header Connection '';
 
-	error_log <%=rootDir%>/logs/nginx_error.log;
+	error_log <%=luanhostDir%>/logs/nginx_error.log;
 
 	proxy_set_header X-Forwarded-Proto $scheme;
 	proxy_set_header X-Real-IP $remote_addr;
--- a/host/update2.sh	Fri Jan 16 16:56:38 2026 -0800
+++ b/host/update2.sh	Fri Jan 16 17:29:05 2026 -0800
@@ -13,13 +13,30 @@
 	openssl genrsa 4096 >local/tiny_account.key
 fi
 
+nginx_conf_dir=$(
+	for d in \
+		/usr/local/etc/nginx/servers \
+		/etc/nginx/conf.d; do
+		if [ -d "$d" ]; then
+			echo "$d"
+			break
+		fi
+	done
+)
+
+if [ -n "$nginx_conf_dir" ]; then
+	echo "using nginx conf dir: $nginx_conf_dir"
+else
+	echo "ERROR: No nginx conf dir found.\nFind it and add it to update2.sh."
+	exit 1
+fi
+
 # id -gn gets the name of the primary group of the current user (staff)
-luan startup/nginx/nginx.conf.luan $(pwd) $(whoami) $(id -gn) >local/nginx.conf
-luan startup/nginx/nginx.default.conf.luan $(pwd) >local/nginx.default.conf
+luan startup/nginx/nginx.conf.luan $(pwd) $nginx_conf_dir $(whoami) $(id -gn) | sudo tee $nginx_conf_dir/luanhost.conf >/dev/null
+luan startup/nginx/nginx.default.conf.luan $(pwd) | sudo tee $nginx_conf_dir/luanhost.default.conf >/dev/null
 
 # this is done because the nginx conf uses absolute paths
 # and this breaks sites when the luan/host directory is moved
-# NOTE: if the luanhost root dir changes, the nginx include needs to be manually updated to reflect the new path.
 luan classpath:luan/host/update.luan
 
 echo Starting...
--- a/src/luan/host/Https.luan	Fri Jan 16 16:56:38 2026 -0800
+++ b/src/luan/host/Https.luan	Fri Jan 16 17:29:05 2026 -0800
@@ -28,6 +28,22 @@
 	local ssl_files_dir = site_dir.child("ssl/")
 	ssl_files_dir.mkdir()
 
+	local nginx_conf_paths {
+		"/usr/local/etc/nginx/servers",
+		"/etc/nginx/conf.d"
+	}
+    local real_nginx_confs_dir
+
+	for _,v in ipairs(nginx_conf_paths) do 
+		local dir = uri("file:"..v)
+		if v.is_directory() then
+			real_nginx_confs_dir = v
+			logger.info("using nginx confs dir "..v.canonical().to_string())
+			break
+		end
+		error("No nginx confs directory found. Manual intervention required.")
+	end
+
 	return {
 		ssl_files_dir = ssl_files_dir
 		nginx_file = ssl_files_dir.child("nginx.ssl.conf")
@@ -36,6 +52,7 @@
 		csr_file = ssl_files_dir.child(domain..".csr")
 		tmp_cert_out = ssl_files_dir.child(domain..".crt.tmp")
 		acme_challenges = ssl_files_dir.child("acme-challenge/")
+		nginx_confs_dir = real_nginx_confs_dir
 	}
 end
 
@@ -100,9 +117,7 @@
 		ssl_certificate <%= files.local_cer_file.to_string() %>;
 		ssl_certificate_key <%= files.key_file.to_string() %>;
 		# path is relative to the dir of the master nginx conf
-		include <%=luanhost_dir.canonical().to_string()%>/local/nginx.default.conf;
-		# allow users to upload custom nginx.*.conf files, e.g. for mixpanel
-		include <%=luanhost_dir.canonical().to_string()%>/sites/<%=domain%>/site/nginx.*.conf;
+		include <%=nginx_confs_dir.canonical().to_string()%>/luanhost.default.conf;
 	}
 <%
 end