Jump to content

Nginx Configuration And Url Rewriting

Recommended Posts

I recently switched some customer domains from php-cgi (apache served) to php-fpm (nginx served with apache as reverse proxy using the plesk-php56 package).
I use a plesk panel on the server that uses apache as a reverse proxy if the requests are served by nginx.
Everything went flawlessly and I had a great boost of performance - but I have an issue with url rewriting that is driving me crazy.
If the domain is configured to have multiple languages the starting URL would be something like :
In this condition Nginx returns a 404 not found error when opening the page.
Of course with an apache-only configuration the url opens correctly and returns the homepage.
I can confirm that even with php-fpm, the native apache prestashop rewrite rules in ".htaccess" are working properly once the web request arrive to apache (because of the reverse proxy configuration)... but this particular request seems to be processed by nginx directly ...
The problems seems to be the trailing slash, in fact if I type the URL with "/en" - the site opens correctly.
It looks like nginx search for index file, not being able to find it.
Those are the solution I tried without success :
1 - Using an .htaccess to nginx converter and apply the rule (same problem)
2 - Using a nginx rewrite rule ( rewrite ^/(.*)/$ /$1 permanent; ) in nginx.conf file to force redirecting slash requests to non-slash requests but this lead me to other redirection-loop url troubles.


any idea for a proper configuration ?
Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

Hi mdekler,


sorry for not respnding again. I am on Gmail and whatever I do, or the support gives as a soloution many of my forum mails fall in the spam folder. So sometimes I miss that I have got response to a posting.


Now by luck because I am still/again struggling with a nginx presta config problem I came back here over google. :)


Actually my shops seems to work quite ok, with only 2 exceptions:


- Customer can not logg out

- Compare list is empty after calling the compare site


This happens only with activated SEO-Urls & Multilingual Shop. If one of these parameters is turned to "off" the problem is solved. My Server is Nginx 1.8 and the config file I have attached containes only the snippet from the Prestashop documentation

location / {
  index /index.php;
  rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
  rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
  rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.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.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.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.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.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.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.jpg last;
  rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
  rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1.jpg last;
  rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
  try_files $uri $uri/ /index.php?$args;

Nothing more.

Link to comment
Share on other sites

  • 5 years later...
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...