Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions backend/src/nginx.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,22 @@ const NGINX_CONF_DIR = path.join(__dirname, '..', 'nginx', 'conf.d');
const SHOPS_LOCATIONS_FILE = path.join(NGINX_CONF_DIR, 'shops-locations.inc');

// Generate a path-based location block for a shop and append it to the
// shared shops-locations.inc file. This replaces the old per-shop
// subdomain server block approach so that shops are reachable at
// domain.com/<slug> instead of <slug>.domain.com.
// shared shops-locations.inc file. Shops are reachable at domain.com/<slug>/
// Next.js is configured with basePath=/<slug> so the full path is passed through.
function generateShopConfig(slug, port) {
const block = `
# Shop: ${slug}
location /${slug} {
rewrite ^/${slug}/(.*) /$1 break;
rewrite ^/${slug}$ / break;
proxy_pass http://host.docker.internal:${port};
location /${slug}/ {
proxy_pass http://127.0.0.1:${port};
proxy_http_version 1.1;
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-Prefix /${slug};
proxy_cache_bypass $http_upgrade;
}

location /${slug}/api/images/ {
proxy_pass http://host.docker.internal:${port};
proxy_http_version 1.1;
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;
}
`;

if (!fs.existsSync(NGINX_CONF_DIR)) {
Expand All @@ -50,10 +37,9 @@ function removeShopConfig(slug) {
if (!fs.existsSync(SHOPS_LOCATIONS_FILE)) return;

const content = fs.readFileSync(SHOPS_LOCATIONS_FILE, 'utf8');
// Each shop has two location blocks: main + /api/images/, both under a "# Shop:" comment
const escaped = slug.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const pattern = new RegExp(
`\\n# Shop: ${escaped}\\nlocation /${escaped}[\\s\\S]*?\\n}\\n\\nlocation /${escaped}/api/images/[\\s\\S]*?\\n}\\n`,
`\\n# Shop: ${escaped}\\nlocation /${escaped}/[\\s\\S]*?\\n}\\n`,
'g'
);
const updated = content.replace(pattern, '');
Expand All @@ -62,7 +48,7 @@ function removeShopConfig(slug) {

function reloadNginx() {
try {
execSync('docker exec nginx-proxy nginx -s reload', { stdio: 'pipe' });
execSync('nginx -t && systemctl reload nginx', { stdio: 'pipe' });
return true;
} catch (err) {
console.error('Failed to reload nginx:', err.message);
Expand Down
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shuttle-platform-frontend",
"private": true,
"version": "2.5.0",
"version": "2.5.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down