Nginx subdomains with www - nginx

I have Nginx configured in a Amazon EC2 server.
Right now both www.myserver.com, .myserver.com works perfect.
What I need, is configure www..myserver.com. I need to redirect the user to .myserver.com. I mean, I need to rewrite the url or something.
How can I do that?

server {
listen 80;
server_name www.myserver.com;
return 301 http://myserver.com$request_uri;
}
You don't need "rewrites" in nginx. Leave them to apache, and read the docs:
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/request_processing.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html
Upd:
That is not clear from your question (who knows, what do you mean by double dots). But if you have ever tried to read the docs, you would easily modify the example for your needs:
server {
listen 80;
server_name ~^www\.([^.]+\.myserver\.com)$;
return 301 http://$1$request_uri;
}

Related

Nginx URL forwarder to another domain

How can I redirect someone from one domain to another whilst keeping the current URL path?
For example:
example.com/ABC -> example2.com/file.html?x=ABC
You can create a rewrite rule to forward from the old domain to the new one.
server {
listen 80;
server_name example.com;
return 301 http://new.example.com$request_uri;
}
Documentation: http://nginx.org/en/docs/http/converting_rewrite_rules.html

Should I use Nginx or Route53 for redirection?

I have problem that I don`t know how to fix. I want when someone go to webmail.mywebsite.com get redirect to gmail.com
So far I try to do it using Nginx using this code
server { Server_name webmail.mywebsite.com return 301 https://www.gmail.com; }
But nothing happens I get 'DNS_PROBE_FINISHED_NXDOMAIN' error on my browser.
Can anyone help me how to solve this problem.
Also I want to mention that I have SSL (let`s encrypt) install on my ec2
can you try following code block for NGINX? Looks like you are missing http/https port number.
Once configured please validate the NGINX syntax and restart NGINX server.
server {
# For HTTP
listen 80;
# For HTTPS
listen 443;
server_name webmail.mywebsite.com;
return 301 https://www.gmail.com;
}

Nginx - all domains redirect

I looked around to see NGINX configuration for ALL domains and sub-domain but I can only find configurations that are specified.
This is what I want to achieve
server {
listen 80;
server_name all;
return 301 https://www.test.com$request_uri;
}
but then I don't want www.test.com forwarded, only anything else that doesn't match it, even if it is like x.test.com it should be forward too
how I can do it?
You need to provide a separate server block for the domain you don't want to redirect. For example:
server {
listen 80;
server_name www.test.com;
# rest of configuration
}
server {
listen 80 default_server;
return 301 https://www.test.com$request_uri;
}
BTW, you are redirecting to https. Then you need to listen not only 80, but 443 as well. I hope you have it working and left out for the question's simplicity. Otherwise, the documentation is here.

Fourth-Level Subdomain Forwarding

I've recently been trying to set up a reverse proxy that would forward certain 4th-level subdomains to particular locations. So, for example, this is what I'm trying to accomplish (configuration in my nginx file):
server {
listen 80;
server_name *.server.domain.com;
rewrite ^ https://$server_name$request_uri;
}
The goal here being that if someone went to, for example, http://item1.server.domain.com, they would be re-routed to https://item1.server.domain.com. However, with this configuration, the URL gets rewritten to https://%2A.server.domain.com.
Is there a way to fix this so that the full domain (item1) gets added correctly to the rewritten URL? Ideally, I wanted it to eventually be able to rewrite any subdomain on server.domain.com directly to https.
Thanks!
The $server_name variable contains the text from the value of the server_name directive. The %2A is a URL encoded representation of the leading *.
Use $host or $http_host to obtain the hostname actually requested by the client. See this document for more.
For example:
server {
listen 80;
server_name *.server.domain.com;
return 301 https://$host$request_uri;
}
Note: Restart nginx and clear the browser cache between each test. Check the configuration using nginx -T.

nginx rewrite for subsubdomains to subdomains

We have an application where we use subdomains for each of our customer's installations. so we have customer1.ourapp.com, customer2.ourapp.com, customer3.ourapp.com and so on.
Because of security we want to redirect all http to https, since we have a wildcard SSL certificate.
Also, some customers are not that tech savvy and add www to their domain name, so then you get things like: http://www.customer1.ourapp.com or https://www.customer1.ourapp.com. In those cases the SSL certificate isn't valid because of the subsubdomain.
I'm trying to write the vhost config for nginx to make the correct redirect in these two cases. I got the http to https redirect to work with:
server {
listen 80;
server_name *.ourapp.com;
#Rewrite all nonssl requests to ssl.
return 301 https://$host$request_uri$is_args$args;
}
correct url's use:
server {
listen 443;
server_name *.ourapp.com;
#Rest of config
}
Made an attempt for the subsub domains, but it's not matching:
server {
server_name "~^(.*)\.(.*)\.ourapp\.com$";
return 301 https://$2.ourapp.com$request_uri;
}
Any idea how to get this working?
Wildcarded server takes precedence over regexp'ed one and matches 'www...' too.
You can use one definition for both cases:
server_name ~ ^(?:.*\.)?(.*)\.ourapp\.com$;
return 301 https://$1.ourapp.com$request_uri;

Resources