Skip to content

Instantly share code, notes, and snippets.

@rask
Last active March 22, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rask/84fccd72fe07a4c1dbff79bd6f753dd0 to your computer and use it in GitHub Desktop.
Save rask/84fccd72fe07a4c1dbff79bd6f753dd0 to your computer and use it in GitHub Desktop.
Modular Nginx configuration for PHP

Modular PHP serving with Nginx

  • Simple maps for vhost locations and PHP versions
  • Still allows custom vhosts when added manually inside conf.d/

WARNING: I use this setup for local development, it is not meant to be used in production as is.

# conf.d/default.conf
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name ~.;
include php_host.conf;
}
user www-data www-data;
pid /var/run/nginx.pid;
http {
include mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
include php_map.conf;
include conf.d/*.conf;
}
charset utf-8;
index index.php index.html index.htm;
root $vhost_root;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_pass $php_backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
upstream php71 {
server unix:/run/php/php7.1-fpm.sock;
}
upstream php72 {
server unix:/run/php/php7.2-fpm.sock;
}
map $http_host $php_backend {
hostnames;
*.appl "php72";
*.deve "php71";
default "php72";
}
map $http_host $vhost_root {
hostnames;
*.deve /var/www/$http_host/public_html;
*.appl /var/www/$http_host/public;
default /var/www/$http_host;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment