N8N Behind HTTPS_PROXY

When running N8N Behind Http proxy, it does not work and gives bad gateway error, here the issue is that the requests are going properly and proxy is responding properly to the messages but N8N shows Bad Gateway error.

What is the error message (if any)?

502 - "\n\n<meta type="copyright" content="Copyright (C) 1996-2019 The Squid Software Foundation and contributors">\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\nERROR: The requested URL could not be retrieved\n<style type="text/css"><!-- \n /*\n * Copyright (C) 1996-2020 The Squid Software Foundation and contributors\n

I am using simple workflow to send messages via slack to channel.

Share the output returned by the last node

Bad gateway - the service failed to handle your request
ERROR: The requested URL could not be retrieved ERROR The requested URL could not be retrieved
The following error was encountered while trying to retrieve the URL:
Read Error

The system returned: [No Error]

An error condition occurred while reading data from the network. Please retry your request.

Your cache administrator is webmaster.

@Vikas_Bukhari, did you set the N8N_PROXY_HOPS environment variable as stated in here?

Yes, I tried modfying the parameter, but It is still not working. Same errror is coming. @Knight

This is the docker-compose.yml file

@Knight

version: "3.3"  # <-- Add this line if needed

services:
  n8n:
    image: IMAGE
    container_name: n8n
    restart: unless-stopped
    environment:
      - N8N_HOST=HOST_NAME
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - N8N_EDITOR_BASE_URL=BASE_URL/
      - WEBHOOK_URL=BASE_URL/
      - WEBHOOK_TUNNEL_URL=BASE_URL/
      - HTTP_PROXY=http://tinyproxy.XXXX.is1:8888  # Use HTTP proxy
      - https_proxy=http://tinyproxy.XXXX.is1:8888
      - http_proxy=http://tinyproxy.XXXX.is1:8888
      - HTTPS_PROXY=http://tinyproxy.XXXX.is1:8888  # Use HTTP proxy
      - NO_PROXY=localhost,127.0.0.1,yourdomain.com
      - NODE_TLS_REJECT_UNAUTHORIZED=0  # Disable SSL verification
      - CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
      - DEBUG=*
      - LOG_LEVEL=debug
      - N8N_USE_DEPRECATED_REQUEST_LIB=true
      - NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt
      - N8N_PROXY_HOPS=4
      - N8N_DIAGNOSTICS_ENABLED=false
      - N8N_VERSION_NOTIFICATIONS_ENABLED=false
      - N8N_TEMPLATES_ENABLED=false
    extra_hosts:
      - "host.docker.internal:host-gateway"
    network_mode: "host"  # Alternative networking approach
    ports:
      - "5678:5678"
    volumes:
      - ~/.n8n:/home/node/.n8n```

It does not only happen with Slack, It happens with all outbound http / https calls. @Knight

@Knight Can you help here?

@Vikas_Bukhari Try this:

version: '3.3'

services:
  n8n:
    image: docker.n8n.io/n8nio/n8n
    container_name: n8n
    ports:
      - "5678:5678"
    environment:
      - N8N_ENCRYPTION_KEY=db1cef92-ee4d-4b83-a807-999ea50e573b
      - N8N_PROXY_HOPS=1
      - WEBHOOK_URL=https://<NGROK_DOMAIN>
      - N8N_DIAGNOSTICS_ENABLED=false
      - N8N_VERSION_NOTIFICATIONS_ENABLED=false
      - N8N_TEMPLATES_ENABLED=false
    volumes:
      - ~/.n8n:/home/node/.n8n
    restart: unless-stopped
    stdin_open: true
    tty: true

Make sure to replace https://<NGROK_DOMAIN> with the url you received from the proxy. Check also if the proxy is forwarding it’s requests to localhost:5678.

@Vikas_Bukhari,
Has your issue been resolved? If so, please mark the solution or mention it so others can benefit from the fix.

Make sure your proxy is configured to pass the host name, for apache that is:

ProxyPreserveHost On

and the full proxy config is:

RewriteEngine On
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteRule ^/?(.*) "ws://localhost:5678/$1" [P,L]
RewriteCond %{HTTP:UPGRADE} !^WebSocket$ [NC]
RewriteRule ^/?(.*) "http://localhost:5678/$1" [P,L]
ProxyPassReverse / http://localhost:5678/
ProxyPreserveHost On