Problem redirecting subdomain to nginx web server - nginx

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.

Related

Redirect domain to different ports based on subdomain - AWS

I have a EC2 instance on which I am running a React App and a domain from Route 53, for example xyz.com.
For this xyz.com I have two subdomains say www.xyz.com and demo.xyz.com.
Now, traditionally my React App serves on PORT 3000 on this instance. I have configured the IP with the PORT using nginx on my instance, something like this 192.1.1:3000. Now whenever I hit this domain (www.xyz.com) I am redirected to 192.1.1:3000.
For example, if I have two versions of my app called dev and prod. dev serves on PORT 3000 and prod serves on PORT 3002.
I want to www.xyz.com to redirect to 3000 and demo.xyz.com redirect to 3002. Both of these are from the same DNS provider.
Is there a away to achieve this?
try this way:
server {
server_name www.xyz.com;
location / {
proxy_pass http://192.1.1:3000;
}
}
server {
server_name demo.xyz.com;
location / {
proxy_pass http://192.1.1:3002;
}
}
( add extra parameters if you want to add regarding the request, header etc )
I hope this answer helps you.
Feel free to comment if you get any errors in this and don't forget to mention the answer as accepted if it works. It'll help the others who is looking for the silimar answer.

Wilcard domain with DigitalOcean and Nginx to catch all requests to subdomains

I have a large list of subdomains and the list continues to grow.
I recently started using Digital Ocean and I need to recreate the list of subdomains but the process of creating them one by one using the DNS Server control panel is very time consuming and besides that, whenever I need to create a new subdomain I will have to do it manually.
I'm trying to create a wildcard domain so that all subdomains are captured by the DNS server and then each one is redirected to the specific service.
The service that will be used is Odoo and it will work together with Nginx.
What I've done so far:
I registered the new domain name (example.com);
Updated the Domain Name Servers to point to Digital Ocean (ns1.digitalocean.com, ns2.digitalocean.com, ns3.digitalocean.com);
In the DNS control panel at Digital Ocean, I created the following DNS records:
A *.example.com xxx.xxx.xxx.xxx 3600
CNAME www.example.com example.com 43200
A example.com xxx.xxx.xxx.xxx 3600
NS example.com ns1.digitalocean.com 1800
NS example.com ns2.digitalocean.com 1800
NS example.com ns3.digitalocean.com 1800
For the records "example.com" and "www.example.com" everything seems to be working because when I ping I see that the traffic is redirected to the Digital Ocean droplet.
In Nginx I created a server block and included as servers:
server {
...
server_name example.com www.example.com *.example.com .example.com test.example.com;
...
}
With this configuration I can easily access the Odoo server using the addresses: example.com and www.example.com but when I try to access the address "test.example.com" I cannot access the server.
Surely it will be necessary to perform some more step that passed me and I didn't find in my searches?
I found that during the tests I had placed a record in the /etc/hosts file with:
127.0.0.1 test.example.com
After removing this record, everything is working as expected.

Nginx uWSGI link server_name to domain for flask application

I have written a Flask website which I hosted with waitress on a Ubuntu 20.04 VM server at port 5000, but now I'd like to do it more properly with uWSGI/Nginx.
To learn uWSGI/Nginx I am following this tutorial: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-20-04
Here in step 6 it tells my to configure Nginx as follows:
server {
listen 80;
server_name my_domain www.my_domain;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/sammy/myproject/myproject.sock;
}
}
However if I now request the webpage for my domain, it only returns the default Nginx page.
But if I "hardcode" the public IP address of my server to the server_name:
server_name: my_domain www.my_domain MY_IP
Then it does show the correct page. I however have a feeling this is not the intended way to configure the server.
When I used the old waitress server i had told my DNS server redirect to http://MY_IP:5000/, but it is now set to redirect to http://MY_IP/. (with redirect mode 302)
So my question is, how should I set up my Domain name redirect or Nginx config so that it works without a "hardcoded" ip? Or is this something where I just need to wait the 48 hours for the DNS update to propagate?
Also an auxiliary related question, how do I make the browser url bar show the domain name instead of an IP address?
Thanks in advance!
You can go through my answer, hope it helps you as well
Regarding your question for URL name in browser, it will be there once DNS name is propagating properly and it shouldn't take more than 5-10 minutes.
Remove the IP from server name, its not supposed to be entered there.

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

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?

Is it fine to add 100s of domains to single website in nginx

In nginx, we can set multiple domains to a single website like
server localhost abc.com def.com ghj.com
I want to keep 100s of domains that way.
The point is - we are auto generating websites. And we want to point all those domains to single website. Using the domain name/url, we want to send angular ajax requests which will fetch data related to that domain.
So, content will be dynamic.
Use a default server as a catch-all for any domain name that does not have an explicit server_name. For example:
server {
listen 80 default_server;
...
}
You do not need to specify a server_name directive for default servers.
See this document for details.

Resources