{
  "slug": "Bridging-Networks--Using-Nginx-as-a-Reverse-Proxy-for-Port-Redirection-b8e1f515b549",
  "title": "Bridging Networks: Using Nginx as a Reverse Proxy for Port Redirection",
  "subtitle": "When managing distributed systems or local development environments, you often encounter a common networking hurdle: needing to access a…",
  "excerpt": "When managing distributed systems or local development environments, you often encounter a common networking hurdle: needing to access a…",
  "date": "2026-02-09",
  "tags": [],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/bridging-networks-using-nginx-as-a-reverse-proxy-for-port-redirection-b8e1f515b549",
  "hero": "https://cdn-images-1.medium.com/max/800/1*E7PXfOBeCrunRsLAyMnxBQ.png",
  "content": [
    {
      "type": "paragraph",
      "html": "When managing distributed systems or local development environments, you often encounter a common networking hurdle: needing to access a service on one IP and port, but having that service actually live on a different server or a different port."
    },
    {
      "type": "paragraph",
      "html": "While tools like <code>iptables</code> operate at the kernel level to move packets. They can be complex to manage and struggle with high-level application data like specific URL paths. This is where <strong>Nginx</strong>, acting as a <strong>Reverse Proxy</strong>, becomes the superior solution."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "The Challenge"
    },
    {
      "type": "paragraph",
      "html": "Imagine you have a local environment where you want to access a Swagger API documentation page. Your local machine is listening on port <strong>9099</strong>, but the actual API service is running on a remote server at <strong>ip_gateway</strong> on port <strong>4000</strong>."
    },
    {
      "type": "paragraph",
      "html": "Additionally, network services like <strong>Apache</strong> might already be occupying standard ports (like port 80), preventing traditional web server setups from working out of the box."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*E7PXfOBeCrunRsLAyMnxBQ.png",
      "alt": "Bridging Networks: Using Nginx as a Reverse Proxy for Port Redirection",
      "caption": "",
      "width": 1024,
      "height": 1024
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Why Nginx over iptables?"
    },
    {
      "type": "paragraph",
      "html": "While <code>iptables</code> can perform Destination Network Address Translation (DNAT), but it has significant limitations compared to Nginx:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "<strong>URL Awareness:</strong> <code>iptables</code> only knows about IPs and Ports. Nginx can read the actual URL path (e.g., <code>/swagger/index.html</code>) and make routing decisions based on it.",
        "<strong>Ease of Debugging:</strong> Nginx provides clear error logs and status codes (like 404 or 502), whereas <code>iptables</code> failures often result in \"silent\" connection timeouts.",
        "<strong>Protocol Intelligence:</strong> Nginx handles the “handshake” of HTTP traffic, ensuring that headers like the original user’s IP address are preserved and passed to the destination server."
      ]
    },
    {
      "type": "heading",
      "level": 2,
      "text": "The Solution: A Dedicated Reverse Proxy Block"
    },
    {
      "type": "paragraph",
      "html": "To solve this, we configure Nginx to listen on the desired local port and “pass” those requests to the remote destination."
    },
    {
      "type": "heading",
      "level": 3,
      "text": "1. Cleaning the Environment"
    },
    {
      "type": "paragraph",
      "html": "Before setting up Nginx, you must ensure that existing kernel-level rules (like <code>iptables</code>) are cleared so they don't intercept the traffic before Nginx can process it:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo iptables -t nat -F"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "2. The Nginx Configuration"
    },
    {
      "type": "paragraph",
      "html": "The core of the solution lies in the <code>/etc/nginx/sites-available/default</code> file. By defining a <code>server</code> block that listens on <strong>9099</strong>, we create a gateway:"
    },
    {
      "type": "code",
      "lang": "nginx",
      "code": "server {    listen 9099;    listen [::]:9099;    server_name _;    location / {        # The\ndestination server and port        proxy_pass http://ip_target:4000;                # Pass essential\nheaders to the target        proxy_set_header Host $host;        proxy_set_header X-Real-IP\n$remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\nproxy_set_header X-Forwarded-Proto $scheme;    }}"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "3. Resolving Port Conflicts"
    },
    {
      "type": "paragraph",
      "html": "If you encounter an An <code>Address already in use</code> error when starting Nginx, it is likely because another service (like Apache) is already using port 80. By explicitly setting Nginx to listen on <strong>9099</strong> and commenting out any default port 80 lines, you allow both services to coexist peacefully on the same machine."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Summary of Workflow"
    },
    {
      "type": "list",
      "ordered": true,
      "items": [
        "<strong>Identify the target:</strong> Ensure the destination IP (<code>10.220.209.38:4000</code>) is reachable from the proxy host.",
        "<strong>Configure Nginx:</strong> Set the <code>listen</code> port to your entry point and <code>proxy_pass</code> to your destination.",
        "<strong>Validate:</strong> Use <code>sudo nginx -t</code> to check for syntax errors.",
        "<strong>Execute:</strong> Restart the service with <code>sudo systemctl restart nginx</code>."
      ]
    },
    {
      "type": "paragraph",
      "html": "By using this approach, you transform your local machine into a powerful traffic coordinator, ensuring that complex URL paths and port mappings are handled seamlessly at the application layer."
    }
  ]
}
