Last active
December 13, 2020 10:09
-
-
Save Khazbs/8a442ad18a8261025b296608e1e517e3 to your computer and use it in GitHub Desktop.
Try this site conf if you (use SSL/TLS certificates by letsencrypt certbot for nginx and) want to redirect http: to https: and www to non-www
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
# Listen for http: | |
listen [::]:80; | |
listen 80; | |
# Handle . and www. | |
server_name example.com www.example.com; | |
# Redirect to https: | |
return 301 https://$host$request_uri; | |
} | |
server { | |
# Listen for https: | |
listen [::]:443 ssl; | |
listen 443 ssl; | |
# Use letsencrypt certificate | |
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; | |
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; | |
include /etc/letsencrypt/options-ssl-nginx.conf; | |
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; | |
# Handle . and www. | |
server_name example.com www.example.com; | |
# Redirect www. to . | |
if ($host ~* ^(www)) { | |
rewrite ^/(.*)$ https://example.com/$1 permanent; | |
} | |
# Serve the request | |
# (Edit if needed) | |
root /var/www/example.com/html; | |
index index.html; | |
location / { | |
try_files $uri $uri/ =404; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment