Skip to content

Instantly share code, notes, and snippets.

@williamgomes
Last active June 30, 2023 12:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamgomes/5749c8f54936823af15c9f1a45713861 to your computer and use it in GitHub Desktop.
Save williamgomes/5749c8f54936823af15c9f1a45713861 to your computer and use it in GitHub Desktop.
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;
}
}
@a-parser
Copy link

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

@eivindml
Copy link

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

@andreasbecker
Copy link

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
Copy link

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
Copy link
Author

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
Copy link
Author

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