Skip to content

Instantly share code, notes, and snippets.

@progandy
Last active August 26, 2020 11:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save progandy/6ed4eeea60f6277c3e39 to your computer and use it in GitHub Desktop.
Save progandy/6ed4eeea60f6277c3e39 to your computer and use it in GitHub Desktop.
mod_proxy_handler for apache 2.4

mod_proxy_handler

This module for apache 2.4 allows you to use e.g. mod_proxy_fcgi in AddHandler or SetHandler directives.

Compile and install the module with apxs:

apxs -i -a -c mod_proxy_handler.c

Example:

Send php files to php-fpm running on a TCP socket:

# set handler for php
LoadModule proxy_handler_module modules/mod_proxy_handler.so
<FilesMatch \.php$>
    SetHandler "proxy:fcgi://127.0.0.1:9000/"
</FilesMatch>

# configuration for phpmyadmin package in archlinux 
Alias /phpmyadmin "/usr/share/webapps/phpMyAdmin"
<Directory "/usr/share/webapps/phpMyAdmin">
    DirectoryIndex index.html index.php
    AllowOverride All
    Options FollowSymlinks
    Require all granted
</Directory>
/*
* Copyright 2014 Andreas Bosch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "apr_strings.h"
#include "http_request.h"
static int proxy_handler_handler(request_rec *r)
{
// This function is adapted from a patch to mod_proxy
// http://svn.apache.org/viewvc?view=revision&revision=1573626
if (r->filename && !r->proxyreq) {
/* We may have forced the proxy handler via config or .htaccess */
if (r->handler &&
strncmp(r->handler, "proxy:", 6) == 0 &&
strncmp(r->filename, "proxy:", 6) != 0) {
r->proxyreq = PROXYREQ_REVERSE;
r->filename = apr_pstrcat(r->pool, r->handler, r->filename, NULL);
apr_table_setn(r->notes, "rewrite-proxy", "1");
r->handler = "proxy-server";
// always return declined since we do some weird stuff:
// we modify the request in a handler
return DECLINED;
}
}
return DECLINED;
}
static void proxy_handler_register_hooks(apr_pool_t *p)
{
static const char * const aszSucc[] = { "mod_proxy.c", NULL };
ap_hook_handler(proxy_handler_handler, NULL, aszSucc, APR_HOOK_REALLY_FIRST);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA proxy_handler_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
proxy_handler_register_hooks /* register hooks */
};
@butesa
Copy link

butesa commented Jul 24, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment