I'm looking for the recommended way of how to configure Nginx server to run Craft.
Currently, I use the following configuration, it works, but I don't know if it could be better:
server {
listen 80;
server_name example.com;
root /home/vagrant/craft/public;
index index.html index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* (?:^|/)\. {
deny all;
}
location ~* (?:\.(?:bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$ {
deny all;
}
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
try_files $uri /index.php?$query_string;
expires -1;
access_log /var/log/nginx/example.com-access.log;
}
location ~* \.(?:rss|atom)$ {
try_files $uri /index.php?$query_string;
expires 1h;
add_header Cache-Control "public";
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
try_files $uri /index.php?$query_string;
expires 1M;
add_header Cache-Control "public";
}
location ~* \.(?:css|js)$ {
try_files $uri /index.php?$query_string;
expires 1y;
add_header Cache-Control "public";
}
location ~* \.(?:ttf|ttc|otf|eot|woff)$ {
try_files $uri /index.php?$query_string;
expires 1M;
add_header Cache-Control "public";
}
}
– Trevor Davis Sep 10 '14 at 21:50location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ { add_header "Access-Control-Allow-Origin" "*"; }I get 404s on the fonts in the admin.location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ { try_files $uri /index.php?p=$uri&$args; add_header "Access-Control-Allow-Origin" "*"; }– mohd4482 Feb 21 '15 at 21:44try_filesinside of that location block means that it's going to fire up php, Yii, and Craft when serving any of those matching files, both on the backend and on the front-end. This is going to be very slow and inefficient way to serve static resources. Instead make a location block for the/adminurl to route only AdminCP requests throughindex.php– andrew.welch Mar 23 '16 at 07:16try_filesactually makes sure the file doesn't already exist on the server first. In fact, most nginx configs for/start with try_files. Your suggestion would work as well but it's going to be the same outcome: internally since the file doesn't exist, nginx is going eventually route back to thephplocation block. – RitterKnight Apr 11 '16 at 18:59locationblock you think that it is.try_filesalways looks for static files first then falls back. On the admin side, all requests DO actually get routed back to PHP. That's just how Craft 2 works since those static resources live behind the document root. I haven't looked at Craft's code but there's probably a way to alias them so nginx serves them directly. Craft 3 is going to change that... – RitterKnight Apr 25 '16 at 15:18