我想使用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
答案 0 :(得分:1)
我长时间使用此配置没有问题。
区别仅在于我使用的是mod_rewrite
定义对 Node.JS 的请求,而不是<Location>
定义。
它可以代理标准的http / https请求以及Web套接字
到Node.JS应用程序并返回到客户端。
在下面的示例中,所有以/api
子字符串开头的请求都被重定向了
通过Acece代理运行在同一台计算机上的Node.JS应用程序
http://127.0.0.1:8888
(或在WebSocket的ws://127.0.0.1:8888
上。)
如果Apache虚拟主机配置了https://example.com/api
,
您需要使用Web套接字地址: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/api
到
http://127.0.0.1:8888
,但不安全的通信仅在内部
您的Web服务器在代理之后,因此仍然可以。
在您的防火墙中,只需要允许端口:80
(或:443
用于https)
在公共区域。不是Node.JS的端口:8888
-该端口必须受到保护。
答案 1 :(得分:0)
尝试编辑proxypass以添加位置并删除其目录标签容器。
ProxyPass /api/ http://MyVpsIp:1337/
ProxyPassReverse /api/ http://MyVpsIp:1337/
</VirtualHost>
答案 2 :(得分:0)
如果您要使用“位置”代理,则应该可以使用以下内容。
<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,则需要在位置标记中进行指定,如下所示
/测试到/ 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,我们可能会为您提供正确的配置。
例如,在我的示例中: 有一个VirtualHost侦听3001端口,并且在DocumentRoot中存储了index.html(内容为“ test”)。因此,如果我浏览:3001,它将输出测试。
但是,如果我想使用 Proxy (可以说此VirtualHost运行在端口88上)。因此,如果我调用some_ip:88 / test,它将根据我的第一个位置示例返回测试。 第二个位置示例中,我需要调用some_ip:88 / test/。