Manage Nginx Proxy Manager proxy hosts from the Proxy hosts page

With an optional second user on the NPM integration (Manage on Proxy Hosts,
no two-factor sign-in) an administrator can make, change, switch off and
delete proxy hosts: names, where it forwards to, an existing certificate and
what is switched on. Only what differs is sent, a host's access list,
locations and custom configuration are never touched, and combinations NPM
would quietly undo (Force HTTPS, HTTP/2 or HSTS without a certificate) are
refused with the reason. Delete asks for the host's first name, checked
against the host as NPM has it. The user that reads is still view-only and
never writes. Audited; the managing user can be taken away again.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-03 19:24:00 +01:00
1 parent 672c7f2f66
commit 4dd7901f1c
13 files changed
+1286 -14

No files matched your search

+110 -3
View File
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from "react";
import { Link } from "react-router-dom";
import ProxyHostForm from "../components/ProxyHostForm";
import { api, type CurrentUser, type IntegrationProblem, type NpmHost, type NpmOverview } from "../api/client";
import SortableTh from "../components/SortableTh";
import Pagination from "../components/Pagination";
@@ -12,11 +13,12 @@ import { useSortable } from "../hooks/useSortable";
import { useTableSearch } from "../hooks/useTableSearch";
import { usePagination } from "../hooks/usePagination";
import { downloadCsv } from "../utils/csv";
import { confirmDialog, promptDialog } from "../utils/dialogs";
import { formatDate } from "../utils/date";
import { readableError } from "../utils/errors";
import { toast } from "../utils/toast";
type Row = NpmHost & { integrationName: string; domain: string; target: string; certDays: number | null; certName: string; statusRank: number; serverName: string };
type Row = NpmHost & { integrationId: number; canManage: boolean; integrationName: string; domain: string; target: string; certDays: number | null; certName: string; statusRank: number; serverName: string };
const DAY_MS = 86_400_000;
const daysLeft = (iso: string | null): number | null => (iso && Number.isFinite(Date.parse(iso)) ? Math.floor((Date.parse(iso) - Date.now()) / DAY_MS) : null);
@@ -84,6 +86,8 @@ export default function ProxyHosts({ user }: { user: CurrentUser }) {
(data?.integrations ?? []).flatMap((i) =>
i.hosts.map((h) => ({
...h,
integrationId: i.id,
canManage: i.canManage,
integrationName: i.name,
domain: h.domains.join(", "),
target: `${h.forwardScheme}://${h.forwardHost}${h.forwardPort !== null ? `:${h.forwardPort}` : ""}`,
@@ -95,6 +99,51 @@ export default function ProxyHosts({ user }: { user: CurrentUser }) {
),
[data],
);
const isAdmin = user.role === "admin";
const managing = (data?.integrations ?? []).filter((i) => i.canManage && !i.error);
const canManageAny = isAdmin && (data?.integrations ?? []).some((i) => i.canManage);
// The form that is open: a new host (on one of the NPMs that can be managed) or a change of one.
const [form, setForm] = useState<{ mode: "new"; integrationId: number } | { mode: "edit"; row: Row } | null>(null);
async function afterChange(message: string) {
setForm(null);
toast.success(message);
await load(true);
}
async function switchHost(h: Row) {
if (h.enabled && !(await confirmDialog({ title: "Switch off proxy host", message: `Switch off ${h.domains[0]}? Nginx Proxy Manager stops serving ${h.domains.length > 1 ? "its names" : "it"}. Nothing is lost, and switching it on again brings it back.`, confirmLabel: "Switch off", danger: true }))) return;
try {
await api.npm.setHostEnabled(h.integrationId, h.id, !h.enabled);
toast.success(`${h.domains[0]} is ${h.enabled ? "off" : "on"}.`);
await load(true);
} catch (err) {
toast.error(readableError(err));
}
}
async function deleteHost(h: Row) {
const typed = await promptDialog({
title: "Delete proxy host",
message: `This deletes the proxy host ${h.domains.join(", ")} (it forwards to ${h.target}) in ${h.integrationName}: Nginx Proxy Manager stops serving ${h.domains.length > 1 ? "those names" : "that name"}. Type its first name to confirm.`,
label: "First name",
confirmLabel: "Delete",
required: true,
});
if (typed === null) return;
if (typed.trim().toLowerCase() !== h.domains[0]) {
toast.error("That is not the first name of the host, so nothing was deleted.");
return;
}
try {
await api.npm.deleteHost(h.integrationId, h.id, h.domains[0]);
toast.success(`Deleted ${h.domains[0]}.`);
await load(true);
} catch (err) {
toast.error(readableError(err));
}
}
const tableSearch = useTableSearch(rows);
const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, "statusRank", "asc", { url: true });
const { pageItems, page, setPage, pageCount, totalCount } = usePagination(sorted);
@@ -117,6 +166,11 @@ export default function ProxyHosts({ user }: { user: CurrentUser }) {
<h2 className="page-title mb-0">Proxy hosts</h2>
{(user.role === "admin" || user.role === "operator") && data && data.integrations.length > 0 && (
<div className="ms-auto btn-list">
{isAdmin && managing.length > 0 && (
<button className="btn btn-primary" onClick={() => setForm({ mode: "new", integrationId: managing[0].id })}>
New proxy host
</button>
)}
<button className="btn btn-outline-secondary" onClick={() => void readNow()} disabled={reading}>
{reading ? "Reading…" : "Read now"}
</button>
@@ -124,9 +178,44 @@ export default function ProxyHosts({ user }: { user: CurrentUser }) {
)}
</div>
<div className="text-secondary mb-3">
What Nginx Proxy Manager serves: the name, where it forwards to, the certificate and whether NPM could set it up. It only reads — nothing here changes anything in NPM. A certificate in its last
What Nginx Proxy Manager serves: the name, where it forwards to, the certificate and whether NPM could set it up. {canManageAny ? "An administrator can also make, change, switch off and delete hosts here. " : "It only reads — nothing here changes anything in NPM. "}A certificate in its last
two weeks, or a host NPM can&rsquo;t set up, is an alert.
</div>
{isAdmin && data && data.integrations.length > 0 && !canManageAny && (
<div className="text-secondary small mb-3">
To make and change proxy hosts from here, give the Nginx Proxy Manager integration a user with <em>Manage</em> on Proxy Hosts: <Link to="/integrations">Integrations</Link> → edit → <em>Email of a user that may manage</em>.
</div>
)}
{form && form.mode === "new" && (
<>
{managing.length > 1 && (
<div className="mb-2">
<label className="form-label" htmlFor="ph-integration">
In
</label>
<select id="ph-integration" className="form-select w-auto" value={form.integrationId} onChange={(e) => setForm({ mode: "new", integrationId: Number(e.target.value) })}>
{managing.map((i) => (
<option key={i.id} value={i.id}>
{i.name}
</option>
))}
</select>
</div>
)}
<ProxyHostForm key={form.integrationId} integrationId={form.integrationId} integrationName={managing.find((i) => i.id === form.integrationId)?.name ?? ""} certificates={managing.find((i) => i.id === form.integrationId)?.certificates ?? []} onDone={(name) => void afterChange(`Made the proxy host ${name}.`)} onCancel={() => setForm(null)} />
</>
)}
{form && form.mode === "edit" && (
<ProxyHostForm
key={`${form.row.integrationId}-${form.row.id}`}
integrationId={form.row.integrationId}
integrationName={form.row.integrationName}
certificates={(data?.integrations ?? []).find((i) => i.id === form.row.integrationId)?.certificates ?? []}
host={form.row}
onDone={(name) => void afterChange(`Saved ${name}.`)}
onCancel={() => setForm(null)}
/>
)}
<ErrorNotice error={error} />
{data === null && !error && <BlockSkeleton lines={4} />}
@@ -197,10 +286,11 @@ export default function ProxyHosts({ user }: { user: CurrentUser }) {
<SortableTh<Row> label="Name" sortKeyName="domain" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<Row> label="Forwards to" sortKeyName="target" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
<SortableTh<Row> label="Certificate" sortKeyName="certDays" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
{canManageAny && <th />}
</tr>
</thead>
<tbody>
{(pageItems ?? []).length === 0 && <EmptyRow colSpan={4} title="No proxy hosts" filtered={tableSearch.active ? () => tableSearch.setQuery("") : undefined} />}
{(pageItems ?? []).length === 0 && <EmptyRow colSpan={canManageAny ? 5 : 4} title="No proxy hosts" filtered={tableSearch.active ? () => tableSearch.setQuery("") : undefined} />}
{(pageItems ?? []).map((h) => (
<tr key={`${h.integrationName}-${h.id}`}>
<td>
@@ -228,6 +318,23 @@ export default function ProxyHosts({ user }: { user: CurrentUser }) {
<td className="stack-label" data-label="Certificate">
<CertBadge h={h} />
</td>
{canManageAny && (
<td className="text-end text-nowrap">
{h.canManage && (
<>
<button className="btn btn-sm btn-outline-secondary me-1" onClick={() => setForm({ mode: "edit", row: h })}>
Edit
</button>
<button className="btn btn-sm btn-outline-secondary me-1" onClick={() => void switchHost(h)}>
{h.enabled ? "Switch off" : "Switch on"}
</button>
<button className="btn btn-sm btn-outline-danger" onClick={() => void deleteHost(h)}>
Delete
</button>
</>
)}
</td>
)}
</tr>
))}
</tbody>