Reputation: 1155
I want to use Nodejs/Apache proxy pass to serve my APIs, but after add below apache(httpd) config, it seems that config not working.
OS:
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>
...
after:
sudo service httpd restart
Open example.com/api in the browser:
Not Found
The requested URL /api was not found on this server.
EDIT:
when I open example.com:1337/api
in the browser, everything is ok! but I want example.com/api
Upvotes: 3
Views: 5583
Reputation: 111
I'm using this configuration for long time with no problems.
The difference is only in the thing, that I'm using mod_rewrite
to define requests for Node.JS, not <Location>
definition.
It could proxy standard http/https request and also web sockets
to Node.JS application and back to client.
In example bellow - all requests starting with /api
substring are redirected
by Apace proxy to Node.JS application running on the same computer on
http://127.0.0.1:8888
(or on ws://127.0.0.1:8888
for websockets).
If Apache Virtual Host is configured with https://example.com/api
,
you need to use web socket address: 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>
Proxy tunnel will be created from https://example.com/api
to
http://127.0.0.1:8888
, but the unsecured communication is only inside
your webserver after proxy, so it's still OK.
In your firewall, it's only necessary to allow port :80
(or :443
for https)
in public zone. Not the port :8888
for Node.JS - that port has to be protected.
Upvotes: 2
Reputation: 61
If you want to proxy by using Location, following should work for you.
<Location /test>
ProxyPass http://127.0.0.1:3001/ retry=0 timeout=60 keepalive=On
ProxyPassReverse http://127.0.0.1:3001/
</Location>
Firt question is how will use your URL? example.com/api or example.com/api/smthng
If you will use api between slashes like /api/ you need to specify it in Location tag like following
/test to /test/
<Location /test/>
ProxyPass http://127.0.0.1:3001/ retry=0 timeout=60 keepalive=On
ProxyPassReverse http://127.0.0.1:3001/
</Location>
And another point is as you see, I also added a / in the end of my ProxyPass (ProxyPass http:.....:3001/ ). So, if you share us some example URLs we may give the correct config for you case.
For example, in my example: There is a VirtualHost listening 3001 port and there is a index.html (content is 'test') stored in the DocumentRoot. So if I browse :3001 , it will output test.
However, If I want to use Proxy (lets say this VirtualHost runs on port 88). So If I call some_ip:88/test , it will return test according to my first Location example. And I need to call some_ip:88/test/ for my second Location example.
Upvotes: 3
Reputation: 299
Try editing proxypass to add location and removing it's directory tag container.
ProxyPass /api/ http://MyVpsIp:1337/
ProxyPassReverse /api/ http://MyVpsIp:1337/
</VirtualHost>
Upvotes: 1