Skip to content

Instantly share code, notes, and snippets.

@nmfzone
Created May 8, 2020 15:34
Show Gist options
  • Save nmfzone/64408fb571a473f885cf19ce89f94e97 to your computer and use it in GitHub Desktop.
Save nmfzone/64408fb571a473f885cf19ce89f94e97 to your computer and use it in GitHub Desktop.
NginX Config for Multi Tenant Sites
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/letsencrypt;
}
server {
listen 80;
listen [::]:80;
server_name domain.com;
include /etc/nginx/snippets/letsencrypt.conf;
# Redirect http://domain.com to https://domain.com
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
root /var/www/path/to/site;
index index.php index.html index.htm;
server_name domain.com;
ssl_certificate /etc/letsencrypt/live/domain.com-0001/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com-0001/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/domain.com-0001/fullchain.pem;
include /etc/nginx/snippets/ssl.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
# Allow CORS for static files (if you're using Amazon S3, you may not need this)
location ~ \.(ttf|ttc|otf|eot|woff2?|css|js|gif|png|jpe?g|svg|svgz|ico|webp)$ {
if ($http_origin ~ 'https?://.*\.domain\.id') {
add_header Access-Control-Allow-Origin "*";
}
}
}
server {
listen 80;
listen [::]:80;
server_name *.domain;
include /etc/nginx/snippets/letsencrypt.conf;
# Redirect http://*.domain to https://*.domain.com
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
root /var/www/path/to/site;
index index.php index.html index.htm;
server_name *.domain.com;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
include /etc/nginx/snippets/ssl.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}
}
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
@nmfzone
Copy link
Author

nmfzone commented May 8, 2020

You may need to obtain Wildcard SSL for your multi tenant:

./certbot-auto --server https://acme-v02.api.letsencrypt.org/directory -d *.domain.com --manual --preferred-challenges dns-01 certonly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment