Nginx server_name 52.com - How to use numerical domain names? - nginx

Maybe I'm going crazy, but I can't get a numerical domain to load anything. It just ignores server_name and loads the first site.
server_name 52.com;

Related

Redirecting one subdomain in Nginx

I'm a bit stuck on this one and any assistance would be amazing. I'm very much an Nginx noob.
We have an existing Nginx setup with lots of redirections already configured.
I'm trying to only redirect one subdomain in Nginx to an external url, however no matter what I try it redirects all subdomains.
I've reviewed our existing configs to get some understanding of how things are setup but the life of me I just can't work this one out and all the search results I seem to find are for redirecting all subdomains or sub sites not for redirecting just one subdomain of many.
This subdomain didn't have an existing config and I've had to create one (copy one of our others and edit it) it looks like previously the subdomain was configured under a catch all.
Example:
I need https://foo-bar.test.com.au to redirect to https://externalURL.com
However I don't want https://other-site.test.com.au to redirect to https://externalURL.com
This is the current config
server {
server_name foo-bar.test.com.au;
rewrite ^ https://externalURL.com;
root /opt/nginx-static/foo-bar;
}
server {
listen 443 ssl http2;
server_name foo-bar.test.com.au;
rewrite ^/(.*)$ https://externalURL;
ssl_certificate /etc/ssl/foo-bar.crt;
ssl_certificate_key /etc/ssl/foo-bar.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_timeout 30m;
root /opt/nginx-static/foo-bar;
location / {
root /opt/nginx-static/foo-bar;
index index.html foo-bar.html;
autoindex on;
}
}
I got this figured out.
Turns out the order of the .conf files matters
Renamed the config to zz.foo-bar.conf so it comes after default.conf
Also the permissions weren't set correctly on /opt/nginx-static/foo-bar
The other piece of the puzzle was DNS, I'd altered it earlier in my troubleshooting, turns out I needed to revert it back to the original DNS entry.

nginx and redirect to varnish

I have nginx config like:
server {
listen *:443 ssl;
server_name ~^(.*\.)?domain.com$;
second
server {
listen *:443 ssl;
server_name ~^(.*\.)?test.domain.com$;
Problem is that domain.com works fine but test.domain.com not. Redirects it to domain.com
If I simply do:
server_name www.domain.com$ domain.com$;
and
server_name test.domain.com$;
Then I always get problem with SSL
Any ideas how to solve it?
Just a guess. Swap first server block with second server block. So test is not eaten by your first server block. Or make sure your first server block does not eat test like so: server_name ~^(.*?[^t]?[^e]?[^s]?[^t]\.)?domain.com$;. Or maybe better like so: server_name ~^(.*(?<!test)\.)?domain.com$; (negative lookbehind - supported by Nginx???)
Completly untested - but maybe you get the idea.
Make sure that you - in your regex - escape all dots . with backslash \. or brackets [.] to make them literally a dot character. Otherwise you serve also test-domain-com for example, which you don't want.
Im not sure if you can use regex dollar $ in server_name if you don't mark it as regex ~.

Why does example.com behave exactly as .example.com?

I don't know why server_name example.com matches also subdomains (at least when there's not a rule for those), if there's an specific syntax for doing so:
Nginx documentation states that you can define server_name in the following ways:
Sets names of a virtual server, for example:
server_name example.com;
Server names can include an asterisk (“*”) replacing the first or last
part of a name:
server_name *.example.com;
Such names are called wildcard names.
The first two of the names mentioned above can be combined in one:
server_name .example.com;
But I tested this, and example.com behaves just as .example.com is expected, matching also all the subdomains.
This is not a problem as I can override the subdomains by setting a server for *.example.com but it seems very odd to me that if the syntax of .example.com exists with the intention to match both other two, it should mean that the other two don't match to eachother...
Why is this?
If your subdomains don't match any rule, they will be called back to the rule tagged as default_server, which, by default, is the first one on the file. In this case, the example.com rule.
This is the answer to the right question:
https://stackoverflow.com/a/64742545/8719655

nginx server_name regex

server_name in nginx does not match
I want to match with such FQDNs
I came up with
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net";
To Match
ucwebapi.testme.net
ucwebapi-uccore.testme.net
ucwebapi-uccore1-0.testme.net
ucwebapi-uccore999-999.testme.net
Validated with https://regex101.com/r/tAwEp9/2
Tested with
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net ucwebapi1.testme.net";
to see if ucwebapi1.testme.de server is reachable at all.
Is there any restriction im not aware of?
Thank you.
Try this:
server_name "~^(www.)?ucwebapi(-uccore)?(\d{1,3}-\d{1,3})?\.testme\.net";
It looks like there are some missing characters between your regex101 page and what ended up in your config.
I've also tuned it a bit so that it will NOT match:
ucwebapi-uccore999.testme.net
ucwebapi-uccore-.testme.net
ucwebapi-uccore-999.testme.net
I've never seen server_name configurations with double-quotes... but I'm not shure if that solves the problem.
Some example configurations here.
Edit: Do you have a default virtual server like this:
server {
listen 80;
server_name _;
return 404; # default
}
# now add your specific server
server {
listen 80;
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net ucwebapi1.testme.net";
...
}
Specific configurations will only work if you have a default configured.
#abcdn: your absolutely right, i didn't know that!
Try:
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net" ucwebapi1.testme.net;
Your server_name quotes encompassed the entire line. What is probably perceived as 2 separate entries, nginx interpolated as a single entry.

NGINX rewrite rule to remove leading www for Vanity URL

I've searched everywhere and although there are 1000s of examples of how to strip a leading www from a URL using NGINX rewrite rules, I've yet to find an example of how to strip the leading 'www' from a vanity url.
For example, convert 'www.fred.mysite.com' to 'fred.mysite.com'
Can you share an example of how this should work in an nginx rewrite rule?
the easiest way to do it is with a 2nd serverblock as follows:
server {
listen [::]:80; listen 80;
server_name www.fred.mysite.com;
return 301 $scheme://fred.mysite.com$request_uri;
}
server {
listen [::]:80; listen 80;
server_name fred.mysite.com;
#your site setup goes here
}
though you probably want to use "server_name *.fred.mysite.com;" in the 1st serverblock just to catch every possible extra prefix including misspellings

Resources