Changing the NGINX Document Root on Plesk 11.5

by | Oct 16, 2013 | WordPress Tweaks

I was excited about upgrading a server to the new Plesk 11.5 Panel, which has implemented a lot of great new features, including improved support for nginx. NGINX can be used in Plesk 11.5 simply by starting the “Reverse Proxy Server (nginx)” service under Tools & Settings > Services.

However, I discovered that any sites that had a manually-rewritten DocumentRoot directive in the vhost.conf file had a problem. Nginx would be unable to locate any static files (e.g. CSS, JavaScript, images) on these sites and so it would generate “404 Not Found” errors. This is because nginx has it’s own configuration file and does not follow the Apache directives in vhost.conf.

Rewriting the DocumentRoot in vhost.conf so that it points to a different virtual host can be a great way to support multiple SSL certificates for WordPress multisite domains. But how do we get this to work with nginx?

Adjusting nginx Root in Plesk Panel Doesn’t Work

My first thought was to simply add the nginx “root” directive to the “Additional nginx directives” in the Plesk Control Panel under Websites & Domains > [domain_name] > Web Server Settings.

However, this approach generated an “Invalid nginx configuration error” because the “root” was defined in a “duplicate location.” So the nginx document root is automatically set by Plesk and cannot be redefined either in the Panel or by directly modifying the vhost_nginx.conf file in the host’s conf directory.

Error

Thankfully, Plesk allows us to change the default nginx configuration (including the root directive) by creating a custom configuration template. This is explained in their Advanced Administration Guide.

1. Create a Custom Configuration Template

  1. Create your custom domain templates folder at /usr/local/psa/admin/conf/templates/custom/domain/ (if there is no such folder yet).
  2. Copy and paste the nginxDomainVirtualHost.php template from the default template folder (/usr/local/psa/admin/conf/templates/default/domain/) to the custom domain template folder (/usr/local/psa/admin/conf/templates/custom/domain/)

2. Redefine the root in the Custom Configuration Template

Now I could edit the nginx custom template (/usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php ) to run some code to first check to see if the vhost.conf or vhost_ssl.conf contains a DocumentRoot directive and use that for the root.

So I replaced this line:

root “<?php echo $OPT[‘ssl’] ? $VAR->domain->physicalHosting->httpsDir : $VAR->domain->physicalHosting->httpDir ?>”;

With this code:

3. Redefine the alias in the Custom Configuration Template

We must also redefine the internal nginx static location’s alias in our custom nginxDomainVirtualHost.php template.

Because my $documentRoot variable is already set up, I can simply replace this code:

location /internal-nginx-static-location/ {
    alias <?php echo $OPT[‘documentRoot’]; ?>/;
    add_header X-Powered-By PleskLin;
    internal;
}

With this code:

After saving and closing the file, I ran php –l nginxDomainVirtualHost.php to make sure that my syntax was correct.

4. Generate the New Configuration Files

My final step was to rebuild the Plesk configuration files by running httpdmng:

/usr/local/psa/admin/bin/httpdmng ––reconfigure-all

Then I simply restarted the nginx service and nginx started serving files correctly!

(By the way, you can always run tail –f /var/log/nginx/error.log to make sure that you’re not getting any nginx error messages in the log)

7 Comments

  1. Michael Bryne

    Thanks very much for this, got me out of trouble with my capistrano / plesk setup

    Reply
  2. Alexei Yuzhakov

    What is the reason to change doc root by hacks? Plesk has an official way to change it in UI. See Websites & Domains > Domain > Hosting settings > Document root input field.

    Reply
  3. Alexei Yuzhakov

    Why not to use add-on domains inside the same subscription? It will be possible to define docroot and no need to disable open_basedir.

    Reply
  4. Christian

    The right file to copy is now: nginxDomainVhost.php instead nginxDomainVirtualHost.php

    Reply
  5. Georg

    Great! Also works with Plesk 12!

    Reply
  6. Rostyslav

    Thank you!
    Pleasу promt me should add root “/var/www/vhosts/magento/”; or /var/www/vhosts/magento/ ?

    Reply

Trackbacks/Pingbacks

  1. Multiple SSL Certificates for Wordpress Multisite Domains in Plesk 11 - […] UPDATE: If you are using nginx in Plesk 11.5, you will also need to specify the DocumentRoot for nginx.…

Submit a Comment

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.