JuliSmz
JuliSmz

Reputation: 1156

How can I create an nginx custom template for a specific domain on Plesk?

I know that plesk allow us to create custom Nginx and Apache templates under the folder /usr/local/psa/admin/conf/templates/custom but Plesk don't give any option to specify a custom template per domain.

https://talk.plesk.com/threads/nginx-vhost-template-for-a-specific-domain.342876/

I've a nodeJs app running over port 3001 and websockets running over port 3002, so I need to config an specific reverse proxy for those 2 cases.

If the domain is a.domain.com I need the proxy forwarding traffic to 3001 port. If the domain is b.domain.com I need the proxy forwarding traffic to 3002 port.

Anything else, the common behavior to apache ports.

There is any way to do this?

Upvotes: 1

Views: 779

Answers (1)

JuliSmz
JuliSmz

Reputation: 1156

I solved this case adding some pieces of logic onto the plesk custom template.

First, create a new folder custom on /usr/local/psa/admin/conf/templates/

mkdir -p /usr/local/psa/admin/conf/templates/custom/domain/service/

Then, create a file with the following code:

vim /usr/local/psa/admin/conf/templates/custom/domain/service/proxy.php

<?php
/**
 * @var Template_VariableAccessor $VAR
 * @var array $OPT
 */
?>
<?php
$port = $OPT['backendPort'];
if($VAR->domain->asciiName == 'a.domain.com') {
        $port =  '3001';
}
else if ($VAR->domain->asciiName == 'b.domain.com') {
        $port = '3002';
}
?>

<?php if ($OPT['ssl'] && ($port == 3002 || $port == 3001)): ?>
        proxy_pass http://<?php echo $OPT['ipAddress']->proxyEscapedAddress . ':' . $port ?>;
<?php elseif ($OPT['ssl'] && ($port != 3002 && $port != 3001)): ?>
        proxy_pass https://<?php echo $OPT['ipAddress']->proxyEscapedAddress . ':' . $port ?>;
<?php else: ?>
        proxy_pass http://<?php echo $OPT['ipAddress']->proxyEscapedAddress . ':' . $port ?>;
<?php endif ?>
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
<?php if (empty($OPT['nginxTransparentMode']) && !$VAR->domain->physicalHosting->proxySettings['nginxTransparentMode'] && !$VAR->domain->physicalHosting->proxySettings['nginxServeStatic']): ?>
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
<?php endif ?>
        access_log off;

<?php if ($OPT['nginxCacheEnabled'] ?? true): ?>
    <?=$VAR->includeTemplate('domain/service/nginxCacheProxy.php', $OPT)?>
<?php endif ?>

Now, just rest to run the reconfigure scripts:

/usr/local/psa/admin/bin/httpdmng --reconfigure-domain a.domain.com
/usr/local/psa/admin/bin/httpdmng --reconfigure-domain b.domain.com

Upvotes: 1

Related Questions