Score:0

How to force SSL on Nginx & Varnish

cn flag

how can i redirect only to https in Nginx and varnish. I use varnish cache in port 80 and Nginx listening on 8080. Works fine in http, but i need to add SSL. my configuration is as follows

server {
    listen  443 ssl http2 default_server;
    listen  [::]:443 ssl http2;
    server_name afrim.com www.afrim.com;
    port_in_redirect off;

    ssl on;
    ssl_certificate /etc/nginx/ssl/afrim_com_crt.crt;
    ssl_certificate_key /etc/nginx/ssl/afrim_com.key;

    location / {
        proxy_pass http://127.0.0.1:80; 
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_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 https;
        proxy_set_header HTTPS "on";
    }
}

server {
    listen 8080;
    listen [::]:8080;
    server_name afrim.com www.afrim.com;
    root /var/www/html/;
    index index.php;
    port_in_redirect off;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

}

server {
    listen  8080;
    listen  [::]:8080;
    server_name afrim.com www.afrim.com;
    return  301 https://afrim.com$request_uri;
}
Score:0
in flag

As mentioned on https://www.varnish-software.com/developers/tutorials/redirect/ you can use the following VCL code to perform HTTP to HTTPS redirections in Varnish:

vcl 4.1;

import proxy;

backend default {
    .host = "127.0.0.1";
    .port = 8080;
}

sub vcl_recv {
    if ((req.http.X-Forwarded-Proto && req.http.X-Forwarded-Proto != "https") || 
        (req.http.Scheme && req.http.Scheme != "https")) {
        return (synth(750));
    } elseif (!req.http.X-Forwarded-Proto && !req.http.Scheme && !proxy.is_ssl()) {
        return (synth(750));
    }
}

sub vcl_synth {
    if (resp.status == 750) {
        set resp.status = 301;
        set resp.http.location = "https://" + req.http.Host + req.url;
        set resp.reason = "Moved";
        return (deliver);
    }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.