Nginx - all domains redirect - nginx

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.

Related

nginx Redirect specific subdomain and path

Our nginx config serves multiple sites with their own subdomains.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ~^(?P<sub>.+)\.(example1|example2)\.com$;
root /var/www/instances/$sub;
...
}
I want to redirect one specific subdomain with its path to different sites, but I cannot figure out how to write the check. I need to check for the host part and then check for the path to decide where the redirect should land.
The map looks somehting like this:
Old URI
New URI
sub1.example1.com/wiki/0/$path
newSub1.example1.com/wiki/0/$path
sub1.example1.com/wiki/20/$path
newSub2.example1.com/wiki/0/$path
Where $path is simply the rest of the request URI
All other requests to sub1.example1.com should work as before.
The obvious solution is to split sub1.example1.com into a separate server block. As you will see from this document a server_name with an exact name always takes precedence over a server_name with a regular expression.
This means that there are two server blocks with near identical contents, but this can be mitigated by using the include directive.
Alternatively, you can test the value of $host$request_uri using a map directive. This is less efficient, as you will be testing the URL in every site.
For example:
map $host$request_uri $redirect {
default 0;
~*^sub1.example1.com/wiki/0/(?<path>.*)$ //newSub1.example1.com/wiki/0/$path;
~*^sub1.example1.com/wiki/20/(?<path>.*)$ //newSub2.example1.com/wiki/0/$path;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ~^(?P<sub>.+)\.(example1|example2)\.com$;
root /var/www/instances/$sub;
if ($redirect) { return 301 $scheme:$redirect; }
...
}

nginx on localhost - wildcard domains and wildcard subdomains

I configured nginx (and dnsmasq) to listen to example.test and *.example.test wildcard subdomain. Everything seems to work fine. Here is nginx.conf:
server {
listen 80;
server_name ~(\.)?example\.test$;
...
}
Now I want to respond to all other *.test domains from their own directory on disk. I just don't know how to make it happen, following config doesn't work (just disables above configuration):
server {
listen 80;
server_name \.test;
...
}
Even following configuration has same effect:
server {
listen 80;
server_name ~(?!(\.)?example)\.test;
...
}
Both these configs work, but disables *.example.test and example.test configuration and responds to them just as other *.test domains.
Here is my question:
How can I configure nginx to respond to *.test but respond to example.test and *.example.test in a different way?
After a wasting a whole day, I finally managed to fix the issue.
For those who may find themselves in such a confusing situation, here is the solution:
# First server block for default configuration:
server {
listen 80;
server_name ~^[a-zA-Z0-9\-_]+\.test$; # matches domain names (e.g. anything.test)
...
}
server {
listen 80;
server_name ~(\.)?example\.test$ example.test; # matches all subdomains (e.g. subdomain.example.test and sub.subdomain.example.test) as well as example.test
...
}

On nginx, Are those two server setting equal things?

I have settings to www rewrite or return.
1:
server {
listen 80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
2:
server {
listen 80;
server_name example.com;
return 301 https://www.$host$request_uri;
}
Are these two things equal?
It seems to be working well, but I can't have assurance.
It depends on which server block is the default server for port 80.
If this server block is also the implicit default server for port 80, it may need to handle requests for server names other than example.com, in which case the value of $host will not be equal to the value of $server_name. See this document for more.
You could use $server_name instead of $host. See this document for details.

Nginx subdomains with www

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;
}

redirecting request to a different listener nginx

I am having two listener 80 and 777. Port 80 act as a reverse proxy. And port 777 does some extra stuff and want to redirect to port 80. How do I redirect to a different port in nginx? I was trying with rewrite but later figured out that it is only used for change of path
###
server{
listen 80;
server_name _;
location / {
proxy_pass "http://upstream0;#" is included since links are not allowed in the post
}
}
server{
listen 777;
server_name _;
#doing some other extra stuf
//Want to redirect to port 80 under some condition
}
Is it possible?
Thanks
as far as nginx is concerned there's no real difference to passing something to another nginx listener/server and passing someting ot apache/mongrel/thin/... or any other http server
in other words if you want to pass things through to another listener you'd use proxy_pass
so what you want to do is something like
location / {
if (some condition) {
proxy_pass http://$host:80
}
}
see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Resources