I've read in Nginx docummentation that instead of using IFs, new server blocks should be made.
I have tried making another block listening to 443 ssl, just to set there redvi.eu (non www) and then redirect it accordingly. It fails because of duplication.
How is the correct way to redirect https non-www here?
server {
listen 80 default_server;
server_name www.redvi.eu redvi.eu;
return 301 https://www.redvi.eu$request_uri;
}
server {
server_name www.redvi.eu;
root /home/deploy/redvi/current/public;
passenger_enabled on;
passenger_app_env production;
location /cable {
passenger_app_group_name redvi_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# Allow uploads up to 100MB in size
client_max_body_size 100m;
location ~ ^/(assets|packs) {
expires max;
gzip_static on;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.redvi.eu/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.redvi.eu/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
}
CodePudding user response:
Even if you don't want to use "IF", there's not much you can do.
I've been using it this way for many years and it works well.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com www.example.com;
root /var/www/sites/example.com/public;
if ($host != 'www.example.com') {
return 301 https://www.example.com$request_uri;
}
# others....
}