Tested in Ubuntu 14 / Varnish 4 / Nginx 1.11 / Apache 2.4
Using nginx as a proxy is the easiest and powerfull method to use SSL on a Varnish scenario, all incoming SSL traffic on 443 port will be redirected by nginx to varnish on port 80. Schema would be this:
We assume that varnish is runnig and caching requests to the backend, so let’s go to install nginx:
~ $ apt-get install nginx
Now you have to create a virtual host file with the SSL and proxy parameters:
server { listen 443 ssl; server_name domain.com; ssl_certificate /etc/ssl/certs/domain.com.pem; ssl_certificate_key /etc/ssl/private/domain.com.key; location / { proxy_pass http://127.0.0.1:80; proxy_redirect off; 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 X-Forwarded-Port 443; proxy_set_header Host $host; } }
Be sure nginx load this file, you can create it in /etc/nginx/conf.d directory with *.conf extension. Add this, if not exist, to the end of /etc/nginx/nginx.conf file inside the http block:
include /etc/nginx/conf.d/*.conf;
You can install a Let’s encrypt certificate or generate one self-signed.
Now restart nginx:
~ $ /etc/init.d/nginx restart
If you try to load domain.com via https probably you will see errors on load style sheets, images, even on secondary pages. This happens because wodpress doesn’t know that the connection is HTTPS, and internally try to serve content in plain HTTP.
To solve it, you have to tell the backend that changes to HTTPS if connection is originated in HTTPS.
Nginx as backend
Configure the HTTPS fastcgi parameter:
~ $ echo "fastcgi_param HTTPS $wordpress_https;" >> /etc/nginx/fastcgi_params
In /etc/nginx/nginx.conf add this to the http block:
map $http_x_forwarded_proto $wordpress_https { https on; }
And restart nginx:
~ $ /etc/init.d/nginx restart
Apache as backend
Be sure apache has loaded mod_setenvif module first. Then create the config file domain.com.conf in /etc/apache2/conf-available/ with the content:
SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
Create the symlink and restart apache:
~ $ ln -s /etc/apache2/conf-available/domain.com.conf /etc/apache2/conf-enable/domain.com.conf ~ $ /etc/init.d/apache2 restart
Now your wordpress should be loading correctly.