Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plesk 12.5 Nginx proxy pass SSL to Node app on different port

Tags:

nginx

plesk

Preface: tried installing JXCore's Node Extension for Plesk, and I can't get it to work. Gives me some error, and their support is not answering emails......

[IP ADDRESS] = real IP, example.com = real domain

ANYWAY, I have my nodejs app up and running with an SSL Cert. Works fine if I go to https://example.com:3000, which is the port I'm using for Node. SSL cert loads up all green, and everything works great.

I'm using Plesk 12.5, and Nginx comes installed already. The .conf file is located at /var/www/vhosts/[domain]/conf/nginx.conf.

At the top it says:

#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.

And they aren't kidding. Making any changes to this file get's overwrote almost immediately.

Here is the entire file contents:

server {
        listen [IP ADDRESS]:443 ssl;

    server_name example.com;
    server_name www.example.com;
    server_name ipv4.example.com;

    ssl_certificate             /usr/local/psa/var/certificates/cert-HREdQ9;
    ssl_certificate_key         /usr/local/psa/var/certificates/cert-HREdQ9;
    ssl_client_certificate      /usr/local/psa/var/certificates/cert-wpX6q1;

    client_max_body_size 128m;

    root "/var/www/vhosts/msgable.com/httpdocs";
    access_log "/var/www/vhosts/system/example.com/logs/proxy_access_ssl_log";
    error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";

    location / {
            proxy_pass https://[IP ADDRESS]:7081;
            proxy_set_header Host             $host;
            proxy_set_header X-Real-IP        $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            access_log off;
    }

    location @fallback {
            proxy_pass https://[IP ADDRESS]:7081;
            proxy_set_header Host             $host;
            proxy_set_header X-Real-IP        $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            access_log off;
    }

    location ~ ^/plesk-stat/ {
            proxy_pass https://[IP ADDRESS]:7081;
            proxy_set_header Host             $host;
            proxy_set_header X-Real-IP        $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            access_log off;
    }
    location ~ ^/(.*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|eot|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|svg|swf|tar|tgz|ttf|txt|wav|woff|woff2|xls|xlsx|zip))$ {
            try_files $uri @fallback;
    }

    location ~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon) {
            proxy_pass https://74.208.65.63:7081;
            proxy_set_header Host             $host;
            proxy_set_header X-Real-IP        $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            access_log off;
    }

    location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
            alias /var/www/vhosts/example.com/web_users/$1/$2;
            fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
            include /etc/nginx/fastcgi.conf;
    }

    location ~ ^/~(.+?)(/.*)?$ {
            proxy_pass https://[IP ADDRESS]:7081;
            proxy_set_header Host             $host;
            proxy_set_header X-Real-IP        $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            access_log off;
    }

    location ~ \.php(/.*)?$ {
            fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
            include /etc/nginx/fastcgi.conf;
    }

    location ~ /$ {
            index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
    }

    include "/var/www/vhosts/system/example.com/conf/vhost_nginx.conf";
}

I won't include the HTTP (non-ssl) stuff, because it's the same info. Only difference being that the port is 80, and the proxy_pass is set to 7080.

You'll notice at the bottom, that it has:

include "/var/www/vhosts/system/example.com/conf/vhost_nginx.conf";

This is what I put into that file:

location / {
    proxy_pass https://[IP ADDRESS]:3000;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
    access_log off;

}

Putting that in there gives me a:

nginx: [emerg] duplicate location "/"

I also tried using Plesk's "Additional nginx directives" in the settings, but I get the same error:

nginx: [emerg] duplicate location "/" Any ideas? I'm about to dump Plesk and look into AWS or something.... I've come to the conclusion that, a) you can NOT overwrite Nginx confs created by Plesk, b) you can NOT change the conf file directly.

So basically proxy_pass with Plesk is impossible.

Edit:

Also tried doing it the Upstream way, but adding Upstream directives to the admin interface through Plesk, or through vhost_nginx.conf gives me an error saying "nginx: [emerg] directive Upstream is not allowed here".

Edit#2: Read this post Nginx to address Nodejs app, adding what was explained in here does nothing. Starting to think this isn't possible using Plesk.

ARG! Such a simple task on a server without Plesk!

like image 878
David Avatar asked Apr 30 '16 14:04

David


1 Answers

After two days of mucking about, this is what solved it for me. In Plesk, you go to the "Additional nginx directives" inside the "Hosting Options" page.

This is what I added to make Nginx proxy_pass to my node app. Didn't have to edit or change any .conf files at all.

## Set the location routing.
location ~ / {

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    ##Use the domain.tld here.
    proxy_pass https://example.com:3000;
}
like image 55
David Avatar answered Sep 21 '22 16:09

David