在运行在apache上的服务器上安装Nodejs应用程序,该服务器使用带有nginx的反向代理

时间:2014-10-17 19:22:11

标签: node.js apache nginx proxy plesk

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

这是我的情况。

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

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

我在SO

上提到了以下帖子

Node.js + Nginx - What now?

Apache and Node.js on the Same Server

我尝试了以下

nginx

我在文件中写了上面的conf,并将其包含在/etc/nginx/conf.d/xzzeaweae_nginx.conf中。

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

我的目录结构。

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

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

我的nodejs app目录:/var/www/vhosts/example.com/httpdocs/my-nodejsapp-folder /

更新

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

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

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

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

这是我的一个网站

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

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

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

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

然后您的节点应用程序需要编写包含以下内容的标题:

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

答案 1 :(得分:0)