How to forward request to Node.js from nginx with tcp_proxy_module? - node.js

Now, I had patched nginx with the nginx_tcp_proxy_module, and it is running OK on port 8080.
How do I connect the clients to port 80 of nignx, not port 8080 of Node.js,
so that having the nginx forward the request to Node.js?

Just change 8080 to 80. But TCP and HTTP on the same port is not possible.
Aleternative solution:
Use HAProxy on port 80
Set up nginx to listen on port 81
Run your node.js app on port 8080
Configure HAProxy to
forward Host: your.nodejs.socketio.com to 127.0.0.1:8080
forward everything else to 127.0.0.1:81
If you go down this route you will probably want to preserve client IPs:
Configure HAproxy
Use RealIP module in nginx
Use X-Forwarded-For in socket.io
socketio.handshakeData = function(data) {
var d = socketio.Manager.prototype.handshakeData(data);
d.ip = data.request.headers['x-forwarded-for'] || data.request.connection.remoteAddress;
return d;
};

Related

HAProxy configuration ports issue

Hi :) I have problem with HAProxy configuration. I have haproxy and two backend servers (backend servers listen on 1234 port)
It's my haproxu config:
frontend http_front
bind *:80
backend http_back
balance roundrobin
server server1 10.0.0.2:1234
server server2 10.0.0.3:1234
This config doesn't work, but when i add to frontend:
bind *:1234
It works great - i don't understand it because bind *:1234 inform only haproxy to listen on 1234 port nothing more. Have you any advices or explanations ?
Port 80 is a privileged port, this means that you can only start haproxy as root when you want that haproxy should listen on port 80.
Another reason could be that there is another server listen on port 80. Maybe a web server run also on this machine which listens on port 80.

Specify Caddy listening port

"By default, Caddy will bind to ports 80 and 443 to serve HTTPS and redirect HTTP to HTTPS." (https://caddyserver.com/docs/automatic-https)
How can we change this port?
Background:
In our setup, Caddy runs behind an AWS load balancer which forwards requests from port 443 to port 4443. Therefore, we would like to have Caddy listen on 4443. (We use the DNS challenge.)
According to the documentation:
The first line of the Caddyfile is always the address of the site to serve.
In your Caddyfile:
<domain>:<port>
Example:
localhost:8080
You should be able to do this
https://example.com:4443 {
# config info
}
Above answers are both good, but if you want to run on specific port and have other reverse proxy redirecting from yourdomain.com:443 to <MY_SERVER_IP>:4443, you can use global settings
{
http_port 880
https_port 4443
}
mydomain.com {
...
}
Only use this when you want your server to run on 4443 but be able to accept requests where Host: mydomain.com is present (host doesn't have :4443 port)

What else is required to access a node app from outside a Digital Ocean droplet?

We have set up a node server which runs on port 5000.
In a newly created droplet, we have installed and started nginx. To access the node app, we have changed the default port from 80 to 5000 in /etc/nginx/sites-enabled/default
server {
listen 5000 default_server;
listen [::]:5000 default_server;
ufw is enabled
sudo ufw enable
Also the port is enabled
sudo ufw allow 5000/tcp
Also, tried this way too:
sudo ufw allow 5000
As confirmed with sudo ufw status
netstat -ntlp
Also the app is configured to listen on the public interface
const server = app.listen(process.env.PORT || 5000, '0.0.0.0', () => {
console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});
However, not even the default port was responding. Hence, we reverted to 80 as the default port.
What else is required to access node app outside of the droplet?
When it comes to NodeJS and NGINX, we'll want to configure NGINX to listen on port 80, though we'll want to use proxy_pass to pass the request from the web server (NGINX) to the NodeJS application on the port that the application is running on. This will allow us to keep the port out of the URL.
With the current configuration, NGINX would be listening on port 5000 which would prevent the application from being able to listen in on the same port (or vice versa).
There is an excellent guide that covers setting up NodeJS + NGINX -- this specific part is the most important:
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04#set-up-nginx-as-a-reverse-proxy-server
The above covers how we'd go about setting up the Server Block :-)

ExpressJS Server - respond to host header with shared port

Lets say I have corporatewebsite.com listening on port 80. Which is an appache / WordPress site.
I have a node application that I'd like to respond to sub.corporatewebsite.com
The node application is running Express currently. I can get it to listen to a separate port at the moment, but my service won't start if it's pointed to port 80 since it's already in use.
How can I have both apache and node listening to port 80, but having node responding to the subdomain?
You could use a reverse proxy like Nginx to route your subdomains.
Here is an example of nginx configuration, you might probaly have to complete according to your project and server :
server {
listen 80;
server_name corporatewebsite.com;
location / {
[ ... some parameters ... ]
include proxy_params; // Import global configuration for your routes
proxy_pass http://localhost:1234/; // One of your server that listens to 1234
}
}
server {
listen 80;
server_name sub.corporatewebsite.com;
location / {
[ ... some parameters ... ]
include proxy_params;
proxy_pass http://localhost:4567/; // The other server that listens to 4567
}
}
You have to configure, for example, apache2 listening to port 1234 while nodejs is listening to port 4567.
If you do like this, a good practice is to block direct access from the outside to your ports 1234 and 4567 (using iptables for example).
I think this post could be useful to you : Node.js + Nginx - What now?

Add subdomain for port that listen node.js

Hi guys i have a problem. I work with php and node, php works with nginx listening port 80 and node works with the port 3000. I want add a subdomain that listen port 3000 a like media.mydomain.com but i could not. I tried with my website register domain and nothing happened and then with nginx in virtual host but show in console error because this port it is using.
nginx-php=mydomain.com
node=mydomain.com:3000 (tried add subdomain "media.fotogena.co")
virtual host:
server {
server_name media.mydomain.com;
listen mydomain.com:3000;
#i don't know that do!
}#show error port is being used
and had seen something like upstream but this assign port 80 to port 3000 but don't is the that me need
If i understand what you want to do, you want a subdomain to use instead of the port 3000, for the subdomain to work you need it to listen on port 80, and proxy what ever comes to port 3000.
server {
server_name sub.example.com;
listen 80;
location / {
proxy_pass http://localhost:3000;
}
}

Resources