• The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Question URL re-writing using nginx?

OlgaKM

Basic Pleskian
I have recently started using nginx with PHP-FPM to serve PHP files, and this is blazing fast. However, where there are rewritten URLs, Apache becomes a bottleneck. How can I have nginx do the rewriting?

Let's say that I'm trying the following kind of rewrite in Apache:

Code:
RewriteRule /cat/([0-9]*) /index.php?cat=$1 [NC]

The equivalent code for nginx should be something like this:

Code:
location ~ /cat/(.*) {
    return 301 /index.php?cat=$1 last;
}

But I'm not sure where I should insert this in the Plesk config.

Just to show what kind of performance difference we're talking about here, my benchmarking using ab shows that nginx with PHP-FPM can serve over 9000 reqs / sec. If it has to go through Apache's URL rewriting, that drops to 150 reqs / sec (!!)
 
A very easy-to-use solution could be to install the extension "htaccess to nginx 1.0". It converts .htaccess rewrite rules to a suitable Nginx format.

The Nginx rules go into Tab "Websites & Domains" > Icon "Apache & Nginx Settings" > Section "Additional Nginx Directives".
 
Thank you for the quick answer! This is exactly what I was looking for.

However, I have run into an issue: I used to redirect visitors to the non-www domain to the www domain with an Apache rule like this:

Code:
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [L,R=301]

The nginx equivalent would be:

Code:
server {
    listen 80;
    server_name domain.com;
    return 301 http://www.domain.com$request_uri;
}

However, Plesk refuses to save this configuration, telling me that the server directive is not allowed where I'm pasting it. Worse, the Apache rule no longer seems to work. How do I resolve this?
 
Ok, I have fixed the non-www to www issue by selecting a preferred domain under Hosting Settings. However, I am still curious: why did the Apache Rewrite directives in vhost.conf stop working?
 
The reason for that you got the "not allowed here" error is, that the directives you enter into the "additional Nginx directives" field are already placed inside a server { ... } bracket. Nesting of server enclosures is not possible (and does not make sense). Simply omit the server { ... } enclosure, instead simply write:
Code:
   server_name domain.com;
   return 301 http://www.domain.com$request_uri;

The Apache rewrite probably never stopped working, but when you change hosting settings, it takes the server update interval until the updated server configuraton is read and applied. So if you change something and do not wait until the interval expired or the webserver has been restarted manually, changes will not be active and it appears as if the .htaccess rules are not working, because the request has not reached Apache yet.
 
Back
Top