Prestashop 1.6 + Nginx + SSL + Multilingual Site - Rewrite Rules - ssl

I am struggling with setting up rewrite rules for prestashop with the following setup
Prestashop 1.6
SSL
Multiligual Site (fr + en in my case)
Nginx 1.4.1 (that is important for ssl settings I found)
I have used some of the references below
http://www.prestashop.com/forums/topic/323391-another-nginx-ssl-rewrite-rules-problem/
http://www.prestashop.com/forums/topic/321261-seo-friendly-nginx-rewrites/ (not multilingual)
PRESTASHOP NGINX + REWRITE RULES
and got to this config below.
Everything works except some of my payment modules which return a url like
mysite.com/en/index.php?parameter1=1&parameter2=2
This triggers a 404
It looks like the url should be rewritten as
mysite.com/index.php?parameter1=1&parameter2=2
I have two questions:
Which rule is handling urls like mysite.com/en/16-crews which are working fine?
and what is it translated to ? (I am just curious to understand how that works)
How do I set up a rule to rewrite
mysite.com/en/index.php?parameter1=1&parameter2=2
to
mysite.com/index.php?parameter1=1&parameter2=2
It must also work with the french side of the site /fr/index.php to /index.php
server {
listen 80;
listen 443 ssl;
server_name mysite.com www.mysite.com;
ssl on;
ssl_certificate /etc/nginx/ssl/mysite.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.key;
access_log /var/log/nginx/mysite.access.log;
error_log /var/log/nginx/mysite.error.log;
rewrite_log on;
location / {
root /srv/d_h2osensations/www/www.mysite.com/htdocs;
index index.html index.htm index.php;
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ - img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?/[_a-zA-Z0-9-]*.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)/[a-zA-Z0-9-]+.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /srv/d_h2osensations/www/www.mysite.com/htdocs$fastcgi_script_name;
}
# Deny access to .htaccess
location ~ /\.ht {
deny all;
}
location /phpmyadmin
{ root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
Thanks in advance.
Nic

After a few weeks of looking around this is what works for me
server {
listen 80;
#listen [::]:80 default_server ipv6only=on;
listen 443 default ssl;
#ssl on;
ssl_certificate /etc/nginx/ssl/cert.crt;
ssl_certificate_key /etc/nginx/ssl/cert-key.key;
access_log /var/log/nginx/h2o.prod.access.log;
error_log /var/log/nginx/h2o.prod.error.log;
root /var/www/www.mysite.com/htdocs;
#root /usr/share/nginx/html;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name mysite.com www.mysite.com;
#Specify a charset
charset utf-8;
rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)/[a-zA-Z0-9-]+.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/[_a-zA-Z0-9-]*.jpg$ /img/c/$1$2.jpg last;
rewrite ^/order$ /index.php?controller=order last;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php last;
}
# Redirect needed to "hide" index.php
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
}

Related

mod rewrite from apache to nginx

