How to get the domain name in nodejs - node.js

I have nginx in front which give proxy pass to node server.
How do I get the domain name which nginx pass to node.
I tried req.headers.host which gives IP address instead of domain name.
e.g. if current url is "http://xyz.abc.com/" then I want http://xyz.abc.com/ but I get ip like 127.0.0.1
How do I get domain name??

You can pass the correct host header from nginx to your Node.js app via the $host variable. You'll need something like this in your nginx.conf file under the server/location section:
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:3002$request_uri;
proxy_redirect off;
break;
}

Related

Redirecting SHOUTcast server to subdomain

Here's my situation:
I have a linux server from Scaleway hosting a SHOUTcast service, on the ip 1.2.3.4, port 8000
Recently I also rented a domain myserv.com so I can redirect the server to.
I changed the Nameserver to the ones provided by https://dns.he.net and started making records.
Got an A record to point my domain to the IP address, and it's working fine.
I can access my server by opening myserv.com:8000, but I'd like to access the SHOUTcast service with a subdomain, rather than adding the port. Let's say if I open sc.myserv.com it'll access 1.2.3.4:8000
I did some search and fount out I'd need to use SRV records, but I guess I didn't configure mine right cause it's not working. What am I doing wrong?
myserv.com. 86400 IN A 1.2.3.4
_shoutcast._tcp.sc.myserv.com. 86400 IN SRV 0 5 8000 myserv.com.
Thank you in advance
No idea about Apache, but if you are running Nginx then this is really straightforward.
It's just a case of setting a proxy directive in your server configuration to route all requests to your subdomain to the Shoutcast server on your machine. Something along these lines:
server {
server_name sc.myserv.com www.sc.myserv.com;
location / {
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-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8000;
proxy_read_timeout 90;
proxy_redirect off;
proxy_buffering off;
tcp_nodelay on;
}
}
If your Shoutcast stats show your server IP address instead of the listeners IP then inside the server block but outside of the location block try adding:
set_real_ip_from <YOUR_SERVER_IP>;
real_ip_header X-Real-IP;
real_ip_recursive on;
You can also match requests using regex, which in turn makes the captured matches available as variables. So your location directive becomes:
location ~ /(.*) {
This will now capture anything you add to your proxied url. Then to pass the captured path along your proxy_pass becomes:
proxy_pass http://127.0.0.1:8000/$1;
or for some awkward clients you might want to specify it's an mp3 in the url, like this:
proxy_pass http://127.0.0.1:8000/$1/stream.mp3;
You can't use SRV records for this. There isn't client support.
If you want to access your SHOUTcast/Icecast/HTTP/HTTP-like service on its default port of 80, you need to use port 80, not 8000.

NGINX shows the actual IP of the server when forwarding requests, How to prevent this?

my /etc/nginx/sites-available/default FILE
location / {
proxyredirect off;
proxypass http://localhost:7000;
proxyhttpversion 1.1;
proxysetheader Upgrade $httpupgrade;
proxysetheader Connection 'upgrade';
proxysetheader Host $host;
proxycachebypass $httpupgrade;
proxy_redirect off;
}
This is the common problem of proxy servers. When you pass the request to localhost:7000, it comes from the current machine, so you see this machine's address as a remote address.
If you want to get a real user's address, you should pass it with an HTTP-header, for example X-Forwarded-For:
proxy_set_header X-Forwarded-For $remote_addr;
Then, on your backend, instead of getting REMOTE_ADDR value you can get real user's IP from X-Forwarded-For HTTP-header.

Node+Express+NGINX application returning localhost instead of domain

I have a node+express website running on my ubuntu server on port 10000 with nginx on port 80 using a proxy_pass to localhost:10000. My issue is that when I ask for the host in express it returns localhost instead of my domain name. I use the nginx proxy so I can manage several domains on the machine pointing to different applications.
Is there a way to keep the original host name on my node+express server while still using proxy_pass in nginx?
By default, nginx sets the Host header in the upstream request to the hostname appearing in the proxy_pass statement. In this case localhost.
You need to set the Host header explicitly using the proxy_set_header directive.
For example, I always set this group:
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
See this document for more.

NGINX dns is not working on VPS

I'm a little new to VPS/Linux so please bear with me.
I have a domain name (attendahh.com) pointed to my host's nameservers.
I've set up /etc/nginx/conf.d/attendahh.com.conf as follows:
# the IP(s) on which your node server is running. I chose port 3000.
upstream attendahh.com {
server 127.0.0.1:1999;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name attendahh.com attendahh;
access_log /var/log/nginx/attendahh.log;
# pass the request to the node.js server with the correct headers and much $
location / {
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://attendahh.com/;
proxy_redirect off;
}
}
Then I service nginx restart.
I've read a bunch of tutorials and stack answers this is ostensibly all I need to do, but if I go to http://attendahh.com it does not work.
Things to note:
Going to my IP + port in the browser works just fine ( 23.226.227.16:1999 )
I have Cpanel for VPS installed (I tried to set up the DNS in there originally but it didn't work, I've since deleted the dns entry from there but it may still be affecting things)
Apache virtual hosts are commented out in httpd.config.
Any ideas on what I'm doing wrong here? Perhaps there's some conflict between apache and nginx?
- proxy_pass http://attendahh.com/;
+ proxy_pass http://attendahh.com;
Nginx uses its own resolver, it doesn't use the system's resolver (/etc/resolver).
You have to configure it using the resolver directive. If you are not using the resolver directive, then use IP address in the proxy_pass directive.
Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

nginx manage local servers depends on income domain name

I have a few applications that are running localy in defferents ports, how can I configure NGINX server for forwarding request from port 80 to my application depends on income domain name. For example 2 local apps named 'app1' on port 8181 , and if request comes from http://app1.com - nginx forwards to http://localhost:8181
I've looked at nginx docs, I ask for your examples if someone did this.
Thanks
Assuming you want to create a reverse proxy, my method is to first configure the following reverse proxy settings in a new file called /etc/nginx/reverse-proxy.conf:
# Serve / from local http server.
# Just add the following to individual vhost configs:
# proxy_pass http://localhost:3001/;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
Then, for each reverse proxy I'm configuring, I add an appropriately-named configuration file in /etc/nginx/sites-enabled with the following:
server {
server_name app1.com;
server_name www.app1.com;
location / {
include /etc/nginx/reverse-proxy.conf;
proxy_pass http://localhost:8181/;
}
}
You can create as many server blocks as you like and point them at different local (or even remote) application servers. You can also add location blocks to serve different URLs within the same domain statically, or from different local application servers.
(You can also just roll all the configuration into /etc/nginx/nginx.conf, but I find it easier to separate configuration out into multiple files.)
I managed to do this easily by following this tutorial.
Create a new file in /etc/nginx/conf.d/ called your-domain.com.conf and put this in it:
server {
listen 80;
server_name your-domain.conf.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
Then restart nginx
sudo service nginx restart

Resources