Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proxy requests only if the file is not found?

I'm looking for an equivalent of Redirect requests only if the file is not found? where instead of redirecting, I want it to do a ProxyPass to another server if a file is missing.

This didn't work too well since it will not handle requests that do not end with "/" e.g. https://site.trajano.net/trajano which goes to the proxy and redrects instead.

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
RewriteRule (.*) https://trajano.github.io/%{REQUEST_URI} [P]

I tried the following from a different answer as well which works slightly better but what happens is it redirects to github rather than proxy.

RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
#
# Requested resource does not exist, do rewrite if it exists in /archive
RewriteCond %{DOCUMENT_ROOT}/archive/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/archive/$1 -d
RewriteRule (.*) /archive/$1 [L]
#
# Else
RewriteRule (.*) https://trajano.github.io/%{REQUEST_URI} [P]
like image 513
Archimedes Trajano Avatar asked Apr 15 '17 00:04

Archimedes Trajano


People also ask

What is ProxyPass and ProxyPassReverse?

The "ProxyPass" and "ProxyPassReverse" parameters are used to tell Apache how to proxy requests. They require the "mod_proxy.so" and "mod_proxy_http.so" Apache modules, which are loaded by default in RHEL5 and RHEL6, but check the following lines are uncommented in the "/etc/httpd/conf/httpd. conf" file to make sure.

What is proxy timeout in Apache?

Apache Default timeout is set to 300 seconds. This issue comes only when accessing website through apache reverse proxy. It works well by using internal IP.

What is proxy preserve host?

The ProxyPreserveHost directive is used to instruct Apache mod_proxy, when acting as a reverse proxy, to preserve and retain the original Host: header from the client browser when constructing the proxied request to send to the target server.

What is Proxyrequests?

When you send a web request, your request goes to the proxy server first. The proxy server then makes your web request on your behalf, collects the response from the web server, and forwards you the web page data so you can see the page in your browser.


1 Answers

Both mod_rewrite and mod_proxy need to be enabled. Place the following in a VirtualHost block (not inside a Directory directive or anything other than VirtualHost):

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [L]
#
# Else proxy
RewriteRule ^/(.*)$ https://trajano.github.io/$1 [P,QSA]
ProxyPassReverse / https://trajano.github.io/

The first Rewrite block handles files and directories that exist in which case it returns the original file block.

The next rewrite rule will handle the proxies. The ProxyPassReverse ensures that the Location being returned is rewritten back to the local server rather than the proxied server.

like image 191
Archimedes Trajano Avatar answered Oct 13 '22 13:10

Archimedes Trajano