Node.js 代理传递的Nodejs/Apache配置
我想使用Nodejs/apacheproxy-pass为我的api提供服务,但是在添加了下面的Apache(httpd)配置之后,配置似乎不起作用 操作系统: /etc/httpd/conf/httpd.conf:Node.js 代理传递的Nodejs/Apache配置,node.js,apache,reverse-proxy,Node.js,Apache,Reverse Proxy,我想使用Nodejs/apacheproxy-pass为我的api提供服务,但是在添加了下面的Apache(httpd)配置之后,配置似乎不起作用 操作系统: /etc/httpd/conf/httpd.conf: ... <VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /home/MyUser/public_html <Direct
...
<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>
...
在浏览器中打开example.com/api:
Not Found
The requested URL /api was not found on this server.
编辑:
当我在浏览器中打开
example.com:1337/api
时,一切正常!但是我想要example.com/api
尝试编辑proxypass来添加位置并删除它的目录标记容器
ProxyPass /api/ http://MyVpsIp:1337/
ProxyPassReverse /api/ http://MyVpsIp:1337/
</VirtualHost>
ProxyPass/api/http://MyVpsIp:1337/
ProxyPassReverse/api/http://MyVpsIp:1337/
尝试编辑proxypass以添加位置并删除其目录标记容器
ProxyPass /api/ http://MyVpsIp:1337/
ProxyPassReverse /api/ http://MyVpsIp:1337/
</VirtualHost>
ProxyPass/api/http://MyVpsIp:1337/
ProxyPassReverse/api/http://MyVpsIp:1337/
如果您想使用位置代理,以下内容适用于您
<Location /test>
ProxyPass http://127.0.0.1:3001/ retry=0 timeout=60 keepalive=On
ProxyPassReverse http://127.0.0.1:3001/
</Location>
ProxyPasshttp://127.0.0.1:3001/ 重试=0超时=60保持激活=On
ProxyPassReversehttp://127.0.0.1:3001/
第一个问题是如何使用你的URL?
example.com/api或example.com/api/smthng
如果您将在斜杠之间使用api,如/api/您需要在如下位置标记中指定它
/测试到/测试/
<Location /test/>
ProxyPass http://127.0.0.1:3001/ retry=0 timeout=60 keepalive=On
ProxyPassReverse http://127.0.0.1:3001/
</Location>
ProxyPasshttp://127.0.0.1:3001/ 重试=0超时=60保持激活=On
ProxyPassReversehttp://127.0.0.1:3001/
另一点是,正如您所看到的,我还在ProxyPass(ProxyPass http:…..:3001/)的末尾添加了一个/。因此,如果您与我们分享一些示例URL,我们可能会为您提供正确的配置
例如,在我的示例中:
有一个VirtualHost侦听3001端口,DocumentRoot中存储了index.html(内容为“test”)。所以,如果我浏览:3001,它将输出test
但是,如果我想使用代理(假设此VirtualHost在端口88上运行)。因此,如果我调用某个_ip:88/test,它将根据我的第一个位置示例返回test。
我需要调用一些ip:88/test/作为我的第二个位置示例。如果您想使用位置代理,以下内容应该适合您
<Location /test>
ProxyPass http://127.0.0.1:3001/ retry=0 timeout=60 keepalive=On
ProxyPassReverse http://127.0.0.1:3001/
</Location>
ProxyPasshttp://127.0.0.1:3001/ 重试=0超时=60保持激活=On
ProxyPassReversehttp://127.0.0.1:3001/
第一个问题是如何使用你的URL?
example.com/api或example.com/api/smthng
如果您将在斜杠之间使用api,如/api/您需要在如下位置标记中指定它
/测试到/测试/
<Location /test/>
ProxyPass http://127.0.0.1:3001/ retry=0 timeout=60 keepalive=On
ProxyPassReverse http://127.0.0.1:3001/
</Location>
ProxyPasshttp://127.0.0.1:3001/ 重试=0超时=60保持激活=On
ProxyPassReversehttp://127.0.0.1:3001/
另一点是,正如您所看到的,我还在ProxyPass(ProxyPass http:…..:3001/)的末尾添加了一个/。因此,如果您与我们分享一些示例URL,我们可能会为您提供正确的配置
例如,在我的示例中:
有一个VirtualHost侦听3001端口,DocumentRoot中存储了index.html(内容为“test”)。所以,如果我浏览:3001,它将输出test
但是,如果我想使用代理(假设此VirtualHost在端口88上运行)。因此,如果我调用某个_ip:88/test,它将根据我的第一个位置示例返回test。
我需要调用一些ip:88/test/作为我的第二个位置示例。我使用此配置很长时间了,没有任何问题 区别在于,我使用的是
mod\u rewrite
定义对节点.JS的请求,而不是
定义
它可以代理标准http/https请求,也可以代理web套接字转到Node.JS应用程序并返回到客户端 在下面的示例中-所有以
/api
子字符串开头的请求都被重定向通过快速代理到同一台计算机上运行的Node.JS应用程序
http://127.0.0.1:8888
(或在ws://127.0.0.1:8888
上,用于WebSocket)。如果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>
。。。
LoadModule proxy\u modules/mod\u proxy.so
LoadModule proxy\u http\u module modules/mod\u proxy\u http.so
LoadModule proxy\u wstunnel\u module modules/mod\u proxy\u wstunnel.so
...
#虚拟主机基本配置:
ServerName example.com
DocumentRoot/var/www/html/example.com
#使用mod_重写引擎选择Node.JS的请求:
重新启动发动机
#Node.JS websockets请求代理配置:
RewriteCond%{HTTP:Upgrade}=websocket[NC]
重写规则/api/(*)ws://127.0.0.1:8888/$1[P,L]
#Node.JS https请求代理配置:
RewriteCond%{REQUEST_URI}^/api(.*)$
重写规则/api(*)http://127.0.0.1:8888$1[P,L]
## ... 任何可选的https证书配置后。。。
#SSLCertificateFile/etc/letsencrypt/。。。
#SSLCertificateKeyFile/etc/letsencrypt/。。。
#包括/etc/letsencrypt/。。。
#SSLCertificateChainFile/etc/letsencrypt/。。。
代理隧道将从https://example.com/api
至http://127.0.0.1:8888
,但不安全通信仅在内部您的Web服务器在代理之后运行,因此它仍然正常。
在防火墙中,只需要允许端口
:80
(或:443
用于https)在公共区域。不是Node.JS的端口
:8888
-必须保护该端口。我使用此配置很长时间了,没有任何问题
区别在于,我使用的是mod\u rewrite
定义对节点.JS的请求,而不是
定义
它可以代理标准http/https请求,也可以代理web套接字转到Node.JS应用程序并返回到客户端 在下面的示例中-所有以
/api
子字符串开头的请求都被重定向通过快速代理到节点。