By OokmaKyi
I am trying to redirect https:// to https://www. Here is the block of code I am using:
server {
# root /home/user/www;
index index.html index.htm index.nginx-debian.html;
server_name ookma-kyi.tech www.ookma-kyi.tech;
location / {
root /home/user/www;
try_files $uri $uri/ =404;
}
location /demo {
root /home/user/app;
rewrite /demo/(.*) /$1 break;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name ookma-kyi.tech www.ookma-kyi.tech;
return 404; # managed by Certbot
}
I tried adding a server block at the top with a 301 redirect like so:
server {
listen 443;
server_name ookma-kyi.tech
ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem; # managed by Certbot
return 301 https://www.ookma-kyi.tech$request_uri;
}
server {
# root /home/user/www;
index index.html index.htm index.nginx-debian.html;
server_name ookma-kyi.tech www.ookma-kyi.tech;
location / {
root /home/user/www;
try_files $uri $uri/ =404;
}
location /demo {
root /home/user/app;
rewrite /demo/(.*) /$1 break;
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = ookma-kyi.tech) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name ookma-kyi.tech www.ookma-kyi.tech;
return 404; # managed by Certbot
}
All that did was show the Nginx welcome page. Any ideas?
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!
Hello,
In order to redirect non https:// traffic to https://www you can use:
### redirect HTTPS n
server {
listen 443 ssl;
server_name ookma-kyi.tech;
ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem;
return 301 https://www.ookma-kyi.tech$request_uri;
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
Redirect all HTTP traffic to HTTPS
# Redirect www.ookma-kyi.tech:80 to https://www.ookma-kyi.tech:443
server {
listen 80;
access_log off;
error_log off;
server_name www.ookma-kyi.tech;
return 301 https://$server_name$request_uri;
}
# # Redirect http://ookma-kyi.tech:80 to https://ookma-kyi.tech:443
server {
listen 80;
access_log off;
error_log off;
server_name theos.in;
return 301 https://$server_name$request_uri;
}
Check the nginx configuration file for any errors:
nginx -T
If there are any errors, you can try to fix them and then check the syntax of the file again.
systemctl restart nginx
Let me know how it goes.
Alex
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.