简体   繁体   English

Apache,使用 Location 将某个端点路由到不同的端口

[英]Apache, Issue routing a certain endpoint to a different port using Location

I'm trying to route any links that start with /api/ to port 3002 on my server but they always get routed to 3008. For example https://example.com/api/customers should be proxied/routed to localhost:3002我正在尝试将任何以 /api/ 开头的链接路由到我服务器上的端口 3002,但它们总是路由到 3008。例如https://example.com/api/customers应该被代理/路由到 localhost:3002

<VirtualHost *:443>
    ServerAdmin (redacted)
    ServerName (redacted)
    ServerAlias (redacted)

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/(redacted)/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/(redacted)/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/(redacted)/chain.pem

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Location "/api\/(.*)/">
        ProxyPass http://localhost:3002/
        ProxyPassReverse http://localhost:3002/
    </Location>

    <Location "/">
        ProxyPass http://localhost:3008/
        ProxyPassReverse http://localhost:3008/
    </Location>
</VirtualHost>

Using this config going to domain.com works and shows my website but domain.com/api/customers returns an error from the webapp on port 3008 so it's not being routed correctly (it should go to 3002).使用此配置转到 domain.com 可以工作并显示我的网站,但 domain.com/api/customers 从端口 3008 上的 webapp 返回错误,因此它没有正确路由(它应该 go 到 3002)。

The apps on port 3008 and 3002 are running correctly so that's not the issue.端口 3008 和 3002 上的应用程序运行正常,所以这不是问题所在。 I've tried putting domain/ first and domain/api last in the config file but that didnt seem to fix it.我试过将 domain/ first 和 domain/api 最后放在配置文件中,但这似乎没有解决它。 And the config file is enabled并启用配置文件

I've tried different regexes to match the api endpoint aswell but this one should work我也尝试了不同的正则表达式来匹配 api 端点,但这个应该可以工作

Apache is listening on port 443 Apache 正在侦听端口 443

These mods are enabled which should be needed for this: proxy_module (shared) proxy_http_module (shared) proxy_wstunnel_module (shared)这些 mod 已启用,为此需要: proxy_module(共享) proxy_http_module(共享) proxy_wstunnel_module(共享)

Please let me know if you want any extra information如果您需要任何额外信息,请告诉我

Modify your config as below and have a try.如下修改您的配置并尝试一下。

Post the access log and curl response if not working.如果不起作用,请发布访问日志和 curl 响应。

curl -ILKv https://domain.name/api/anything curl -ILKv https://domain.name/api/anything

curl -ILKv https://domain.name/api curl -ILKv https://domain.name/api

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

ProxyRequests Off
<Proxy *>
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
</Proxy>
ProxyPass /api http://localhost:3002
ProxyPassReverse /api http://localhost:3002

I ended up solving it like this:我最终像这样解决了它:

RewriteEngine  on
RewriteRule "/api\/(.*)" "http://localhost:3002/api/$1" [P] 

<Location "/">
    ProxyPass http://localhost:3008/
    ProxyPassReverse http://localhost:3008/
</Location>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM