Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
how to configure nginx with craft (also multilingual)
<?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(
/**
* You only need this part if you are configuring multilingual craft cms website, otherwise just ignore 'siteUrl' array
* You dont need this 'siteUrl' array for single language craft cms website
**/
'siteUrl' => array(
'en' => '<YOUR-WEBSITE-URL>/', //Please replace this <YOUR-WEBSITE-URL> with your website domain or hosting address
'de' => '<YOUR-WEBSITE-URL>/de/', //Please replace this <YOUR-WEBSITE-URL> with your website domain or hosting address
),
'usePathInfo' => true,
'omitScriptNameInUrls' => true
);
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php index.html index.htm;
server_name localhost;
###############################
# MOD REWRITE BLOCK START #
###############################
## Use Block 1 if you are working with multilingual craft cms.
## For example, for me it was en: http://localhost/ and for de: http://localhost/de
## For single language craft cms website, use Block 2
### BLOCK 1 START ###
location / {
try_files $uri $uri/ @rewrites;
}
location @rewrites {
rewrite ^(/en_gb|de|fr|es)?/(.*)$ $1/index.php?p=$2&$args? last;
}
### BLOCK 1 END ###
### BLOCK 2 START ###
location / {
try_files $uri $uri/ @rewrites;
}
location @rewrites {
rewrite ^(.*) /index.php?p=$1 last;
}
### BLOCK 2 END ###
###############################
# MOD REWRITE BLOCK ENDS #
###############################
error_page 404 /404.html;error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
@williamgomes

This comment has been minimized.

Copy link
Owner Author

@williamgomes williamgomes commented Aug 18, 2016

After searching on google for hours and going through several articles and other gist, i came up with above solution. Its easy to setup single language craft cms in nginx but its complicated with multilingual craft. You have to change your mod rewrite portion of nginx default config. I will also add configuration for virtual host in nginx for multi language.

@a-parser

This comment has been minimized.

Copy link

@a-parser a-parser commented Oct 26, 2017

thanks for sharing, I tried get working craft 3 with multi language for a couple of days and ended up with this solution

@eivindml

This comment has been minimized.

Copy link

@eivindml eivindml commented Feb 26, 2018

Thanks for this! Finally a solution which let's me remove omitScriptNameInUrls: false.

@andreasbecker

This comment has been minimized.

Copy link

@andreasbecker andreasbecker commented Aug 30, 2018

Works great, thank you. But the rewrite adds the locale to the action urls as well. So the link in the account activation email is wrong. How can that be prevented?

@a-am

This comment has been minimized.

Copy link

@a-am a-am commented Sep 3, 2018

We have run into the same issue with activation emails sending from locales and urls don't resolve. We are unsure how to prevent.

@williamgomes

This comment has been minimized.

Copy link
Owner Author

@williamgomes williamgomes commented Sep 16, 2018

Thank you very much for your support. Basically, i fall into some issues by myself and was able to found a solution after so many experiments. So thought it would help others if i share my findings. Feel to fork or improve it. 👍

@williamgomes

This comment has been minimized.

Copy link
Owner Author

@williamgomes williamgomes commented Sep 16, 2018

Works great, thank you. But the rewrite adds the locale to the action urls as well. So the link in the account activation email is wrong. How can that be prevented?

Please check my update. I put my own host in the URL, my bad, i believe that was the problem of wrong activation email. Please replace '' part with your domain name or hosting address. Believe that will solve the issue.

We have run into the same issue with activation emails sending from locales and urls don't resolve. We are unsure how to prevent.

Please check my update. I put my own host in the URL, my bad, i believe that was the problem of wrong activation email. Please replace '' part with your domain name or hosting address. Believe that will solve the issue.

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