Setting Up Expression Engine Under NGINX

I am a big fan of Expression Engine for standard CMS style needs, the only issue is that the .htaccess for mod_rewrite will obviously only work on a Apache server, so when hosting on an NGINX box, I normally use this config.

You can add the server{} block inside your http{} block but I included it for clarity.

Please remember to change the example.bonbon.io in the file to reflect your server_name and folder structure

To check the config before you restart, run this command, please do this and make sure the syntax is OK before you restart or the webserver will not like it at all!

$ sudo /usr/sbin/nginx -t

That will inform you of any syntax errors, before you restart with:

$ sudo /usr/sbin/nginx -s reload
http {
  server {
      listen 80;
      server_name example.bonbon.io;

      access_log /var/log/nginx/example.bonbon.io-access.log;
      error_log  /var/log/nginx/example.bonbon.io-error.log info;

      root /var/www/example.bonbon.io;

      location = /favicon.ico {
        log_not_found off;
        access_log off;
      }

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

      # Deny all attempts to access hidden files
      location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
      }
      
      location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$args;
        add_header 'Access-Control-Allow-Origin' '*';
      }

      location ~*  \.(jpg|jpeg|png|gif|css|js|ico)$ {
        expires max;
        log_not_found off;
      }

      location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
         fastcgi_pass unix:/tmp/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
  }
}

Please feel free to suggestions or improvements, but this works great for my own needs, and with the volume of suggestion configs out there, I wanted to share the one I use.