0
votes

I know there are many such questions on stack exchange. But nothing could help to the scenario that I have.

Here is my situation.

I have a webserver running on apache2 listening to the port numbers 7080 and 7081. I have used reverse-proxy method on my server and installed nginx which is listening to the port 80. So now nginx is the front end. I have my wordpress website running on http://www.example.com.

Now I am trying to install node.js app on my server which I could not. It makes sense because port 80 is being used by nginx.

I referred to the following posts on SO

Node.js + Nginx - What now?

Apache and Node.js on the Same Server

I tried the following

    upstream example.com/my-app {
    server 1**.*.**.**:3010;
}

# the nginx server instance
server {
    listen 1**.*.**.**:80;
        server_name example.com/my-app;
    server_name www.example.com/my-app;
    server_name ipv4.example.com/my-app;


    access_log off;

    # pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
    location / {
      proxy_set_header Host             $host;
      proxy_set_header X-Real-IP        $remote_addr;
      proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_set_header X-Accel-Internal /internal-nginx-static-location;



      proxy_pass http://example.com/my-app;
      proxy_redirect off;
    }
location /internal-nginx-static-location/ {
        alias      /var/www/vhosts/example.com/httpdocs/node;
        access_log /var/www/vhosts/example.com/httpdocs/node/statistics/logs/proxy_access_ssl_log;
        add_header X-Powered-By PleskLin;
        internal;
    }
 }

I wrote the above conf in a file and included it in /etc/nginx/conf.d/xzzeaweae_nginx.conf.

It is not working. but the app is running properly on 1++.+.++.++:3010 though.

My directory structure.

/var/www/vhosts/example.com/httpdocs/

my wordpress website root directory : /var/www/vhosts/example.com/httpdocs/

my nodejs app directory: /var/www/vhosts/example.com/httpdocs/my-nodejsapp-folder/

UPDATE

Here is my reverse proxy config for my apache application

server {
    listen +++.+.++.++:80 ;
    listen ++.+.+++.++:80 ;

    location / {
        proxy_pass http://127.0.0.1:7080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Since I have more than one website running on my server,

I have reverse proxy config for every website.

Here it is for one of my website

server {
    listen +++.+.++.++:443 ssl;

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

    ssl_certificate             /opt/psa/var/certificates/certaqnxHd2;
    ssl_certificate_key         /opt/psa/var/certificates/certaqnxHd2;
    ssl_session_timeout         5m;

    ssl_protocols               SSLv2 SSLv3 TLSv1;
    ssl_ciphers                 HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    client_max_body_size 128m;

    location / { # IPv6 isn't supported in proxy_pass yet.
        proxy_pass https://+++.+.++.++: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;
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
        access_log off;
    }

    location /internal-nginx-static-location/ {
        alias      /var/www/vhosts/example.com/httpdocs/;
        access_log /var/www/vhosts/example.com/statistics/logs/proxy_access_ssl_log;
        add_header X-Powered-By PleskLin;
        internal;
    }
}

server {
    listen +++.+.++.++:443 ssl;
    server_name webmail.example.com;

    ssl_certificate             /opt/psa/var/certificates/certaqnxHd2;
    ssl_certificate_key         /opt/psa/var/certificates/certaqnxHd2;
    ssl_session_timeout         5m;

    ssl_protocols               SSLv2 SSLv3 TLSv1;
    ssl_ciphers                 HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;
    client_max_body_size 128m;

    location / { # IPv6 isn't supported in proxy_pass yet.
        proxy_pass https://+++.+.++.++: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 /var/www/vhosts/example.com/statistics/logs/webmail_access_ssl_log;
    }
}



server {
    listen +++.+.++.++:80;

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


    client_max_body_size 128m;

    location / { # IPv6 isn't supported in proxy_pass yet.
        proxy_pass http://+++.+.++.++:7080;

        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Accel-Internal /internal-nginx-static-location;
        access_log off;
    }

    location /internal-nginx-static-location/ {
        alias      /var/www/vhosts/example.com/httpdocs/;
        access_log /var/www/vhosts/example.com/statistics/logs/proxy_access_log;
        add_header X-Powered-By PleskLin;
        internal;
    }
}

server {
    listen +++.+.++.++:80;
    server_name webmail.example.com;

    client_max_body_size 128m;

    location / { # IPv6 isn't supported in proxy_pass yet.
        proxy_pass http://+++.+.++.++:7080;

        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 /var/www/vhosts/example.com/statistics/logs/webmail_access_log;
    }
}

Note: sites-available and sites-enabled files are present inside apache2. Not in nginx.

I want my nodejs app to run on example.com/my-nodejsapp-folder/ without any port number.

Any help would be highly appreciated.

2
Why not just treat it in the same way as your Apache installation? I.E., let it listen on some free port port and proxy_pass requests to it from Nginx. - Dayo
How Do I do that? anything you can help ? - Venkateshwaran Selvaraj
Where is the config for the reverse proxy for your Apache applications? Can you include that in the question? - arco444
@arco444 I have updated the question. Please check it. - Venkateshwaran Selvaraj
@arco444 where did all the conversation go? - Venkateshwaran Selvaraj

2 Answers

0
votes

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

I haven't seen where it says you can use dots and slashes in the upstream name

upstream mynodeapp {
    server 1**.*.**.**:3010;
}

then

server {
    listen 1**.*.**.**:80;
    server_name example.com/my-app;

    #...etc.

    location / {
      proxy_set_header Host             $host;
      proxy_set_header X-Real-IP        $remote_addr;
      proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;

      # not this.
      # proxy_set_header X-Accel-Internal /internal-nginx-static-location;

      proxy_pass http://mynodeapp/my-app;
      proxy_redirect off;
    }
 }

Then your node app needs to write a header containing:

X-Accel-Redirect: /internal-nginx-static-location/somefile

There are restrictions, as in, it may not work if you start returning content (e.g. print statements) before returning all headers. It's simpler to first test with only the interesting header.


Example:

# /etc/nginx/conf.d/default.conf
upstream mynodeapp {
    server 127.0.0.1:8000;
}

server {
    listen 127.0.0.1:80;

    location /secret {
        alias /tmp/secret;
        internal;
    }

    location /my-app {
        proxy_pass http://mynodeapp/my-app;
    }
}

And let's try the following:

// /tmp/index.js
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'X-Accel-Redirect': '/secret/foo'});
  res.end('Hello World\n');
}).listen(8000, '127.0.0.1');

And now the command line:

[root@localhost secret]# pwd
/tmp/secret
[root@localhost secret]# echo bar > foo 
[root@localhost secret]# curl http://127.0.0.1:80/my-app
bar
[root@localhost secret]# curl http://127.0.0.1:80/secret/foo
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.0.15</center>
</body>
</html>
[root@localhost secret]#