Workers (sending logs to local) are working on test but not when deployed

Hello all,

I am trying to send some data from the requests to a Graylog. Graylog is hosted on a local machine.

Following the documantation and this article https://blog.cloudflare.com/logs-from-the-edge/

I am using this code:

addEventListener(‘fetch’, event => {
event.respondWith(fetchAndLog(event).catch(
(err) => new Response(err.stack, { status: 500 })
))
})

/**

  • Many more examples available at:
  • https://developers.cloudflare.com/workers/examples
  • @param {Request} request
  • @returns {Promise}
    */
    async function fetchAndLog(event) {
    const response = await fetch(event.request, { cf: { scrapeShield: false } });
    event.waitUntil(log(event.request, response));
    return response;
    }
    async function log(request, response) {
    const myJSON = JSON.stringify({
    ‘version’: 1.1,
    ‘source’: request.headers.get(‘host’),
    ‘short_message’: ‘CF Worker’,
    ‘url’: request.url,
    ‘referrer’: request.referrer,
    ‘userAgent’: request.headers.get(‘user-agent’) || ‘’,
    ‘responseStatus’: response.status,
    level: 3
    });
    console.log(myJSON);
    // Send async analytics request.

const logRequest = new Request(“http://site.net:4001/gelf”, {
method: ‘POST’,
headers: new Headers({
‘Content-Type’: ‘application/json’,
‘content-length’: new TextEncoder().encode(myJSON).length
}),
body: myJSON
});
const resp = await fetch(logRequest);
}

This code works on the “Edit” enviroment of the worker, it sends the data and I can read them on GrayLog.

But when I deploy the code, even though that the Worker is triggered when I visit the website no data are received on GrayLog.

Can anyone give a hand?

Thank you!

Could it be the non-standard port 4001? This might apply to the outbound connections:

https://support.cloudflare.com/hc/en-us/articles/200169156-Identifying-network-ports-compatible-with-Cloudflare-s-proxy

Thank you for the answer svanlund

I tried all the possible ports from this article , same results unfortunately.

You cannot use custom ports like that, they’re basically stripped from the request (so this will go to port 80 due to HTTP)

You should setup a reverse proxy for this port instead, nginx can easily handle this.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.