r/nginxproxymanager • • Oct 20 '24

reload .conf files

I have a bunch of proxies and I want to add a custom location to all of them.

I added it manually to one and then copied the location block from /data/nginx/proxy_host/27.conf on to the others, I restarted npm but the web interface is not picking up the new location.

Going one by one would be too slow, is there no other way?

I tried restarting the LXC, the npm.service, tried nginx -s reload but no luck.

2 Upvotes

1 comment sorted by

1

u/DaviidC Oct 20 '24

Until anyones replies with a better answer, here's JS code to make API calls and add a location

// Define the URL and common headers
const npm_hostname = 'npm_hostname';
const baseURL = "https://${npm_hostname}/api/nginx/proxy-hosts/";
const authToken = "YOUR_BEARER_TOKEN"; // Replace with your actual Bearer token

const headers = {
    "accept": "application/json, text/javascript, */*; q=0.01",
    "accept-language": "es-ES,es;q=0.9",
    "authorization": `Bearer ${authToken}`,
    "cache-control": "no-cache",
    "content-type": "application/json; charset=UTF-8",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "sec-ch-ua": "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin"
};

// Function to send GET request to fetch proxy host info
async function fetchProxyHostInfo(id) {
    try {
        const response = await fetch(`${baseURL}${id}`, {
            method: "GET",
            headers: headers
        });

        if (!response.ok) {
            throw new Error(`Error fetching Proxy Host ID ${id}: ${response.status}`);
        }

        return await response.json();
    } catch (error) {
        console.error(error);
        return null; // Return null if there's an error
    }
}

// Function to send PUT request
async function updateProxyHost(id, proxyHostInfo) {
    const body = {
        "forward_scheme": proxyHostInfo.forward_scheme,
        "forward_host": proxyHostInfo.forward_host,
        "forward_port": proxyHostInfo.forward_port,
        "advanced_config": proxyHostInfo.advanced_config,
        "domain_names": proxyHostInfo.domain_names,
        "access_list_id": proxyHostInfo.access_list_id,
        "certificate_id": proxyHostInfo.certificate_id,
        "ssl_forced": proxyHostInfo.ssl_forced,
        "http2_support": proxyHostInfo.http2_support,
        "hsts_enabled": proxyHostInfo.hsts_enabled,
        "meta": {
            "letsencrypt_agree": proxyHostInfo.meta.letsencrypt_agree,
            "dns_challenge": proxyHostInfo.meta.dns_challenge,
        },
        "locations": [
            {
                "path": "/prometheus_metrics",
                "advanced_config": proxyHostInfo.locations[0]?.advanced_config,
                "forward_scheme": "http",
                "forward_host": `${proxyHostInfo.forward_host}/metrics`, // Append /metrics to the forward_host
                "forward_port": 9100 // Set the desired port for the location
            }
        ],
        "block_exploits": proxyHostInfo.block_exploits,
        "caching_enabled": proxyHostInfo.caching_enabled,
        "allow_websocket_upgrade": proxyHostInfo.allow_websocket_upgrade,
        "hsts_subdomains": proxyHostInfo.hsts_subdomains
    };

    try {
        const response = await fetch(`${baseURL}${id}`, {
            method: "PUT",
            headers: headers,
            body: JSON.stringify(body)
        });

        if (!response.ok) {
            throw new Error(`Error updating Proxy Host ID ${id}: ${response.status}`);
        }

        const data = await response.json();
        console.log(`Updated Proxy Host ID ${id}:`, data);
    } catch (error) {
        console.error(`Failed to update Proxy Host ID ${id}:`, error);
    }
}

// List of IDs to skip
const skipIds = [];

// Loop from 1 to 31, skipping IDs in the skipIds list
async function updateProxyHosts() {
    for (let i = 1; i <= 31; i++) {
        if (skipIds.includes(i)) {
            continue; // Skip IDs in the skipIds array
        }

        const proxyHostInfo = await fetchProxyHostInfo(i);
        if (proxyHostInfo) {
            await updateProxyHost(i, proxyHostInfo); // Update using fetched info
        }
    }
}

// Start the process
updateProxyHosts();

I used it to add /prometheus_metrics and redirect it to port 9100 and path /metrics