【问题标题】:How to run nodejs application in apache server如何在 apache 服务器中运行 nodejs 应用程序
【发布时间】:2016-08-23 05:56:17
【问题描述】:

我想通过 apache 服务器上的子域运行我的 nodejs 应用程序。我在 cpanel 中创建了主域的子域。我的项目有超过 3 个子域,所有子域都指向不同的 nodejs 应用程序。子域将我重定向到正确的文件夹中,但是当我通过浏览器中的子域运行应用程序时,它不会在 apache 服务器上运行节点应用程序。它仅显示项目的文件夹列表。

我参考以下链接。

http://www.codingtricks.biz/run-nodejs-application-apache/

Running Node.js in apache?

项目目录:

/home/abc/public_html/node

我的主域名是:

https://www.example.com

我在 cpanel 中创建了子域:

https://www.node.example.com

我在浏览器中访问子域:

node.example.com

我在 softlayer 服务器上部署了我的应用程序。

以下是apache配置:

文件:

/usr/local/apache/conf/includes/abc_node.conf

配置:

<VirtualHost 132.159.25.21:80>
   ServerAdmin info@example.com
   ServerName node.example.com
   ServerAlias www.node.example.com

   DocumentRoot /home/abc/public_html
   <Directory /home/abc/public_html/node>
      Options -Indexes +FollowSymLinks
      AllowOverride None
      Require all granted
   </Directory>

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>

   <Location /node>
      ProxyPass http://132.159.25.21:8080
      ProxyPassReverse http://132.159.25.21:8080
   </Location>

   ErrorLog /home/abc/public_html/node/error.log

   # Possible values include: debug, info, notice, warn, error, crit,
   # alert, emerg.
   LogLevel warn

   CustomLog /home/abc/public_html/node/access.log combined

</VirtualHost>

命令摘要:

命令:

/usr/local/apache/bin/httpd -tD DUMP_VHOSTS

输出:

AH00548: NameVirtualHost has no effect and will be removed in the next release /usr/local/apache/conf/httpd.conf:445
VirtualHost configuration:
127.0.0.1:80           wb01-development.example.com (/usr/local/apache/conf/httpd.conf:504)
127.0.0.1:443          wb01-development.example.com (/usr/local/apache/conf/httpd.conf:554)
132.159.25.21:443      is a NameVirtualHost
         default server example.com (/usr/local/apache/conf/httpd.conf:368)
         port 443 namevhost example.com (/usr/local/apache/conf/httpd.conf:368)
                 alias www.example.com
         port 443 namevhost wb01-development.example.com (/usr/local/apache/conf/httpd.conf:554)
                 wild alias cpanel.*
                 wild alias whm.*
                 wild alias webmail.*
                 wild alias webdisk.*
                 wild alias cpcalendars.*
                 wild alias cpcontacts.*
132.159.25.21:80       is a NameVirtualHost
         default server 132.159.25.21 (/usr/local/apache/conf/httpd.conf:274)
         port 80 namevhost 132.159.25.21 (/usr/local/apache/conf/httpd.conf:274)
         port 80 namevhost example.com (/usr/local/apache/conf/httpd.conf:297)
                 alias www.example.com
         port 80 namevhost node.example.com (/usr/local/apache/conf/httpd.conf:446)
                 alias www.node.example.com
         port 80 namevhost wb01-development.example.com (/usr/local/apache/conf/httpd.conf:504)
                 wild alias cpanel.*
                 wild alias whm.*
                 wild alias webmail.*
                 wild alias webdisk.*
                 wild alias cpcalendars.*
                 wild alias cpcontacts.*
         port 80 namevhost node.example.com (/usr/local/apache/conf/includes/abc_node.conf:1)
                 alias www.node.example.com
*:*                    wb01-development.example.com (/usr/local/apache/conf/httpd.conf:286)

命令

/usr/local/apache/bin/httpd -tD DUMP_MODULES

输出:

AH00548: NameVirtualHost has no effect and will be removed in the next release /usr/local/apache/conf/httpd.conf:445
Loaded Modules:
    core_module (static)
    authn_file_module (static)
    authn_core_module (static)
    authz_host_module (static)
    authz_groupfile_module (static)
    authz_user_module (static)
    authz_core_module (static)
    access_compat_module (static)
    auth_basic_module (static)
    socache_shmcb_module (static)
    socache_dbm_module (static)
    so_module (static)
    include_module (static)
    filter_module (static)
    deflate_module (static)
    http_module (static)
    mime_module (static)
    log_config_module (static)
    logio_module (static)
    env_module (static)
    expires_module (static)
    headers_module (static)
    unique_id_module (static)
    setenvif_module (static)
    version_module (static)
    proxy_module (static)
    proxy_connect_module (static)
    proxy_http_module (static)
    slotmem_shm_module (static)
    ssl_module (static)
    mpm_prefork_module (static)
    unixd_module (static)
    status_module (static)
    autoindex_module (static)
    asis_module (static)
    suexec_module (static)
    cgi_module (static)
    negotiation_module (static)
    dir_module (static)
    actions_module (static)
    userdir_module (static)
    alias_module (static)
    rewrite_module (static)
    bwlimited_module (shared)
    suphp_module (shared)
    security2_module (shared)

