Skip to content

Instantly share code, notes, and snippets.

@jeffbr13
Last active August 29, 2015 13:57
Show Gist options
  • Save jeffbr13/9354450 to your computer and use it in GitHub Desktop.
Save jeffbr13/9354450 to your computer and use it in GitHub Desktop.
Nginx virtual host configuration with HTTPS and redirection for common variants.
## /etc/nginx/sites_enabled/website.tld
#
# Nginx virtual host configuration for <https://website.tld>,
# redirecting all traffic from <http[s]://[www.]website.tld>.
server {
server_name website.tld;
root /var/www/website.tld;
# HTTPS (IPv4)
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/website.tld.crt;
ssl_certificate_key /etc/nginx/ssl/website.tld.key;
location / {
index index.html index.htm index;
try_files $uri $uri/ $uri.html =404;
}
}
# Redirect <https://www.website.tld> to <https://website.tld>
server {
server_name www.website.tld;
listen 443 ssl;
return 301 https://website.tld$request_uri;
}
# Redirect HTTP traffic to HTTPS
server {
server_name website.tld;
# HTTP (IPv4)
listen 80;
return 301 https://website.tld$request_uri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment