How To Route SSL Traffic With Nginx

This is the code inĀ /etc/nginx/conf.d/default.conf:

server {
 listen 443;
 ssl on;
 server_name subdomain.vpsbox.com;
 ssl_certificate /var/lib/lxc/lxc_container/rootfs/etc/apache2/cert.crt;
 ssl_certificate_key /var/lib/lxc/lxc_container/rootfs/etc/apache2/cert.key;

location / {
 proxy_pass https://10.0.3.2:443;
 proxy_set_header X-Forwarded-Proto https;
 add_header Front-End-Https on;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_buffering off;
 proxy_redirect off;

}
}

Note: nginx (on the main host) is using same ssl certificate as apache on the lxc container.

server {
 listen 80;
 server_name subdomain.vpsbox.com;;
 return 301 https://10.0.3.2$request_uri;
}

This dirrective redirects the http traffic to https.

Leave a Comment