我使用以下 ip 和端口运行我的节点 js 应用程序

132.159.25.21:3030

我在 nodejs 应用程序中以以下方式定义了我的端口

var port = parseInt(process.env.PORT, 3030) || 8080;

参考这个错误:AH00548: NameVirtualHost has no effect and will be removed in the next release /usr/local/apache/conf/httpd.conf:445 已编写以下代码。 名称VirtualHost 132.159.25.21:80

我将可能的 vhosts 文件放在 /usr/local/apache/conf/httpd.conf 文件的末尾,因为他们在文件中写了一个警告“#请勿编辑。自动生成。如果您需要进行更改请使用包含的文件。”

我需要帮助来解决这个问题。如果您需要更多关于申请的信息,请告诉我。

【问题讨论】:

  • parseInt(process.env.PORT, 3030) parseInt 的第二个参数是基数。
  • 嗯是的..但这不是争论的问题
  • 您说您在132.159.25.21:3030 上运行您的节点,但代理传递到132.159.25.21:8080
  • @JohannesMerz :我已经遵​​循了所有方法希望您可以非常仔细地阅读我的问题并查看附加的链接。我已经尝试了所有方法,但无论端口 8080 还是端口 3030 都不起作用。这就是我在这里描述我的完整配置的原因。

标签: node.js apache reverse-proxy subdomain


【解决方案1】:

配置看起来是正确的,除了一位之外,/node 位置路径应该有目标匹配斜线以正确进行反向代理,而不会产生意外后果:

为了清晰起见,我宁愿这样定义它:

ProxyPass /node/ http://132.159.25.21:8080/
ProxyPassReverse /node/ http://132.159.25.21:8080/

考虑到使用目标http://132.159.25.21:8080 进行代理时不正确,它应始终定义为http://132.159.25.21:8080/,因此源应以相同的方式匹配斜杠,因此 /node/ 是正确的方式。

至于“NameVirtualHost”警告。它只是告诉您,在 2.4.X 版本的 Apache HTTPD 中不再需要它,因为该指令仅在 2.2 中需要,2.4 在检测命名虚拟主机时变得“更智能”。

旁注:&lt;Proxy *&gt; 没有效果,所以定义它是徒劳的。 Proxy * 用于转发代理,显然您不想要它,并且在 ProxyRequests 关闭的情况下禁用了此类功能。所以也删除那个。

--- 由于您的 cmets 回答扩展:

要访问您的节点,您必须访问:

http://node.example.com/node/

或添加一个“默认”重定向,将您带到它。例如:

RedirectMatch ^/$ /node/

您通过访问 node.example.com 来获取索引(文件列表),因为 -Indexes 是为子目录定义的,这意味着前一个已启用索引,所以定义:

<Directory /home/abc/public_html>
    Options -Indexes +FollowSymLinks
    ....

而不是定义 /home/abc/public_html/node,这不适用于这种情况,因为 node/ 不是目录,而是后端中的虚拟路径,因此为了更正抖动,它不应该包含在目录路径。

如果您只是想在访问http://node.example.com/ 的同时访问后端,只需使用它而不是ProxyPass /node/:

ProxyPass / http://132.159.25.21:8080/
ProxyPassReverse / http://132.159.25.21:8080/

【讨论】:

  • 首先感谢您给我一些有用的回复。我已删除 并添加不带 标记的 ProxyPass 和 ProxyPassReverse。它仍然不起作用,当我在浏览器中添加 url node.example.com 它只显示目录列表但是当我运行 node.example.com:3030 它运行 nodejs 应用程序和我在我的问题中提到的另一件事我有 https 连接,那么它可以与 https 连接一起使用吗?你能帮我解决这个问题吗?
  • 我已经“放大”了我之前的回答以回复您的 cmets。
  • 你参考我的配置快照链接吗?
  • 不,我指的是您在问题中的实际配置,但正如我所见,您发布的 png 与您在问题中发布的配置几乎相同,所以是的,它适用于两者
猜你喜欢
  • 1970-01-01
  • 2016-06-09
  • 2018-09-01
  • 2013-05-30
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 2016-12-09
相关资源
最近更新 更多