If I want to route a HTTP request through a local (alias: on the current server) Proxy, how would I go around this?
The allowed Proxy settings for the wp-config.php
are the following:
# HTTP Proxies
# Used for e.g. in Intranets
# Fixes Feeds as well
# Defines the proxy adresse.
define( 'WP_PROXY_HOST', '127.0.84.1' );
# Defines the proxy port.
define( 'WP_PROXY_PORT', '8080' );
# Defines the proxy username.
define( 'WP_PROXY_USERNAME', 'my_user_name' );
# Defines the proxy password.
define( 'WP_PROXY_PASSWORD', 'my_password' );
# Allows you to define some adresses which
# shouldn't be passed through a proxy.
define( 'WP_PROXY_BYPASS_HOSTS', 'localhost, www.example.com' );
This question is a follow up on this question.
The proxy settings work just like a regular HTTP requests but in this case obviously routed through a proxy. In terms of WordPress the API’s transport layers all support proxy connections(fsockopen, fopen, cURL, ).
The things about proxy configurations are they come in several flavors and each setup is different so it makes answering this difficult, it doesn’t really matter if your proxy is on your localhost or remote, the
wp-config.php
settings will work regardless. Typically you want to use these settings if your on an intranet/firewall that has specific requirements.It is worth noting you can just set your localhost/webserver to use a proxy/chaining by default for HTTP requests and in that case it is not necessary to set any options with
wp-config.php
since this is configured at the server level . If you disable your proxy you typically see a response code oferror 130 ERR_PROXY_CONNECTION_FAILED
, but these setups are outside the scope of WordPress.Some tools that can help you setup and debug proxy connections:
To dig into the WordPress HTTP API I recommend the following snippet using the
http_api_debug
action (altered tovar_dump
found via viper007bond’s site):The Request Response is the interesting part, sometimes you can tell with a quick glance if your request is going through proxy..
For example using the default HTTP API to make the following request.
Now the same exact request but using a remote proxy enabled via wp-config.php
As you can see the proxy the output is different, most importantly the proxy is adding the
via
tag, in this case a squid proxy. Proxies are supposed to do this and not alter the server response-header , but not everyone follows the rules so be careful;).The constant
define( 'WP_PROXY_BYPASS_HOSTS', 'localhost, www.example.com' )
is useful for allowing access to hosts that you might not want to go through a proxy (WordPress updates for example). The comments in theclass-http.php
are misleading because by defaultlocalhost
andget_option('siteurl);
are already included but can be altered via thepre_http_send_through_proxy
filter.Some additional options that work with the proxy settings are:
WP_HTTP_BLOCK_EXTERNAL
WP_ACCESSIBLE_HOSTS