An Nginx virtual host configuration for Craft CMS that implements a number of best-practices.
The Nginx-Craft configuration handles:
PATH_INFO
properly via php-fpm -> PHPHTTP_HOST
to mitigate HTTP_HOST Security Issues.env
filesThe following are assumptions made in this configuration:
'omitScriptNameInUrls' => true,
in your craft/general.php
If any of these assumptions are invalid, make the appropriate changes.
Note: We disable TLSv1.0 because it is insecure, but IE 8, 9 & 10 need to have support for TLSv1.1 manually enabled or they will not be able to connect.
This Nginx configuration comes in two parts:
sites-available/somedomain.com.conf
- an Nginx virtual host configuration file tailored for Craft CMS; it will require some minor customization for your domainnginx-partials
- some Nginx configuration partials used by all of the virtual hosts, logically segregated. These don't need to be changed, but can be selectively disabled by changing the suffix to .off
(or anything other than .conf
)dhparam.pem
via sudo openssl dhparam -out /etc/nginx/dhparams.pem 2048
mkdir /etc/nginx/ssl; sudo wget -O /etc/nginx/ssl/lets-encrypt-x3-cross-signed.pem "https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem"
nginx-partials
folder to /etc/nginx/
somedomain.com.conf
file to yourdomain.com.conf
yourdomain.com.conf
to change SOMEDOMAIN
-> yourdomain
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
line to reflect whatever version of PHP you're runningsudo nginx -s reload
If you're using Forge, it takes care of a number of these things for you, but still needs tuning.
The same applies for CloudWays, ServerPilot, Homestead, MAMP, etc.
A Forge Template is provided in forge-templates/NginxTemplate.conf
that you can use to automate setting up your Forge servers.
For this to work, you must clone the repo into /home/forge
via:
git clone https://github.com/nystudio107/nginx-craft.git /home/forge
For further information on TLS optimization, see the How to properly configure your nginx for TLS article.
N.B.: Forge now has opcache
functionality baked-in, you can enable it via the Server settings, so this information is largely deprecated.
If you're using Forge, understand that opcache
is off by default. To enable it, go to your server in Forge, click on Edit Files and choose Edit PHP FPM Configuration and search on opcache
. Here are the defaults I use; tweak them to suit your needs:
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
; Determines if Zend OPCache is enabled for the CLI version of PHP
;opcache.enable_cli=0
; The OPcache shared memory storage size.
opcache.memory_consumption=256
; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=16
; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 100000 are allowed.
opcache.max_accelerated_files=8000
; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code.
opcache.save_comments=0
More about tweaking opcache
can be found in the Fine-Tune Your Opcache Configuration to Avoid Caching Suprises article. The Best Zend OpCache Settings/Tuning/Config article is very useful as well.
While all of the configuration in the somedomain.com.conf
will work fine in local development as well, some people might want a simpler setup for local development.
There is a basic_localdev.com.conf
that you can use for a basic Nginx configuration that will work with Craft without any of the bells, whistles, or optimizations found in the somedomain.com.conf
.
While this is suitable for getting up and running quickly for local development, do not use it in production. There are a number of performance optimizations missing from it.
Brought to you by nystudio107