Subdomain is unexpectedly resolving despite Nginx not being set up to reverse proxy it - nginx

So I have been setting up my home network to host a few websites under a domain (and it's subdomains) using a combination of cloudflare to proxy and provide DDoS protection/HTTPS to the sites, and an Nginx reverse proxy running on my network to allow multiple sites to return from behind the same gateway that the DNS records in cloudflare are pointed at.
For the purposes of this explanation, I will replace my real domain name with [domainNameHere].
The first thing to explain is my DNS setup on cloudflare.
I have 4 CNAME records setup to do the following:
Note that I'm using CNAME records because I do not have a static home IP, therefore I'm using a Dynamic DNS address that resolves to the IP address of my gateway. This same Dynamic DNS address is used in place of an A record as I'm aiming to not need to update A records all the time, and instead just have it resolve via a automatically updating Dynamic DNS record.
[domainNameHere].net - Reverse proxy returns the root site when this domain is requested.
www.[domainNameHere].net - Behaves the same as the above, just there to handle any www requests, the reverse proxy returns the same root site for both wwww and the root domain name.
map.[domainNameHere].net - When this subdomain of [domainNameHere] is called, the reverse proxy instead returns a different site (a map, as you might have guessed).
test.[domainNameHere].net - This is a proxied DNS record setup in cloudflare for future purposes, I do not yet intend for it to actually return a site.
Now, the expected behaviour is that all of these DNS records should currently return a site, expect for test.[domainNameHere].net which shouldn't - I'd expect it to just return a standard ERR_NAME_NOT_RESOLVED like any other DNS record that doesn't actually go anywhere.
Instead though, when test.[domainNameHere].net is used, it returns the root site that [domainNameHere].net and www.[domainNameHere].net resolve to?
Using map, www or the root domain name all return the expected content.
I believe that I have configured something incorrectly in the Nginx settings, below are the two configuration files that are currently in my sites-enabled directory:
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.nginx-debian.html;
server_name [domainNameHere].net www.[domainNameHere].net;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name test.[domainNameHere].net;
location / {
proxy_pass http://jake-server:8123;
}
}
Is there anything wrong with my Nginx setup? I thought the behaviour would be that if I haven't setup test.[domainNameHere].net within Nginx, then it wouldn't resolve on the reverse proxy and therefore wouldn't return anything, or would just return the Nginx 404/403 page?
Is it maybe something to do with how the first setup file is setup to point to local files on the proxy, rather than using proxy_pass like is usually done in a reverse proxy?
Or is this not an issue with Nginx, and is an issue with how I've set Cloudflare up?

Related

Nginx reverse proxy without defining server_name?

I need to access a webserver in a private network, that has no direct access from outside. Opening router ports etc. is not an option.
I try to solve this with a raspi in that network, that i can manage via upswift.io.
Amongst other things, upswift allows temporary remote access to a given port over url's like
http://d-4307-5481-nc7nflrh26s.forwarding.upswift.io:56947/
This will map to a port that i can define.
With this, i can access a VNC Server on the pi, start a browser there and access the webserver i need.
But i hope to find a more elegant way, where i can access the Site from my local browser, and where the Pi does not need to run a Desktop.
As far as i found out, this can be done with a reverse proxy like nginx.
I found a lot of tutorials on it, but i struggle at one point:
After being able to install nginx and accessing it's default index page from my local browser through the temporary upswift.io url, i can't get it to work as reverse proxy.
I think my conf needs to look like
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://192.x.x.2;
}
}
Where example.com would be the name or IP under which the device is accessed.
Now, this would not work for me, as that name is dynamic.
So i wonder if there's a way to configure nginx so it does not need that name. I would expect that is possible, as the default webserver config works without it too. Are reverse proxies different in that regard?
Or, is there a better way than with a reverse proxy to do what i want?
You could try to define it as a default block
server {
listen 80 default_server;
server_name _;
location / {
proxy_pass http://192.x.x.2;
}
}

Super basic question on linking domain to server

