Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
First, at the top of your configuration file, you have the following server directive:
server {
server_name myDomain.com;
return 301 $scheme://www.myDomain.com$request_uri;
}
The reason why it isn’t working is that you don’t have listen directive. If you take a look at other blocks, you can see that each of them have that directive. It’s instructing Nginx on what port to listen for that block, e.g. in you case you probably want both 80 and 443, so you redirect users accessing from both http and https protocols.
However, at the end of your configuration file, you have another server block, which is created by certbot and is doing the same thing. Because of it, you should remove the server block you added at the top of the file.
The server block at the end of configuration files looks like:
server {
if ($host = www.myDomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = myDomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
return 404; # managed by Certbot
}
This block is doing almost what you want when you’re accessing over http. What you can do is to modify it little bit to include www when your host is myDomain.com (pay attention to highlighted part):
server {
if ($host = www.myDomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = myDomain.com) {
return 301 https://www.$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
return 404; # managed by Certbot
}
At this point, you have the following redirects:
http://www.myDomain.com -> https://www.myDomain.com (redirecting insecure http to secure https; www is already part of the URL).http://myDomain.com -> https://www.myDomain.com (redirect insecure http non-www to secure https with www).But there’s missing one:
https://myDomain.com -> https://www.myDomain.com (redirecting secure non-www to www).This one is tricky, as for server block running on 443, you need to have valid certificates. If you already generated certificates for both non-www and www, you can try using them, in a server block such as:
server_name myDomain.com;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/codeverb.com/fullchain.pem; # managed$
ssl_certificate_key /etc/letsencrypt/live/codeverb.com/privkey.pem; # manag$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
return 301 $scheme://www.myDomain.com$request_uri;
Theoretically, something like this could work, but I can’t test it to verify it.
The much easier way, but depends on your application, is to allow both non-www and www. Then you don’t need redirections and forwards.