Skip to content

Instantly share code, notes, and snippets.

@zviryatko
Last active July 22, 2021 10:40
Show Gist options
  • Save zviryatko/600ee141f70e866533a5 to your computer and use it in GitHub Desktop.
Save zviryatko/600ee141f70e866533a5 to your computer and use it in GitHub Desktop.
Nginx config to catch all .devel sites

This little gist help you to catch all *.devel domain and open localhost for it.

First you need to set up local DNS server and add it to resolv conf.

sudo apt-get install bind9 dnsutils resolvconf

bind9 - DNS server

resolvconf - help you to manage you /etc/resolv.conf file automatically independs on network configuration

Copy config from gist to bind config dir:

sudo cp db.devel /etc/bind/db.devel

sudo cp named.conf.local /etc/bind/named.conf.local

sudo systemctl restart bind9 - restart dns server

echo 'nameserver 127.0.0.1' | sudo tee --append /etc/resolvconf/resolv.conf.d/tail - add local dns to resolvconf config file

sudo resolvconf -u - update /etc/resolv.conf file withour reboot

;
; BIND data file for local loopback interface
;
$TTL 1h
devel. IN SOA ns.devel. admin.devel. (
2009010910 ;serial
3600 ;refresh
3600 ;retry
3600 ;expire
3600 ;minimum TTL
)
devel. IN NS ns.devel.
devel. IN MX 10 mail.devel.
devel. IN MX 20 mail.devel.
@ IN A 127.0.0.1
* IN A 127.0.0.1
www IN A 127.0.0.1
*.devel IN A 127.0.0.1
mail IN A 127.0.0.1
ns IN A 127.0.0.1
devel. IN TXT "v=spf1 a mx ip4:127.0.0.1 -all"
devel. IN SPF "v=spf1 a mx ip4:127.0.0.1 -all"
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
#listen 443 ssl;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
server_name ~^((?<subdomain>.*)\.)(?P<project>.+)\.devel$ ~^(?P<project>.+)\.devel$;
#ssl on;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_certificate /etc/nginx/ssl/nginx.crt;
#ssl_certificate_key /etc/nginx/ssl/nginx.key;
set $original "${project}.devel";
if (-d /home/alexd/www/$project/master/docroot) {
set $project "${project}/master/docroot";
}
if (-d /home/alexd/www/$project/docroot) {
set $project "${project}/docroot";
}
if (-d /home/alexd/www/$project/web) {
set $project "${project}/web";
}
if (-d /home/alexd/www/$project/code) {
set $project "${project}/code";
}
if (-d /home/alexd/www/$project/public) {
set $project "${project}/public";
}
root /home/alexd/www/$project;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /misc/favicon.ico {
log_not_found off;
access_log off;
alias /home/alexd/www/favicon/drupal.ico;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \..*/.*\.php$ {
return 403;
}
# No no for private
location ~ ^/sites/.*/private/ {
return 403;
}
# Block access to "hidden" files and directories whose names begin with a
# period. This includes directories used by version control systems such
# as Subversion or Git to store control files.
location ~ (^|/)\. {
return 403;
}
location / {
# This is cool because no php is touched for static content
try_files $uri @rewrite;
}
location @rewrite {
# You have 2 options here
# For D7 and above:
# Clean URLs are handled in drupal_environment_initialize().
if (-f /home/alexd/www/$project/index.php) {
rewrite ^ /index.php;
}
if (!-f /home/alexd/www/$project/index.php) {
rewrite ^ /index.html;
}
# For Drupal 6 and below:
# Some modules enforce no slash (/) at the end of the URL
# Else this rewrite block wouldn't be needed (GlobalRedirect)
#rewrite ^/(.*)$ /index.php?q=$1;
}
#location ~ ^/sites/.*.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
# root /home/alexd/www/$project;
#}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_intercept_errors on;
set $sock "unix:/var/run/php/php7.1-fpm.sock";
set $phpver '7.1';
if ($http_cookie ~ 'php=5.6') {
set $sock "unix:/var/run/php5-fpm.sock";
set $phpver '5.6';
}
if ($http_cookie ~ 'php=7.0') {
set $sock "unix:/var/run/php/php7.0-fpm.sock";
set $phpver '7.0';
}
if ($http_cookie ~ 'php=7.1') {
set $sock "unix:/var/run/php/php7.1-fpm.sock";
set $phpver '7.1';
}
add_header 'X-PHP-Version' $phpver;
fastcgi_pass $sock;
fastcgi_read_timeout 150;
fastcgi_param SERVER_NAME "${original}";
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
}
# Fighting with Styles? This little gem is amazing.
location ~ ^/sites/default/files/styles/ {
log_not_found off;
try_files $uri @rewrite;
}
location ~ ^/sites/default/files/ {
log_not_found off;
#try_files $uri @rewrite;
}
location ~* \.(png|jpg|jpeg|gif|ico|woff|otf|ttf|eot|svg|txt|pdf|docx?|xlsx?)$ {
access_log off;
log_not_found off;
}
}
//
// Do any local configuration here
//
// Consider adding the 1918 zones here, if they are not used in your
// organization
//include "/etc/bind/zones.rfc1918";
zone "devel" {
type master;
file "/etc/bind/db.devel";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment