Skip to content

Instantly share code, notes, and snippets.

@kenzie
Last active May 17, 2018 17:48
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kenzie/5811327 to your computer and use it in GitHub Desktop.
Save kenzie/5811327 to your computer and use it in GitHub Desktop.
Nginx virtual host configuration for Craft CMS, PHP5-FPM, NGINX 1.2.1 and craft/config/general.php for friendly URLs.
server {
listen 80;
root /var/www/craft.dev/public;
index index.php index.html index.htm;
server_name craft.dev;
location / {
try_files $uri $uri/ @rewrites;
}
location @rewrites {
rewrite ^(.*) /index.php?p=$1 last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
error_page 404 /index.php;
}
<?php
/**
* General Configuration
*
* All of your system's general configuration settings go in here.
* You can see a list of the default settings in craft/app/etc/config/defaults/general.php
*/
return array(
/**
* Whether generated URLs should be formatted using PATH_INFO, e.g. http://domain.com/index.php/path/,
* as opposed to using the query string, e.g. http://domain.com/index.php?p=path
*
* Possible values: true, false, 'auto'
*/
'usePathInfo' => true
);
@keithmancuso
Copy link

this worked great for me... was able to get a site fully up and running. I have another site im trying to get up but its multilingual so theres a separete index.php in the language subfolders /en/index.php /nb/index.php I had these working fine in apache mod rewrites but having a hard time wrapping my head around how to do that here?

I tried to make it do something like:

to make it

location / {
    try_files $uri $uri/@rewrites;
}

location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
    rewrite  ^(/en/.*) /en/index.php?p=$1 last;
    rewrite  ^(/nb/.*) /nb/index.php?p=$1 last;
}

but no dice? what am i doing wrong?

The root works fine, but the /en/ and /nb/ subdirectories arent loading properly, just getting the

Page Not Found

The requested URL was not found on this server.

@keithmancuso
Copy link

Hey so i was able to get this working with what feels like a messy solution, would love to find a better way to do this. Thanks again for positing this was hugely helpful in getting things running properly.

location /en/ {
   try_files $uri $uri/ @enrewrites;
  }

  location /nb/ {
   try_files $uri $uri/ @nbrewrites;
  }

  location / {
    try_files $uri $uri/ @rewrites;
  }

  location @rewrites {
    rewrite ^(.*) /index.php?p=$1 last;
  }

  location @enrewrites {
    rewrite ^/en/(.*)$ /en/index.php?p=$1? last;
  }

  location @nbrewrites {
    rewrite ^/nb/(.*)$ /nb/index.php?p=$1? last;
  }

Copy link

ghost commented Jun 25, 2014

FYI, not sure why but this nginx config wouldn't work for me without the following in the PHP location block:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

@Harry-Harrison
Copy link

I've tried everything I can think of on this, but my URLs still come out craft.dev/index.php/pageurl

Am I missing something really obvious?

EDIT:
A small bit of digging discovered the config option of 'omitScriptNameInUrls' => true makes all the difference.

@alkrauss48
Copy link

Prior to this config, I had my craft entries loading properly, but the admin wasn't loading assets from the right directories (css, js, etc.). This nginx config helped me to get it all working properly - thanks!

@jessehudl
Copy link

@keithmancuso I know your comment is years old, but here's a more efficient way to make that config:

        location / {
            try_files $uri $uri/ @rewrites;
        }
        location @rewrites {
            rewrite ^(/en_gb|de|fr|es)?/(.*)$ $1/index.php?p=$2&$args? last;
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment