Multiple domains - subdomains routed to main domain - nginx

I have multiple domains connected to the same DO droplet, with nginx
Let's assume:
firstdomain.com
seconddomain.com
I would like to set my nginx in a way that, every subdomain will be directed to it's main domain, and it will be reflected in address bar too:
subdomain.firstdomain.com ----> firstdomain.com
asldk.firstdomain.com --------> firstdomain.com
test.seconddomain.com --------> seconddomain.com
and so on.
What is the simplest way to achieve this?

server {
listen 80;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
}

I want to post this answer, for future visitors and for myself (I'm sure I will forget)
server {
listen 80 default_server;
server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$;
if ($subdomains != "") {
rewrite ^/(.*)$ http://$domain/$1;
}
index index.html;
root /etc/nginx/conf.d/404;
}
It is almost identical with the solution I accepted, and it has a defined 404 page in conf.d to modify default nginx page.

Related

How to drop path in rewrite and proxy pass the args in nginx

Example request - http://localhost/iframe?ip=192.168.0.237
I want to proxy pass the request to the value of IP and remove the path and args after localhost/ .
Ideally the proxy_pass should point to 192.168.0.237 and the URL should be http://localhost/.
localhost /iframe {
rewrite ^/(iframe/.*)$ http://localhost/ permanent;
proxy_pass $arg_ip;
}
I'm not sure whether rewrite is the proper way to address this problem.
I would use the argument ip and a rewrite to remove the iframe location
server {
listen 8085;
location /iframe {
rewrite ^/iframe(.*)$ /$1 break;
proxy_pass http://$arg_ip;
}
}
server {
listen 8080;
location / { return 200 "$host$uri"; }
}
Security Notice
I just have a feeling you should whilelist the upstream servers accepted as arguments. If not this will be a wildcard proxy to every single http-server reachable in the network. This is a easy to use SSRF attack vector. So please add some extra layer of security.
SSRF Explained:
Let's say we use this configuration without any further security. Given the folowing NGINX config:
server {
listen 8085;
location /iframe {
rewrite ^/iframe(.*)$ /$1 break;
proxy_pass http://$arg_ip;
}
}
# Server for iframe service
server {
listen 8080;
root /usr/share/nginx/;
location / { return 200 "$host$uri\n"; }
}
# Private Server Section here!
server {
listen 8086;
allow 127.0.0.1;
deny all;
.....
location / {
index welcome.html;
}
}
Trying to reach the secret server directly
curl -v EXTERNALIP:8086
will fail with HTTP 403.
The NGINX will just allow connections form localhost/127.0.0.1 as defined in the allow/deny directives.
But lets try the iframe with the ip argument.
$# curl localhost:8085/iframe?ip=127.0.0.1:8086
Welcome to our very secure server! Internals only!
It prints the content of the secret server. Whitlisting a proxy-pass like this is never a good idea regardless its working or not.

nginx config - pass request from subdomain to path with argument, regex in location

I have a server view.example.com, and I have to take a request like this: view.example.com/1234 - domain + 4 numbers/letters.
What i want to do is proxy_pass the request to my local service at: 192.168.33.10/view/1234
How can I write the nginx config to:
care only about requests which match the regex (4 numbers/letters)
pass the request along to my service.
So far I have:
server {
server_name view.example.com;
listen 80;
location ~ '^/(?<hash>[a-zA-Z0-9\-_]{4})/?$' {
# rewrite - which I should probably use but not sure how
proxy_pass http://192.168.33.10/view/;
}
}
Arek
just checked this one:
server {
server_name view.example.com;
listen 80;
location ~ '^/(?<hash>[a-zA-Z0-9\-_]{4})/?$' {
rewrite ^ /view/$hash break;
proxy_pass http://192.168.33.10;
}
}
and it seems to do the trick - any better solutions ?
Arek

Redirect in Nginx variable subdomain

I have a bunch of subdomain http://product.domain.com that I would like to redirect to http://www.domain.com/product.
Of course, the product name can be different, and the redirect has to be done accordingly.
Any pointers?
Thanks
try something that could include several subdomains, like this
server {
server_name ~^(sub1|sub2|sub3|sub4).example.com;
return 301 $scheme://example.com/$1;
}
Try this:
server {
listen 80;
server_name product.domain.com;
return 301 http://www.domain.com/product$request_uri;
}

NGINX - Proxy (I think)

so I have 4 instances of node running for 4 different sites. I want to use Nginx, here is what I want.
www.site1.co.uk
= localhost:50010
www.site2.co.uk
= localhost:50020
www.site3.co.uk
= localhost:50030
test.site3.co.uk
=localhost:50031
I'm not asking for the code, (although it would be nice), what is the best way to achieve this sort of setup? I am still new to Nginx so sorry if it's a really simple question.
Thanks in advance,
Harry
To proxy the requests to those ports according to the domain being requested use this
server {
server_name www.site1.co.uk
proxy_pass http://localhost:50010
}
server {
server_name www.site2.co.uk
proxy_pass http://localhost:50020
}
server {
server_name www.site3.co.uk
proxy_pass http://localhost:50030
}
server {
server_name test.site1.co.uk
proxy_pass http://localhost:50031
}

Nginx redirect from one domain to another?

I'm on the process of migrating the same app but to a different domain.
For the old domain, I've the routes as:
http://app.example.com/app/users/sign_in?query=hello
I want it to be redirected to another domain omitting the app part as:
http://app.newexample.com/users/sign_in?query=hello
I tried with:
server {
...
location /app {
rewrite ^$scheme://app.sparkon.com/app(/.*)$ $1 last;
}
...
}
I doesn't work. How to achieve this?
I had this issue about a year ago and spent a long time looking for solutions. I found and use this:
server {
listen 80;
server_name example.com app.example.com;
rewrite ^/app(.*)$ http://app.newexample.com$1 permanent;
}
server {
listen 80;
server_name app.newexample.com;
# config for primary domain here
}
From this link. Credit to them.
Don't put scheme in the rewrite pattern:
server {
server_name app.example.com;
location /app/ {
rewrite ^/app/(.*)$ http://app.newexample.com/$1;
}
}
Brg.
I prefer this method as it doesn't need to use rewrite, one of the things that i read are good to avoid too much, cause it needs more processing by the nginx engine.
location ~ /app/(.*) {
return 301 $scheme://app.sparkon.com/$1;
}

Resources