Skip to content

Instantly share code, notes, and snippets.

@williamgomes
Last active February 12, 2024 05:46
Show Gist options
  • Save williamgomes/ab9a0709f82273b5fd7c7797338076a3 to your computer and use it in GitHub Desktop.
Save williamgomes/ab9a0709f82273b5fd7c7797338076a3 to your computer and use it in GitHub Desktop.
Minimal working Nginx conf for CraftCMS with CertBot SSL redirect
server {
##### this section is for redirecting any non-SSL request to SSL
listen 80;
server_name www.example.com example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
##### this part should be copied from below section where it was auto generated by CertBot #####
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
##### this part should be copied from below section where it was auto generated by CertBot #####
##### this part is to redirect non-www request to www request with SSL #####
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
server_name www.example.com;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
rewrite ^/$ https://www.example.com/de-de/ redirect;
root /var/www/craftcms/web;
index index.php;
location / {
try_files $uri/index.html $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
try_files $uri $uri/ /index.php?$query_string;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTP_PROXY "";
}
##### this part should be auto generated by CertBot #####
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
##### this part should be auto generated by CertBot #####
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment