Nodejs/Apache配置用于代理传递

3

我希望使用Nodejs/Apache代理传递来提供我的API,但是在添加下面的Apache(httpd)配置后,似乎配置没有起作用。

操作系统:

CentOS 6

/etc/httpd/conf/httpd.conf:

...
<VirtualHost *:80>
   ServerName example.com
   ServerAlias www.example.com
 
   DocumentRoot /home/MyUser/public_html
   <Directory />
      Options -Indexes +FollowSymLinks
      AllowOverride None
      Require all granted
   </Directory>
 
   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>
 
   <Location /api>
      ProxyPass http://MyVpsIp:1337
      ProxyPassReverse http://MyVpsIp:1337
   </Location>
 
</VirtualHost>
...

之后:

sudo service httpd restart

在浏览器中打开example.com/api:
Not Found
The requested URL /api was not found on this server.

编辑: 当我在浏览器中打开 example.com:1337/api 时,一切正常!但我想要的是 example.com/api

3个回答

3
如果您想使用Location进行代理,下面的方法应该适用于您。
  <Location /test>
            ProxyPass http://127.0.0.1:3001/  retry=0 timeout=60 keepalive=On
            ProxyPassReverse http://127.0.0.1:3001/
  </Location>

首先问题是你将如何使用你的URL?example.com/api还是example.com/api/smthng

如果你将在斜杠之间使用API,比如/api/,那么你需要在Location标签中指定它,如下所示:

/test变为/test/

  <Location /test/>
            ProxyPass http://127.0.0.1:3001/  retry=0 timeout=60 keepalive=On
            ProxyPassReverse http://127.0.0.1:3001/
  </Location>

另一个要点是,正如你所看到的,我还在我的ProxyPass结尾处添加了一个/(ProxyPass http:.....:3001/)。因此,如果您与我们分享一些示例URL,我们可以为您提供正确的配置。例如,在我的示例中:有一个监听3001端口的VirtualHost,并且在DocumentRoot中存储了一个index.html(内容为“test”)。因此,如果我浏览:3001,它将输出test。但是,如果我想使用代理(假设此VirtualHost运行在88端口),那么如果我调用some_ip:88/test,它将根据我的第一个Location示例返回test。而对于我的第二个Location示例,我需要调用some_ip:88/test/。

谢谢兄弟。这是一个例子: http(s)://example.com/api/something => http://localhost/api/something - Milad Jafari

2
我使用这个配置已经很长时间了,没有出现任何问题。
唯一的区别在于,我使用mod_rewrite来定义Node.JS请求,而不是使用<Location>定义。
它可以代理标准的http/https请求,也可以代理web sockets到Node.JS应用程序并返回给客户端。
在下面的例子中,所有以/api子字符串开头的请求都会被Apache代理重定向到运行在同一台计算机上的Node.JS应用程序,地址为http://127.0.0.1:8888(或者对于websockets,地址为ws://127.0.0.1:8888)。
如果Apache虚拟主机配置为https://example.com/api,则需要使用web socket地址:wss://example.com/api
...
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
...

<VirtualHost 127.0.0.1:443>
    # Virtual host basic configuration:
    ServerName example.com
    DocumentRoot /var/www/html/example.com

    # Use mod_rewrite engine to select request for Node.JS:
    RewriteEngine on

    # Node.JS websockets requests proxy configuration:
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /api/(.*) ws://127.0.0.1:8888/$1 [P,L]
    # Node.JS https requests proxy configuration:
    RewriteCond %{REQUEST_URI} ^/api(.*)$
    RewriteRule /api(.*) http://127.0.0.1:8888$1 [P,L]

    ## ... any optional https certificates configuration after...
    #SSLCertificateFile /etc/letsencrypt/...
    #SSLCertificateKeyFile /etc/letsencrypt/...
    #Include /etc/letsencrypt/...
    #SSLCertificateChainFile /etc/letsencrypt/...
</VirtualHost>

https://example.com/apihttp://127.0.0.1:8888 将创建代理隧道,但仅在代理后您的 Web 服务器内部进行不安全通信,因此仍然可以使用。
在防火墙中,只需要在公共区域允许端口 :80(或用于 https 的 :443)即可。不需要允许 Node.JS 的 :8888 端口 - 该端口必须受到保护。


1
尝试编辑proxypass以添加位置并删除其目录标记容器。
  ProxyPass /api/ http://MyVpsIp:1337/
  ProxyPassReverse /api/ http://MyVpsIp:1337/
</VirtualHost>

谢谢兄弟。这是一个例子: http(s)://example.com/api/something => http://localhost/api/something - Milad Jafari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接