geekish
7/28/2015 - 11:03 PM

Homebrew Nginx and PHP-FPM configuration

Homebrew Nginx and PHP-FPM configuration

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_buffers 8 4k;
        fastcgi_buffer_size 4k;
    }
worker_processes  1;
 
error_log  /usr/local/etc/nginx/logs/error.log;
 
events {
    worker_connections  1024;
}
 
http {
    include mime.types;
    default_type application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log /usr/local/etc/nginx/logs/access.log main;
 
    sendfile on;
 
    keepalive_timeout 65;
 
    index index.html index.php;
 
    include /usr/local/etc/nginx/conf.d/*.conf; 
}
server {
    listen 80;

    server_name ~^(?<sname>.+?).devio$ ~^(?<sname>.+?)\.(.*)\.xip\.io;

    root /var/www/$sname/public;

    index index.php index.html;

    error_log /usr/local/etc/nginx/logs/wildcard.error.log debug;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { allow all; log_not_found off; access_log off; }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    include /usr/local/etc/nginx/inc/php-fpm.conf;
}
server {
    listen 80 default_server;

    server_name localhost;
    root /var/www/default;
    index index.html index.php;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { allow all; log_not_found off; access_log off; }
    
    location / {
        try_files $uri $uri/ /index.php?$query_string =404;
    }

    error_page 500 502 503 504 /500.php;
    error_page 404 /404.php;
    
    include /usr/local/etc/nginx/inc/php-fpm.conf;
}

This is the configuration I use for nginx + PHP on my Macs. I use dnsmasq to handle *.devio.

nginx setup:

  • configuration is in /usr/local/etc/nginx.
  • Server conf files are in subfolder conf.d.
  • Include files (php-fpm.conf) are in subfolder inc.

DNS setup:

echo 'address=/.devio/127.0.0.1' > /usr/local/etc/dnsmasq.conf
sudo brew service restart dnsmasq
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/devio'