I'm having some trouble in transformation of htaccess from apache to nginx.
actual htaccess rule:
options +followsymlinks
RewriteEngine On
RewriteCond $1 !^(images|system|themes|css|js|flex|slider|rss|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*) /index.php?$1 [L]
this rule set is used in expression engine 1.7
my current solution :
location / {
index index.php;
try_files $uri $uri/ #ee;
}
location #ee {
rewrite ^(.*) /index.php?/$1 last;
}
location /index.php {
include fastcgi_params;
set $script $uri;
set $path_info $uri;
if ($args ~* "^(/.+)$") {
set $path_info $1;
}
fastcgi_pass fastcgi-upstream_${server_name};
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
}
location ~* \.php$ {
include fastcgi_params;
fastcgi_pass fastcgi-upstream_${server_name};
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
but it not reflexting 100% the same thing as in htaccess.
You can use the location block in Nginx. This will redirect all your request to index.php.
location / {
rewrite ^/(.*) /index.php?$1 break;
}

Why Nginx redirect all my https request to a specific subdomain?

After installing Owncloud on my server, on a subdomain (let's say) cloud.motherboard.fr, I have the issue that all my https requests (like https://hey.motherboard.fr) redirect to the following Owncloud page :
while going to https://hey.motherboard.fr
So I guess that my Nginx configuration redirect all https connections to Owncloud. Here is my configuration file :
upstream php-handler {
server 127.0.0.1:9000;
# server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name cloud.motherboard.fr; #YourIP or domain
return 301 https://$server_name$request_uri; # redirect all to use ssl
}
server {
listen 443 ssl;
server_name cloud.motherboard.fr; #YourIP or domain
#SSL Certificate you created
ssl_certificate /etc/nginx/cert/owncloud.crt;
ssl_certificate_key /etc/nginx/cert/owncloud.key;
# owncloud path
root /var/www/cloud/owncloud/;
client_max_body_size 10G; # set max upload size
fastcgi_buffers 64 4K;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
location / {
# The following 2 rules are only needed with webfinger
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ index.php;
}
location ~ ^(.+?\.php)(/.*)?$ {
try_files $1 = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
fastcgi_param PATH_INFO $2;
fastcgi_param HTTPS on;
fastcgi_pass php-handler;
}
# Optional: set long EXPIRES header on static assets
location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
# Optional: Don't log access to assets
access_log off;
}
}
It looks like it comes from the two first server block, but I didn't managed to change it. Can it have a link with php-pfm ?
My hey.motherboard.fr configuration is pretty simple :
server {
server_name hey.motherboard.fr;
location / {
root /var/www/hey;
index index.html index.htm;
}
}

Nginx Configuration/Rewrite

I want to configure nginx to behave in this way :
[OK] If i browse to domain.com/, the /var/www/index.php file is called
[HOW ?] If i browse to domain.com/blah, /var/www/controller.php is called
On apache, is done by a rewrite condition :
RewriteRule (.*) controller.php [L,QSA]
[HOW ?] If i browse to domain.com/api/someMethod, /var/www/api/controller.php is called
On apache, is done by a rewrite condition :
RewriteRule ^api api/controller.php [L,NC]
[HOW ?] If i browse to domain.com/image.png, nginx display the image /var/www/image.png
On apache, is done by a rewrite condition :
RewriteRule \.(js|css|gif|png|jpg|ico|txt|woff|woff2)$ - [L,NC]
And my nginx config :
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
listen [::]:443 ssl;
server_name _;
root /var/www;
index index.php;
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/private/server.key;
location ~ {
try_files $uri $uri/ /index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The significant parts of the configuration would be:
root /var/www;
index index.php;
location = / { }
location / {
try_files $uri $uri/ /controller.php;
}
location /api {
try_files $uri $uri/ /api/controller.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
The first two lines are from your original configuration.
The location = will ensure that the URI / obeys the index index.php rather than the try_files rule next.
The location / defines the default action, serving image.png, index.php on subdirectories and controller.php on anything else.
The location /api modifies this behaviour with respect to controller.php.
The location ~ \.php$ block implements the fastcgi interface. Notice that include fastcgi_params; should come before any fastcgi_param directives to avoid the latter being silently overridden.
If you would like the URI domain.com/blah.php to call controller.php instead of throwing a 404, then change the =404 to /controller.php.
Please see this for a list of nginx directives.

Webservice doens't working on fresh install

After performing a fresh install of Prestashop ( v 1.6.0.9 ) i've encountered some problems about making the webservice feature avaliable.
I have set the key and i'm able to check the ws's avaliable using the url
http://example.com/webservice/dispatcher.php?ws_key=my_key
And 3 of the relevant result's are:
http://example.com/api/employees?schema=synopsis
http://example.com/api/employees
http://example.com/api/employees?schema=blank
So, testing the links above,i receive the message This page is not available right into my face, i'm not sure why this behaviour.
For aditional information, i followed the steps on the Web Service Tutorial where i downloaded the PSWebServiceLibrary.php file into my root folder, i also created a test file with the content:
<?php
/**
* Created by PhpStorm.
* User: thales.pereira
* Date: 05/01/15
* Time: 16:10
*/
require_once( './PSWebServiceLibrary.php' );
$shop_url="http://localhost";
$secret_key="the_key";
$debug=false;
try {
$webService = new PrestaShopWebservice($shop_url, $secret_key, $debug);
$opt['resource'] = 'customers?schema=synopsis';
$xml = $webService->get($opt);
echo $xml;
}
catch (PrestaShopWebserviceException $ex) {
echo 'Other error: <br />' . $ex->getMessage();
}
But well.. the result was:
Other error:
This call to PrestaShop Web Services failed and returned an HTTP status of 404.
That means: Not Found.
For this dev environment, i'm using MAMP Version 3.0.7.3
The main problem was with nginx regex rules.
Here is what is currently working for me:
server {
listen 80;
server_name 127.0.0.1;
access_log /var/log/nginx/prestashop.access.log;
error_log /var/log/nginx/prestashop.error.log;
root /var/www/prestashop;
if ($http_host != "127.0.0.1") {
rewrite ^ http://127.0.0.1$request_uri permanent;
}
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
rewrite ^/c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2$3.jpg last;
rewrite ^/c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/images_ie/?([^/]+)\.(jpe?g|png|gif)$ /js/jquery/plugins/fancybox/images/$1.$2 last;
try_files $uri $uri/ /index.php$is_args$args;
error_page 404 /index.php?controller=404;
location ~* \.(gif)$ {
expires 2592000s;
}
location ~* \.(jpeg|jpg)$ {
expires 2592000s;
}
location ~* \.(png)$ {
expires 2592000s;
}
location ~* \.(css)$ {
expires 604800s;
}
location ~* \.(js|jsonp)$ {
expires 604800s;
}
location ~* \.(js)$ {
expires 604800s;
}
location ~* \.(ico)$ {
expires 31536000s;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}

CakePHP Htaccess 2 Nginx rewrite

We're moving a CakePHP Framework installation to a server where there's an Nginx running. Previous server had Apache. This CakePHP has multiple sub-installations on subfolders which all include the /app/webroot/ folder. We've managed to get the index.php working but all the other files located under /app/webroot/ like javascript and CSS don't link up there.
Now, we've tried getting this to work on nginx with multiple different variations. The problem is, the site loads up PHP files and clean URL'S work. Loading CSS and JS files which are located under /app/webroot/ don't.
We're trying to set up the root to subdomain.example.com where there's an index.php with a header() function to redirect the user to a folder, where there's CakePHP. Basically multiple sites under sub folders. So the CakePHP sites are http://subdomain.example.com/subfolder
Here's the nginx conf we're trying. I've been trying various different options with no effect.
server {
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com www.example.com subdomain.example.com;
access_log /home/example.com/logs/access.log;
error_log /home/example.com/logs/error.log error;
root /home/example.com/public_html/;
index index.php;
gzip_static on;
location /subfolder {
root /home/example.com/public_html/subfolder/;
index index.php;
rewrite ^/subfolder/(/.*)$ /app/webroot$1 break;
try_files $uri $uri/ /subfolder/app/webroot/index.php?$args;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_comles $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/example.com-php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
app/webroot/ will be your server root. And separate location for process index.php file.
Example:
server {
listen 80;
server_name yourserver.com;
root /web/path/;
index index.php;
location / {
rewrite ^(/.*)$ /app/webroot$1 break;
try_files $uri $uri/ /app/webroot/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}