【问题标题】:Install Nodejs app on server running on apache which uses reverse proxy with nginx在 apache 上运行的服务器上安装 Nodejs 应用程序,该服务器使用带有 nginx 的反向代理
【发布时间】:2014-12-13 10:47:29
【问题描述】:

我知道堆栈交换有很多这样的问题。但没有什么可以帮助我遇到的情况。

这是我的情况。

我有一个运行在apache2 上的网络服务器,监听端口号70807081。我在我的服务器上使用了反向代理方法并安装了nginx,它正在监听端口80。所以现在nginx是前端。我的 wordpress 网站在 http://www.example.com 上运行。

现在我正在尝试在我的服务器上安装 node.js 应用程序,但我无法安装。这是有道理的,因为nginx 正在使用端口 80。

我在 SO 上提到了以下帖子

Node.js + Nginx - What now?

Apache and Node.js on the Same Server

我尝试了以下

    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;
    }
 }

我把上面的 conf 写在一个文件中,并包含在 /etc/nginx/conf.d/xzzeaweae_nginx.conf 中。

它不工作。但是该应用程序在 1++.+.++.++:3010 上正常运行。

我的目录结构。

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

我的 wordpress 网站根目录:/var/www/vhosts/example.com/httpdocs/

我的 nodejs 应用程序目录:/var/www/vhosts/example.com/httpdocs/my-nodejsapp-folder/

更新

这是我的 apache 应用程序的反向代理配置

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;
    }
}

由于我的服务器上运行着多个网站,

我为每个网站都有反向代理配置。

这是我的一个网站

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;
    }
}

注意sites-availablesites-enabled 文件存在于 apache2 中。不在nginx

我希望我的 nodejs 应用程序在 example.com/my-nodejsapp-folder/ 上运行而无需任何端口号。

任何帮助将不胜感激。

【问题讨论】:

  • 为什么不把它和你的 Apache 安装一样对待呢?即,让它监听某个空闲端口端口和来自 Nginx 的 proxy_pass 请求。
  • 我该怎么做?有什么可以帮忙的吗?
  • 您的 Apache 应用程序的反向代理配置在哪里?你能把它包括在问题中吗?
  • @arco444 我已经更新了这个问题。请检查一下。
  • @arco444 对话都去哪儿了?

标签: node.js apache nginx proxy plesk


【解决方案1】:

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

我还没有看到它说你可以在上游名称中使用点和斜杠

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

然后

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;
    }
 }

那么你的node应用需要写一个header包含:

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

有一些限制,例如,如果您在返回所有标题之前开始返回内容(例如打印语句),它可能不起作用。仅使用有趣的标头进行首次测试会更简单。


示例:

# /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;
    }
}

让我们尝试以下方法:

// /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');

现在是命令行:

[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]# 

【讨论】:

    【解决方案2】:

    【讨论】:

    • 你有和我一样的场景吗?
    • 它看起来和你的很相似。我的 Node.js 应用程序在端口 3000 上运行,但从端口 80 提供服务。此外,它使用 websockets。有几个例外,比如我的应用程序没有通过 SSL 运行,它可以从 http 根目录运行,但您可以使用 location 指令更改它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2020-09-07
    • 2020-06-29
    • 1970-01-01
    相关资源
    最近更新 更多