Jump to content

node.js proxy from NGINX.conf template


proxycon
 Share

Recommended Posts

proxycon

Hi Emby Community,

I'm trying to set up a reverse proxy (jada jada jada ipv6 reachable from ipv4 jada jada) and for reasons I can only do it with node.js.

I took inspiration from this nginx config guide as there where no guides for node.js but I'm stuck at setting the headers correctly.

First I want a working setup in pure node.js and the think about using express for compression middleware and stuff like that.

const http = require('http');
const PORT = 80;
const hostname = 'emby.mydomain.com';

function onRequest(req, res) {
  console.log('serve: ' + req.url);

  const options = {
    hostname: hostname,
    port: 80,
    path: req.url,
    method: req.method,
    headers: {
      ...req.headers,
      'X-Real-IP': '$remote_addr',
      'X-Forwarded-for': '$proxy_add_x_forwarded_for',
      'Host': '$host',
      'X-Forwarded-Proto': '$remote_addr',
      'X-Forwarded-Protocol': '$scheme',
      //proxy_redirect off;

      // websockets
      //proxy_http_version 1.1;
      'Upgrade': '$http_upgrade',
      'Connection': "upgrade"
    }
  };

  const proxy = http.request(options, function (r) {
    res.writeHead(r.statusCode, r.headers);
    r.pipe(res, {
      end: true
    });
  });

  req.pipe(proxy, {
    end: true
  });
}

http.createServer(onRequest).listen(PORT);
console.log('Listening on port '+PORT);

As I am not very comfortable in nginx and honestly ony a beginner in node.js I'm missing some header information, how to set http to version 1.1 (red something that thats maybe already default?) and how to translate the proxy_redirect directive.

 

Thank you for any responses, maybe someone with some nodejs experience can help me out here.

Link to comment
Share on other sites

pir8radio

I guess......   Uhm what?  lol      I don't understand the goal..    Your ISP is ipv6 only and you want ipv4 people to be able to access it?  is that the goal?

Link to comment
Share on other sites

proxycon

correct @pir8adio. And because my hosting provider has both I wanted to proxy the traffic. Maybe I need to setup a 4to6 tunnel but I thought plain proxy would be sufficent.

If you think this is nonsense and have a better suggestion I'm glad for any advice!

 

Link to comment
Share on other sites

pwhodges

Just to get it clear, you're saying that you have only IPv6 (that's most unusual!) where your Emby server is, so you're setting up a proxy in remote hosting that can be accessed by IPv4 - is that right?

Paul

Link to comment
Share on other sites

proxycon

@pwhodges my ISP provides Dual-Stack Lite (4in6) so I can access both, but everything I host is not reachable from IPv4 because I have no public IPv4 address.

Link to comment
Share on other sites

pir8radio
10 hours ago, proxycon said:

@pwhodges my ISP provides Dual-Stack Lite (4in6) so I can access both, but everything I host is not reachable from IPv4 because I have no public IPv4 address.

So you are setting up NGINX at some other location that has IPv4?      you can't create ipv4 compatibility without a network that allows Ipv4 inbound connections...   If I were you I would look at using Cloudflare in front of your emby server, let them handle the IPV4/IPV6  then the link to your emby server can be pure IPV6.

Link to comment
Share on other sites

  • 1 month later...
Familia Chapin

@pir8radio,

I am in the same situation as the original poster. i just setup a domain and a cloudfare account. emby works fine on my phone remote, which i know is IPv6. could you explain in a little more detail how i can use cloudfare to handle the IPv4/IPv6? I am admittedly a noob (i dont think anyone says that anymore) so i appreciate any detail you could give.

 

Link to comment
Share on other sites

pir8radio
8 hours ago, Familia Chapin said:

@pir8radio,

I am in the same situation as the original poster. i just setup a domain and a cloudfare account. emby works fine on my phone remote, which i know is IPv6. could you explain in a little more detail how i can use cloudfare to handle the IPv4/IPv6? I am admittedly a noob (i dont think anyone says that anymore) so i appreciate any detail you could give.

 

yours is a little different.   what kind of connection do you have where your server is?   IPv6 or IPv4 only?

 

Link to comment
Share on other sites

Familia Chapin

So I have the dual stack but the IPv4 part is behind a CGNAT. I am running Emby Server from my home and trying to use the IPv6 part to connect remote. I got it to work, when i use the app or my web browser on my phone it connects fine when i am off wifi (but i know my phone is IPv6). My sister is unable to connect on her home TV which they only have IPv4.

Sorry for the extra information, I dont want to come off as being short :)

 

Link to comment
Share on other sites

pir8radio
8 hours ago, Familia Chapin said:

So I have the dual stack but the IPv4 part is behind a CGNAT. I am running Emby Server from my home and trying to use the IPv6 part to connect remote. I got it to work, when i use the app or my web browser on my phone it connects fine when i am off wifi (but i know my phone is IPv6). My sister is unable to connect on her home TV which they only have IPv4.

Sorry for the extra information, I dont want to come off as being short :)

 

that's easy then if using cloudflare..   you just tell cloudflare your server IPv6 address and leave out an ipv4 address.    So remove any A records you have under the DNS tab and create only an AAAA record.   So if your domain name is   mydomain.com  you would set the name to  "mydomain.com" and set the AAAA address to your public ipv6 address.   don't create an A record.   cloudflare servers will always have an A and AAAA record, the dns settings page A and AAAA records on cloudflare is just to tell cloudflare how to get to your server.       

Edited by pir8radio
  • Like 1
Link to comment
Share on other sites

Familia Chapin

I can't believe it would be that easy! Now to wait and see once it all updates. I appreciate your help!!

 

Link to comment
Share on other sites

pir8radio
2 hours ago, Familia Chapin said:

I can't believe it would be that easy! Now to wait and see once it all updates. I appreciate your help!!

 

this is all assuming you have the ports open on your router..  some routers can be weird when it comes to opening ports for ipv6...   but i'm not a whole lot of help there.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...