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
3
Answers
Try editing proxypass to add location and removing it’s directory tag container.
If you want to proxy by using Location, following should work for you.
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/
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.
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 redirectedby Apace proxy to Node.JS application running on the same computer on
http://127.0.0.1:8888
(or onws://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
.Proxy tunnel will be created from
https://example.com/api
tohttp://127.0.0.1:8888
, but the unsecured communication is only insideyour 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.