I have remote Linux Ubuntu 20 running Nginx reverse proxy for a Node app with LetsEncrypt SSL cert recently installed and I want to enforce https on all requests. Currently both http and https work as expected. How can I update my config to redirect all http to https?
Protocol: IPV4
Firewall: Nginx Full ALLOW Anywhere
Node app location /var/www/html/mydomain
Nginx config location: /etc/nginx/sites-available/default
Nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/mydomain;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name mydomain.com www.mydomain.com;
location ^~ /assets/ {
gzip_static on;
expires 12h;
add_header Cache-Control public;
}
location / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:3000;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/gfrweb.site/fullchain.pem; # managed by Certbot
The site is working for both http and https and I want it to redirect all http traffic to https.
Just move your http listener into a single server block and use this:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mydomain.com www.mydomain.com;
return 301 https://$host$request_uri;
}
And remove the lines
listen 80 default_server;
listen [::]:80 default_server;
from your other server block.