Apache Proxy to Internal Server: Difference between revisions

From Leo's Notes
This page was last edited on 1 September 2019, at 06:20.
initial
 
m Text replacement - "Category:Linux{{Navbox Linux}}" to "{{Navbox Linux}}Category:Linux"
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Suppose you are running a server/daemon as a normal user on a very high port number but you wish to serve the contents on an existing Apache web server. You can use Apache's {{code|mod_rewrite}} module to proxy any requests made to Apache to your internal service.
Suppose you are running a server/daemon as a normal user on a very high port number but you wish to serve the contents on an existing Apache web server. You can use Apache's {{code|mod_rewrite}} and {{code|mod_proxy}} modules to proxy any requests made to Apache to your internal service.


An example use-case would be, if you ran a rails application on a cPanel account listening on {{code|127.0.0.1:300}} but you want to serve it on your primary domain.
An example use-case would be, if you ran a rails application on a cPanel account listening on {{code|127.0.0.1:300}} but you want to serve it on your primary domain.


If Apache has {{code|mod_rewrite}} support enabled, create a {{code|.htaccess}} file with the following contents:
If Apache has {{code|mod_rewrite}}, {{code|mod_proxy}} and {{code|mod_proxy_http}} support enabled, create a {{code|.htaccess}} file with the following contents:


{{highlight|lang=text|code=
{{highlight|lang=text|code=
# Prevent errors about /index.html not found
DirectoryIndex disabled
DirectoryIndex disabled


# Use [P] to enable proxy
RewriteEngine on
RewriteEngine on
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]
Line 14: Line 16:
With this file made, any requests made to your website will now be proxied by Apache.
With this file made, any requests made to your website will now be proxied by Apache.


[[Category:Linux]]
== See Also ==
* https://httpd.apache.org/docs/2.4/rewrite/proxy.html
 
{{Navbox Linux}}[[Category:Linux]]

Latest revision as of 06:20, 1 September 2019

Suppose you are running a server/daemon as a normal user on a very high port number but you wish to serve the contents on an existing Apache web server. You can use Apache's mod_rewrite and mod_proxy modules to proxy any requests made to Apache to your internal service.

An example use-case would be, if you ran a rails application on a cPanel account listening on 127.0.0.1:300 but you want to serve it on your primary domain.

If Apache has mod_rewrite, mod_proxy and mod_proxy_http support enabled, create a .htaccess file with the following contents:

# Prevent errors about /index.html not found
DirectoryIndex disabled

# Use [P] to enable proxy
RewriteEngine on
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

With this file made, any requests made to your website will now be proxied by Apache.

See Also