I have a super basic question. I have a GoDaddy account set up with subdomain xxx.mydomain.com. I also have some services running in an AWS instance on xxx.xxx.xxx.xxx:7000. My question is, what do I do to configure so that when people click xxx.mydomain.com it goes to xxx.xxx.xxx.xxx:7000?
I am not talking about domain forwarding. In fact, I also hope to do the same for yyy.mydomain.com to link it to xxx.xxx.xxx.xxx:5000. I am running Ngnix in xxx.xxx.xxx.xxx. Maybe I need to configure something there?
You want a reverse proxy.
Add two A-records to your DNS configuration to map the subdomains to the IP address of the AWS instance. With GoDaddy, put xxx / yyy in the "Host" field and the IP address in the "Points to" field. (more info)
Since you already have Nginx running, you can use it as a reverse proxy for the two subdomains. Therefore, add two more server blocks to Nginx's configuration file. A very simple one could look like this:
http {
# ...
server {
server_name xxx.mydomain.com;
location / {
proxy_pass http://localhost:7000;
}
}
server {
server_name yyy.mydomain.com;
location / {
proxy_pass http://localhost:5000;
}
}
}
You might want to rewrite some headers depending on your services/applications (more info). Also, consider to use Nginx for SSL termination (more info).

Problem redirecting subdomain to nginx web server

Problem explanation
I want to show one service in my domain and another different service in my subdomain, so I purchased a domain and tried to link it to my Nginx web server.
I have buyed the domain in google domains. And uploaded my web application to a Nginx web server.
When I run my domain for example www.balls.com it's redirected perfectly, but when I try to redirect to my subdomain, there is no way to make it work, I have tried different configurations and received 2 different errors:
-DNS doesn't exist (Checked with DNScheck).
-Google DNS page error.
google domain service config:
Nameservers:
-Using the Google Domains nameservers.
DNSSEC
-Your domain has DNSSEC enabled.
Registered hosts
red.balls.com-->111.11.11.11 (My server Address).
Synthetic records
Domain forward
balls.com, www.balls.com → 111.11.11.11 (My server Address).
Custom resource records (Extracted from exported records)
balls.com.balls.com. 3600 IN A 111.11.11.11
red.balls.com.balls.com. 3600 IN A 111.11.11.11
www.balls.com. 3600 IN A 111.11.11.11
www.balls.com.balls.com. 3600 IN A 111.11.11.11
nginx services config:
Subdomain service:
server {
listen 80;
root /var/www/redballs.com;
index index.html index.htm;
server_name red.balls.com;
}
Subdomain service:
server {
listen 80;
root /var/www/balls.com;
index index.html index.htm;
server_name balls.com;
}
Thanks for your read time, a simple comment pointing what I'm doing wrong will be very appreciated, as I have finished my application but I'm unable to set up this subdomain. Thanks again.
that sounds a lot like a DNS problem to me.
Did you add a DNS record for the subdomain?
Each hostname you want to use needs a mapping in DNS to the IP addresses of the host or a CNAME pointing to another name.
It is a bit hard to debug a DNS Problem without seeing thr actual data.
Sidenote: www.balls.com.balls.com. looks to me like you forgot a dot when creating that entry. In DNS a name always ends with a dot in the configuration (or zone file) if you omit it, the domain name is usually automatically appended.

Subdomain returns root domain's index page

I am trying to configure wildcard subdomains for my app but whenever I enter the subdomain I receive my root domain's index page. My app is configured through cloudflare and then I have an Nginx server that either serves the static files or pipes the request to a node app.
Within cloudflare I've added two rules in this order
http://*domain.com/*
-always use https
http://*.domain.com
-always use https
Then within nginx I have two server blocks
server
{
listen 80;
server_name www.domain.com domain.com;
//this will redirect user to use https
}
server
{
listen 443;
//this is my root domain's server that should handle all traffic for the root domain
}
then I have a block for the subdomains
server
{
listen 443;
server_name ~[^(www)|0-9A-Z]\.domain.com
//proxies the request to the node app
}
The strange thing is that I am seeing the request being handled by the correct routes within the node app but I am getting the wrong file (the root domain's index page).
I am a bit confused since at first I suspected it might have been cloudflare caching the index page and serving that for all subdomains but then if that were the case why I am able to see the requests reaching my server?

How to Alias a Page to a Sub-Domain

I'm running WordPress on a LEMP stack.
I have a landing page at: https://example.com/landing
I want to point this subdomain at the above page: http://landing.example.com
I don't want to 301/302 redirect the subdomain to the subdirectory.
I want visitors to think they are still on http://landing.example.com.
This question is similar but doesn't solve my particular problem.
I need to know how to rewrite the request with nginx and configure DNS.
Link to previous post discussing rewrites/redirects/vhosts. You want to rewrite the request and then either land it on the same server or proxy it to a different one.
I found the answer in this thread.
To summarize, you need to proxy the request with nginx:
# abc.example.com
server {
listen 80;
server_name .example.com;
location / {
proxy_pass http://127.0.0.1/abc$request_uri;
proxy_set_header Host example.com;
}
}
The just setup an A record pointing at your server's IP.

Resources