Skip to content

Instantly share code, notes, and snippets.

@frengky
Last active September 18, 2021 12:38
Show Gist options
  • Save frengky/92e001103a7033655dd6 to your computer and use it in GitHub Desktop.
Save frengky/92e001103a7033655dd6 to your computer and use it in GitHub Desktop.
Nginx dynamic document root folder and sub-domain configuration
#!/bin/sh
# Self signed certificate for Nginx HTTPS
#
openssl req -x509 -nodes -days 1825 -newkey rsa:2048 -keyout /etc/nginx/ssl/my-domain.key -out /etc/nginx/ssl/my-domain.crt
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/my-domain.crt;
ssl_certificate_key /etc/nginx/ssl/my-domain.key;
server_name my-domain.com;
rewrite ^(.*) http://www.my-domain.com$1 permanent;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/my-domain.crt;
ssl_certificate_key /etc/nginx/ssl/my-domain.key;
server_name ~^(.*)\.my-domain\.com$;
set $subdomain $1;
set $baseroot "/home/my-domain/sites";
set $docroot "";
if (-d "${baseroot}/${subdomain}/webroot") {
set $docroot "/webroot";
}
if (-d "${baseroot}/${subdomain}/public") {
set $docroot "/public";
}
if (-d "${baseroot}/${subdomain}/public_html") {
set $docroot "/public_html";
}
root "${baseroot}/${subdomain}${docroot}";
index index.html index.htm index.php;
autoindex on;
error_log /var/log/nginx/my-domain-error.log error;
access_log /var/log/nginx/my-domain-access.log;
location ~* \.(jpg|jpeg|gif|png|css|js|ico)$ {
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
include alias.d/*.conf;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/my-domain.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
location ^~ /phpMyAdmin {
#allow 203.128.67.2;
#deny all;
if ($scheme = 'http') {
rewrite ^ https://$http_host$request_uri? permanent;
}
# Assuming /home/website/phpMyAdmin is exists
root /home/website;
index index.php;
access_log off;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/my-domain.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment