0

I have a both apache & nodejs server, apache on port 80 and nodejs on 3001

I wanna make a url for api calls something like:

https://mywebsite..com:3001/ to https://api.mywebsite.com/ on CloudFlare

Is it possible to do? I've tried Page Rules but it didn't work.

So, how can I do it?

Thanks

  • You have to configure apache to route requests from port 80 to port 3001. Read about reverse proxy. Or you can serve node.js app on port 80. – Grynets Jan 09 '19 at 13:48
  • if i do it on port 80, apache doesn't work – Alden Willms Jan 09 '19 at 13:55
  • Yes, if you'd like to run apache, you have to forward requests from apache to node.js app on port 3001. It's called reverse proxy. Look at this example https://stackoverflow.com/a/39250354/6066986 – Grynets Jan 09 '19 at 14:01
  • @Grynets When running a node application on a dedicated server you will need to start node as root or with sudo to allow it to use port 80. Indeed when Apache is running on port 80 on the same server you wont be able to start node on this port even when you are root – Brian van Rooijen Jan 09 '19 at 14:06
  • so i should httpd.conf for it right? where exactly is that in Ubuntu 14.04 – Alden Willms Jan 09 '19 at 14:09
  • @BrianvanRooijen please, read my comment carefully. I've mentioned reverse proxy to nodejs app. – Grynets Jan 09 '19 at 14:09
  • 1
    @Grynets my bad replied to your first comment. Reverse proxy will fix this. – Brian van Rooijen Jan 09 '19 at 14:14
  • @AldenWillms if it is a default install you can add an extra config to /etc/apache/sites-available and enable it with a2ensite function https://manpages.debian.org/jessie/apache2/a2ensite.8.en.html ensure you also enable the apache mod_proxy with a2enmod when setting up the proxy – Brian van Rooijen Jan 09 '19 at 14:16

1 Answers1

1

The best way to do this is to configure apache to proxy api.mywebsite.com to port 3001 on your server grepped the following example from <VirtualHost *:*> ProxyPreserveHost On ProxyPass "/" "http://localhost:3001/" ProxyPassReverse "/" "http://localhost:3001/" ServerName api.mywebsite.com </VirtualHost> https://httpd.apache.org/docs/2.4/vhosts/examples.html

please ensure you you also use the right CORS settings else you wont be able to request your api from yourwebsite frontend

A different way to prevent this is just to proxy the location /api to your api

<Location "/api/"> ProxyPass "http://localhost:3001" SetEnv force-proxy-request-1.0 1 SetEnv proxy-nokeepalive 1 </Location> https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

Brian van Rooijen
  • 1,906
  • 1
  • 14
  • 10
  • i've added the first one you wrote to /etc/apache2/sites-available/000-default.conf by changing api.mywebsite.com to my domain name, when i restarted apache, returned this * Restarting web server apache2 [fail] * The apache2 configtest failed. Output of config test was: AH00526: Syntax error on line 34 of /etc/apache2/sites-enabled/000-default.conf: Invalid command 'ProxyPreserveHost', perhaps misspelled or defined by a module not included in the server configuration Action 'configtest' failed. The Apache error log may have more information. – Alden Willms Jan 09 '19 at 14:19
  • did you enable mod_proxy? – Brian van Rooijen Jan 09 '19 at 14:20
  • sudo apt-get install libapache2-mod-proxy-html libxml2-dev -y sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_ajp sudo a2enmod rewrite sudo a2enmod deflate sudo a2enmod headers sudo a2enmod proxy_balancer sudo a2enmod proxy_connect sudo a2enmod proxy_html Ran all of them and now apache2 on restart says: [proxy_html:notice] [pid 9178] AH01425: I18n support in mod_proxy_html requires mod_xml2enc. Without it, non-ASCII characters in proxied pages are likely to display incorrectly. – Alden Willms Jan 09 '19 at 14:26
  • Ok with the notice it should start. To remove the message you can also install mod_xml2enc as proposed from the apache log message. If you are spending much time configuring it would also like to advice upgrading your ubuntu version as Ubuntu14.04 will no longer be supported starting april this year – Brian van Rooijen Jan 09 '19 at 14:49
  • it doesn't work, when i go to api.mysite.com: The DNS name does not exist. Error Code: INET_E_RESOURCE_NOT_FOUND – Alden Willms Jan 09 '19 at 15:07