I am using nginx for managing multiple domains for reverse proxy.
I want to redirect all http request to https
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
However, I am not sure how can I make it work for rewriting non www requests to redirect to www in generic way excluding rewriting those requests that already has www in requests as if I just modify below rule it creates problem when someone write as www.domain.com it
rewrite to www.www.domain.com
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://www.$host$request_uri;
}
Please check this image to understand the issue.
So I need to have some condition that handles that.
3
Answers
Please try this
this will rewrite all 3 domains to https://yourdomain.com
I am not sure about my answer. I am not able to test this now.
please try this and comment back
If you want a generic approach, you can use
map
directive (should be placed outside theserver
block):Update 1
It’s look I misread your question (you need to redirect
example.com
towww.example.com
but my answer does an opposite thing), please try the following:Update 2
OP asks an additional question:
Yes, rewrite of
static
subdomain can be prevented with the followingmap
block:If you want to prevent rewrite of any three-component domain name, you can use
Non-www to www & https redirect
If I understand correctly, then you want to force non www to www. And you want to force all requests to https. This config code checks if the
$host
start with"www."
or not. If it does not, then nginx will add"www."
before the$host
then return the correct https location. It will work with subdomains as well.Possibly better way with Certbot
You can automatically produce the code you need by using Certbot. It will also help you manage certificates a lot and automate renewal. But Certbot will not change non-www to www, you’ll have to edit auto-generated config to look like the config above. The other difference is that you will be expected to have different config blocks/files per domain, but not sure if necessary. This is what the default config looks like per domain.
I think it’s best practice to separate the config files for each domain and subdomain and use the default Certbot format until you have understood Nginx more. This way you can disable the redirect of non-www to www very easily. The default code above from Certbot, would be what you would put when you don’t want to redirect in that case. Long term this makes more sense to me, but I understand when users are more comfortable with www or other parts of the website might need the www.