nginx maximum value of server_name - nginx

I have a block of server_names which cover the redirection of 14 domains to https
server {
listen 80;
listen [::]:80;
server_name saim-service.com www.saim-service.com [...];
return 301 https://$server_name$request_uri;
}
The block functions for the first four domains listed, then breaks down and redirects in https to that first domain listed. nginx version: nginx/1.8.1 Is nginx or this version subject to limitations on the number of server_names? Is this a defect of 301 response which is then hard-wired into the browser (as a permanente redirect) and previous testing may have polluted the browser? Is there any way to reliably overcome this problem?

Related

Configuration issue with "return 301" on nginx and aws

I have had a nginx server running on aws with an ubuntu 16.04 OS. My current sites SSL "return 301 https://$host$request_uri" works flawlessly but is not working on the new site.
I have tried configuring the default, and sites-enabled/website1.ca.conf and sites-enabled/website1.ca.conf
server {
server_name site2.ca www.site2.ca;
listen 80;
listen [::]:80;
return 301 https://$host$request_uri;
#return 301 https://site2.ca$request_uri;
root /var/www/vhosts/site2.ca/;
index index.html index.htm index.php;
This page isn’t working site2.ca redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
After hours and hours of jerking around I analyzed the SSL cert and realized that Cloudflare's default settings use their SSL rather than the sites.
Resolution was setting Crypto to Full SSL.

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.

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 dynamic HTTP/S resolves to https://_

I'm trying to let all traffic for my nginx be redirected to HTTPS, independent of server name. So, any other vhost should be redirected to its HTTPS counterpart.
example.com -> https://example.com
test.com -> https://test.com
...
Yet, instead of using the incoming $server_name or $host (tried both), it keeps redirecting to a plain https://_. Is my config incorrect?
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$server_name$request_uri;
}
Okay, rather silly. If you have previously setup any non-working configuration (e.g. one which will result in the faulty redirect), the browser will remember this redirect without asking the server again (as according to HTTP 301 - permanently moved). So, either clear the browser's data or try with a different one.

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