• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • 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.

Nginx configuration for vhosts

akcontreras

New Pleskian
How can I customize/add a configuration for a vhost in nginx. I want to put proxy_pass for Node.js into a subdomain of a vhost. All files I look it says: DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY
 
You can modify the default template by

vim /usr/local/psa/admin/conf/templates/default/domain/domainVirtualHost.php

and then,

/usr/local/psa/admin/sbin/httpdmng --reconfigure-all
 
1.- Is with Nginx but thanks, is the right clue what I needed.
2.- That will be global, I need only 1 domain, it will be nice if I could modify just 1 domain and the changes will keep
 
I need to know how to do this on a per domain basis as well. The template helps somewhat (and I've used it for the time being), but most of the time, I want to be able to change the nginx settings on a per domain basis, not have my changes appear globally for all domains. What is the equivalent of vhost.conf (in /var/www/vhosts/domain/conf/) for nginx
 
there is a file in /var/www/vhosts/domain/conf/ that is called "last_nginx.conf" that is what you edit and then thereafter you can run:

Code:
/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain <domain_name>
 
I know you can edit that file, that will not however affect changes moving forward when running:
/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain
As the file states:
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.

As a matter of fact, when you run httpdmng --reconfigure-domain, you will actually lose the changes you just made (this is why you add a vhost.conf file, NOT edit last_httpd.include)

the file you could edit is <newest_version>_nginx.conf and then restart nginx
running /usr/local/psa/admin/sbin/httpdmng --reconfigure-domain will once again, wipe out those changes

So what is the nginx.conf (could it be nginx.conf??) file that can be added on a per domain basis, that
/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain will then pull in and include in the auto generated file.
 
It looks like the auto generated nginx.conf files on a per domain basis do NOT include any type of mechanism to include per domain additions.

This can be added however it looks like. The nginx configure files do support the ability to include other files.

What you will want to do is edit the nginx template file, located at:
/usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php

Add a line like:
include <?php echo $VAR->domain->physicalHosting->vhostDir;?>/conf/nginx.conf;

I don't have any recommendations as to where to place this, I am sure it depends if you want to add new listeners, overwrite listeners (I haven't played with nginx enough yet, but I would assume, the last definition would overwrite a previous definition), or just add new locations and proxy's.

Then add your own nginx.conf file in the /var/www/vhosts/domain/conf folder

One stumbling point: This will cause httpdmng --rebuilt-all to fail, if any domain does not have the new file. Adding a php test if file_exists will probably help solve this.
 
Last edited by a moderator:
Well you can lock the file against changes in it by:

Code:
chattr +i last_nginx.conf
 
Well you can lock the file against changes in it by:

Code:
chattr +i last_nginx.conf

Using of immutable bit on such files can brake plesk upgrade sometimes.

Here steps how to add custom include in nginx virtual host config:

mkdir /usr/local/psa/admin/conf/templates/custom/domain

cp /usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php /usr/local/psa/admin/conf/templates/custom/domain/

add in /usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php:

<?php if (file_exists($VAR->domain->physicalHosting->vhostDir . '/conf/nginx.conf')): ?>
include <?php echo $VAR->domain->physicalHosting->vhostDir;?>/conf/nginx.conf;
<?php endif ?>

/usr/local/psa/admin/bin/httpdmng --reconfigure-all # to apply new configuration for all domains

As result, if domain has conf/nginx.conf - it will be included into virtual host config.
 
Great advice by OlegN!

Though I should note that this solution will break apart during upgrade to Plesk 11.5 (where this functionality is present out of the box).

After upgrade to 11.5 you'll need to move your custom configs to conf/vhost_nginx.conf, remove custom template and call httpdmng --reconfigure-all.

Oh, and really avoid using chattr +i even if your filesystem supports it.
 
Thx. It Works!

Great Job OLEG

5 Stars

And also Nikolay!

If you wan´t to edit the file after:

chattr +i nginx.conf

then execute

chattr -i nginx.conf

* Edit the file and
chattr +i nginx.conf
again
 
Last edited:
i now tried something different, in plesk 11.5
i went to web server settings of the subdomain
and in extra nginx directives i added
location ~ /$ {
root /var/www/vhosts/domain.com/httpdocs;
}

that changed that now when going to
secure.domain.com instead of getting a 404 not found,
i get a 403 forbidden

other than that if i try
secure.domain.com/index.php i still get 404 error and same for other files
 
dear friends
so i have created a subdomain secure.domain.com and i pointed it to the main folder of domain.com,
i created custom vhost.conf for apache and that works great,
but nginx is pointing still to wrong folder, logical as i can see that the last_nginx.conf file is pointing to wrong place

so i found the thread http://forum.parallels.com/showthread.php?260814-Nginx-configuration-for-vhosts
and i followed the indications there but its not working for me,

- i have edited a file nginxDomainVirtualHost.php
on /usr/local/psa/admin/conf/templates/custom/domain
after copying from the default folder
and i added this as you indicated
<?php if (file_exists($VAR->domain->physicalHosting->vhostDir . '/conf/nginx.conf')): ?>
include <?php echo $VAR->domain->physicalHosting->vhostDir;?>/conf/nginx.conf;
<?php endif ?>

just i wasnt sure where to add it, i added it at the very end before the last }
is that correct, or if not where should i add it within that file?

and then i added an nginx.conf inside
/var/www/vhosts/system/secure.domain.com/conf

and inside i just put
root "/var/www/vhosts/domain.com/httpdocs"

is that correct, or do i have to put more directives inside this custom nginx.conf?

and then i did of course the /usr/local/psa/admin/bin/httpdmng --reconfigure-all
but nothing happened

when i go to secure.domain.com i keep getting 404 nginx errors,
however the apache is pointing to right place because the .htaccess of main folder is getting processed, so apache all good,
but all php and non php pages are failing with 404 nginx errors still

i now tried something different, in plesk 11.5
i went to web server settings of the subdomain
and in extra nginx directives i added
location ~ /$ {
root /var/www/vhosts/domain.com/httpdocs;
}

that changed that now when going to
secure.domain.com instead of getting a 404 not found,
i get a 403 forbidden

other than that if i try
secure.domain.com/index.php i still get 404 error and same for other files


thank you for any help
best
 
Sorry please i have a question.
how can i proxy pass to another server ip?
i have a domain say sub.example.com , how can proxy to another web application running on another server different from plesk?
 
Back
Top