Skip to content

Instantly share code, notes, and snippets.

@RafaelKa
Forked from progandy/README.md
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save RafaelKa/f17e1eafa740fd07eb3e to your computer and use it in GitHub Desktop.

Select an option

Save RafaelKa/f17e1eafa740fd07eb3e to your computer and use it in GitHub Desktop.

Revisions

  1. RafaelKa revised this gist May 6, 2014. 2 changed files with 90 additions and 38 deletions.
    22 changes: 22 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,28 @@ Send php files to php-fpm running on a TCP socket:
    LoadModule proxy_handler_module modules/mod_proxy_handler.so
    <FilesMatch \.php$>
    SetHandler "proxy:fcgi://127.0.0.1:9000/"
    #uncomment following line if fcgi server runs on same machine
    #ProxyPassInvalidFile Off
    </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>
    ```

    Send php files to php-fpm running on UDS socket(since Apache HTTPd 2.4.9):

    ```
    # set handler for php
    LoadModule proxy_handler_module modules/mod_proxy_handler.so
    <FilesMatch \.php$>
    SetHandler "proxy:unix:/path/to/socket.sock|fcgi://./"
    ProxyPassInvalidFile Off
    </FilesMatch>
    # configuration for phpmyadmin package in archlinux
    106 changes: 68 additions & 38 deletions mod_proxy_handler.c
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,90 @@
    /*
    * 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
    *
    *
    * 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;
    }

    /* Directives stuff */
    typedef struct {
    int directive_ProxyPassInvalidFile_is_on; // ProxyPassInvalidFile (Off|On) (Default: On)
    } proxy_handler_config;

    static proxy_handler_config config;

    const char *set_ProxyPassInvalidFile(cmd_parms *cmd, void *cfg, const char *arg) {
    if(!strcasecmp(arg, "off")) config.directive_ProxyPassInvalidFile_is_on = 0;
    else config.directive_ProxyPassInvalidFile_is_on = 1;
    return NULL;
    }

    static const command_rec proxy_handler_directives[] = {
    AP_INIT_TAKE1("ProxyPassInvalidFile", set_ProxyPassInvalidFile, NULL, ACCESS_CONF, "Handle only existing files with proxy.")
    };
    /* END: Directives stuff */

    static int requested_file_exists(request_rec *r) {
    apr_finfo_t finfo;
    char *filename;
    filename = apr_pstrdup(r->pool, r->filename);
    if ( apr_stat(&finfo, filename, APR_FINFO_MIN, r->pool) == APR_SUCCESS
    && finfo.filetype == APR_REG ) {
    return 1;
    }
    return DECLINED;
    return 0;
    }

    static int proxy_handler_handler(request_rec *r) {
    // stop if ProxyPassInvalidFile is OFF AND file does not exist
    if (!config.directive_ProxyPassInvalidFile_is_on && !requested_file_exists(r)) {
    return DECLINED;
    }
    // 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);

    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 */
    };
    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 */
    proxy_handler_directives, /* table of config file commands */
    proxy_handler_register_hooks /* register hooks */
    };
  2. @progandy progandy revised this gist Mar 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mod_proxy_handler.c
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ static int proxy_handler_handler(request_rec *r)
    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, AP_HOOK_REALLY_FIRST);
    ap_hook_handler(proxy_handler_handler, NULL, aszSucc, APR_HOOK_REALLY_FIRST);
    }

    /* Dispatch list for API hooks */
  3. @progandy progandy revised this gist Mar 15, 2014. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions mod_proxy_handler.c
    Original file line number Diff line number Diff line change
    @@ -33,17 +33,19 @@ static int proxy_handler_handler(request_rec *r)
    r->filename = apr_pstrcat(r->pool, r->handler, r->filename, NULL);
    apr_table_setn(r->notes, "rewrite-proxy", "1");
    r->handler = "proxy-server";
    return OK;
    // always return declined since we do some weird stuff:
    // we modify the request in a handler
    return DECLINED;
    }
    }
    return OK;
    return DECLINED;

    }

    static void proxy_handler_register_hooks(apr_pool_t *p)
    {
    static const char * const aszSucc[] = { "mod_proxy.c", NULL };
    ap_hook_fixups(proxy_handler_handler, NULL, aszSucc, APR_HOOK_FIRST);
    ap_hook_handler(proxy_handler_handler, NULL, aszSucc, AP_HOOK_REALLY_FIRST);
    }

    /* Dispatch list for API hooks */
  4. @progandy progandy revised this gist Mar 8, 2014. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion mod_proxy_handler.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,18 @@

    /*
    * 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"
    @@ -8,6 +22,8 @@

    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 &&
  5. @progandy progandy created this gist Mar 8, 2014.
    28 changes: 28 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # 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>
    ```
    42 changes: 42 additions & 0 deletions mod_proxy_handler.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@

    #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)
    {
    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";
    return OK;
    }
    }
    return OK;

    }

    static void proxy_handler_register_hooks(apr_pool_t *p)
    {
    static const char * const aszSucc[] = { "mod_proxy.c", NULL };
    ap_hook_fixups(proxy_handler_handler, NULL, aszSucc, APR_HOOK_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 */
    };