如何在apache服务器中运行nodejs应用程序

Dip*_*vda 6 apache subdomain reverse-proxy node.js

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

我参考以下链接以供参考.

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

在apache中运行Node.js?

项目目录:

/家庭/ ABC /的public_html /节点

我的主要域名是:

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>
Run Code Online (Sandbox Code Playgroud)

命令摘要:

命令:

/ 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)
Run Code Online (Sandbox Code Playgroud)

命令

/ 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)
Run Code Online (Sandbox Code Playgroud)

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

132.159.25.21:3030

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

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

引用此错误:AH00548:NameVirtualHost无效,将在下一个版本/usr/local/apache/conf/httpd.conf:445中删除代码.NameVirtualHost 132.159.25.21:80

我把may vhosts文件放在/usr/local/apache/conf/httpd.conf文件的末尾,因为在文件中他们写了一个警告"#DO NOT EDIT.自动生成.如果你需要改变请使用包括文件."

我需要帮助才能解决此问题.如果您需要进一步了解申请,请告知我们.

ezr*_*a-s 6

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

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

ProxyPass /node/ http://132.159.25.21:8080/
ProxyPassReverse /node/ http://132.159.25.21:8080/
Run Code Online (Sandbox Code Playgroud)

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

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

SideNote:<Proxy *>没有效果,所以定义它是徒劳的.Proxy*用于转发代理,显然您不需要它,并且已关闭ProxyRequests禁用此类功能.所以也删除那个.

---由于您的意见,答案延期:

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

http://node.example.com/node/
Run Code Online (Sandbox Code Playgroud)

或添加一个"默认"重定向,将您带到它.如:

RedirectMatch ^/$ /node/
Run Code Online (Sandbox Code Playgroud)

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

<Directory /home/abc/public_html>
    Options -Indexes +FollowSymLinks
    ....
Run Code Online (Sandbox Code Playgroud)

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

如果您只想在访问http://node.example.com/时访问后端,只需使用此代替ProxyPass/node /:

ProxyPass / http://132.159.25.21:8080/
ProxyPassReverse / http://132.159.25.21:8080/
Run Code Online (Sandbox Code Playgroud)