Search code examples
nginxreverse-proxycloudflare

How to proxy pass from url path to different subdomain on different dns server?


Let's say I have my main domain on one server and one of the subdomains to another server.

both of these addresses are using Cloudflare DNS to different ip addresses, so:

example.com => ip1

new.example.com => ip2

Now I want to proxy_pass a certain path on example.com to new.example.com without changing the url, so:

example.com/something should show content of new.example.com/somethingElse

These are my nginx config files, the problem is if I point example.com/something to google.com or even an ngrok server that I hosted for test, everything works just fine, but when I point it to new.example.com/something it gives me 502 error, so my guess is there's something wrong with my new.example.com config.

example.com Config:

server {

        listen 443 ssl http2;
        listen [::]:443 ssl http2;

        ssl     on;
        ssl_certificate         /etc/letsencrypt/live/cert.pem;
        ssl_certificate_key     /etc/letsencrypt/live/key.pem;

        server_name example.com www.example.com;
        resolver 8.8.8.8;

        location = /something {
                proxy_set_header X-Forwarded-Host  new.example.com;
                proxy_set_header Host              new.example.com;
                proxy_pass https://new.example.com/somethingElse;
        }
}

new.example.com Config:

server {
  listen 443;

  server_name www.new.example.com new.example.com;

  ssl_certificate         /etc/ssl/private/cert.pem;
  ssl_certificate_key     /etc/ssl/private/key.pem;

  location / {
      proxy_pass         http://container-name:80;
      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}


Solution

  • Please test the connectivity between the servers. Login into example.com server and send CURL request to the new.example.com service. Looks like example.com server is not able to reach new.example.com server. Please check nginx service logs.

    Another option to achieve your requirements is cloudflare worker service.