I have the need to run 2 servers, one in Node.js and one with Meteor, let's say on my_server.com
The Node server listens on my_server.com:8080,
The Meteor server listens on my_server.com:3000
I'd like to open just the port :80, and then redirect the user with vhost of Node according to the subdomain, so
node.my_server.com:80 should go to my_server.com:8080
meteor.my_server.com:80 should go to my_server.com:3000
and I want to open just one port. Is this possible?
Thank you
Yes, it's totally possible, you should use nginx of apache for that.
Here is example nginx config:
server {
listen *:80;
server_name node.my_server.com;
access_log /var/log/nginx/node.access.log;
error_log /var/log/nginx/node.error.log;
location / {
proxy_pass http://127.0.0.1: 8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $remote_addr;
}
}
And similar one for meteor
server {
listen *:80;
server_name meteor.my_server.com;
access_log /var/log/nginx/meteor.access.log;
error_log /var/log/nginx/meteor.error.log;
location / {
proxy_pass http://127.0.0.1: 3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $remote_addr;
}
}
Related
I am having a problem.
I have a node application running on :3000 on a subdomain inside of Plesk.
I have the subdomain https://xxx.flamingocams.co.uk ;
when I navigate to the subdomain it displays the default plesk page and this is the problem;
I have tried to change the port of the node application to 80 and 443 however this conflicts with plesk.
I have no issues when accessing the node application on https://xxx.flamingocams.co.uk:3000.
Now the only other thing I've seen other people attempt is a reverse proxy;
I found this example;
server {
listen 0.0.0.0:80;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
I am running Plesk Obsidian v18.0.34_build1800210325.10 os_Ubuntu 16.04 so my question is, Where would I place this config to get the subdomain to point only to the nodejs application?
And is this config correct for what I'm trying to achieve?
I have little to no knowledge on nginx configuration my apologies
I have checked out this post and the answer says I need to add a config /etc/nginx/sites-available/yourdomain.com however I do not have the directory sites-available
response to comments // xxx.flamingocams.co.uk.conf
server {
listen 0.0.0.0:80;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 0.0.0.0:443;
server_name xxx.flamingocams.co.uk;
ssl_certificate /opt/psa/var/certificates/scfZc0CwJ;
ssl_certificate_key /opt/psa/var/certificates/scfZc0CwJ;
server_name xxx.flamingocams.co.uk;
access_log "/var/log/nginx/xxxflam.log";
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
}
}
In your config, the server is running only on port 80
listen 0.0.0.0:80;
With this config, the page : http://xxx.flamingocams.co.uk will display your application (the request come to the NginX proxy then is forwarded to NodeJS application on port 3000). Because there aren't any server block listen on port 443, the default Plesk screen is displayed.
In order to have your app running on https, you need to listen on port 443 on NginX, you also need to configure the SSL certificate
The config would be :
server {
listen 0.0.0.0:443;
server_name xxx.flamingocams.co.uk;
ssl_certificate path_to_your_ssl_certificate;
ssl_certificate_key path_to_your_ssl_key;
# The rest of your config is ok :)
}
I'm trying to run node-media-server on a EC2 instance, but i a'm not able to make OBS to connect to the server, here is my Nginx config:
server {
listen 8000;
listen 1935;
server_name example.com;
location / {
proxy_pass http://localhost:$server_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
And here is my security group set up:
Any idea what is going on?
Thanks in advance,
I found the problem, the first thing is to setup Nginx to listen on the port 80 only as node-media-server takes care of listening on the ports 8000 and 1935
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:$server_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Second thing is to make sure the ports 8000 and 1935 are open in the server as by default they are not.
Hope it helps to anyone with the same problem.
I am installing a site under my machine. I have a NodeJS server that listens on port 4000.
I am using an NGINX reverse proxy, so that it is accessible from port 80. Here is the following configuration in / sites-availabes
upstream site {
server 127.0.0.1:4000;
}
server {
listen 80;
server_name site.infra.monsite.blog;
location / {
proxy_pass http://site;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
But I can't access my site from site.infra.monsite.blog; which points to port 80 of the machine.
Are there any ports to open like on Windows, or an NGINX configuration that I missed?
Yet when I do "curl -X GET http://localhost/": It works on the machine.
Thanks for your help.
Try this:
upstream site {
server 127.0.0.1:4000;
keepalive 64;
}
server {
listen 80;
server_name www.example.com;
return 301 https://www.example.com$request_uri;
}
location / {
proxy_pass http://site;
proxy_http_version 1.1;
proxy_redirect off;
proxy_pass_header Set-Cookie;
proxy_set_header Connection 'upgrade';
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass_header X-XSRF-TOKEN;
proxy_read_timeout 240s;
}
I have NGINX running as reverse proxy which forwards all http and https traffic to my node.js application, which listens to localhost:port
However the issue I have is that the node.js application sees all incoming requests as coming from ::ffff:127.0.0.1
How can I change the NGINX config such that the real IP will be passed through and forwarded to the node.js application?
server {
listen 80;
listen [::]:80;
listen 443;
listen [::]:443;
root /var/www/example.com/html;
index index.html index.htm;
server_name example.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:myport;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# Requests for socket.io are passed on to Node on port x
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:myport;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Edit: The express.js/node.js application processes req.ip and has app.enable('trust proxy'); at startup
Express.js official site has this guide. Instructions:
app.set('trust proxy', true) in js.
proxy_set_header X-Forwarded-For $remote_addr in nginx.conf
You can now read-off the client IP address from req.ip property
This solves it taking into consideration above NGINX config.
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
I'am putting my node js API (that I manage with pm2) behind a reverse proxy with nginx, here no problem.
But since, if I send any request it become a GET on '/'.
How can I tell to nginx to forward the full url, the request types (PUT, OPTIONS, DELETE, ...), the requests params and the request body ?
here is my simple nginx config.
server {
listen 80;
listen [::]:80;
server_name api-prod.mysite.com www.api-prod.mysite.com localhost;
location / {
proxy_pass http://127.0.0.1:3111;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Your nginx configuration seems good except, you should use proxy_http_version and proxy_cache_bypass because sometimes(depends on version) Nginx and nodejs using different HTTP version.
Following configuration working fine for me
server {
listen 80;
server_name arifjaunpur.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}