Skip to content
This repository was archived by the owner on Dec 2, 2018. It is now read-only.

Commit 52cd23f

Browse files
committed
Proxy off to PHP better.
The preserve_host option on rack-rewrite seems a bit off. I would assume it would mean it keeps the hostname requested. But instead it replaces it with the internal hostname (the host we proxy to). There is a ticket related to this (jaswope/rack-reverse-proxy#11). In addition, even if it pulled from the right host it also doesn't include the port. There is also already a ticket for this (jaswope/rack-reverse-proxy#17). To resolve this I simply avoiding using the preserve host option. This allows the original host to pass on through to PHP (it doesn't seem to need to be explicitly set, PHP already know it). Both of these option (wrong host and missing port) cause some PHP scripts, like Wordpress, who are trying to redirect to a canonical URL to get caught in an infinate loop. I am also now stripping out the index.php from the PATH_INFO before proxying off. The PHP webserver has it's own built-in method of looking for the index.php. So we add it for detecting the PHP handler should be called but then remove it to let PHP do things it's own way. This helps for script like Wordpress that try to direct to a canoncial URL. It prevent it from bouncing between index.php and the root directory. It may be slightly incorrect for cases where the user explicitly chose to pull up the index.php script. But in the real world I don't know where that would cause a problem and making this change solves a real world problem for Wordpress. Finally, I am passing on the HTTP_X_FORWARDED_FOR header. Although not standardized it is fairly widely and provide a way for the script to determine the IP address of the real browser since REMOTE_ADDR will be set to the proxy server.
1 parent 2fc412f commit 52cd23f

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/rack/legacy/php.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class Rack::Legacy::Php
2222
#
2323
def initialize app, public_dir=Dir.getwd, php_exe='php', port=8180, quiet=false
2424
@app = app; @public_dir = public_dir
25-
@proxy = Rack::ReverseProxy.new {reverse_proxy /^.*$/, "http://localhost:#{port}"}
25+
@proxy = Rack::ReverseProxy.new do
26+
reverse_proxy_options preserve_host: false
27+
reverse_proxy /^.*$/, "http://localhost:#{port}"
28+
end
2629
@php = ChildProcess.build php_exe,
2730
'-S', "localhost:#{port}", '-t', public_dir
2831
@php.io.inherit! unless quiet
@@ -35,6 +38,9 @@ def initialize app, public_dir=Dir.getwd, php_exe='php', port=8180, quiet=false
3538
def call env
3639
if valid? env['PATH_INFO']
3740
@php.start unless @php.alive?
41+
ip = Rack::Request.new(env).ip
42+
env['HTTP_X_FORWARDED_FOR'] = ip if ip
43+
env['PATH_INFO'].gsub! /\/index.php$/, '/'
3844
@proxy.call env
3945
else
4046
@app.call env

rack-legacy.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'rack-legacy'
3-
s.version = '0.9.0'
3+
s.version = '0.9.1'
44
s.homepage = 'https://github.com/eric1234/rack-legacy'
55
s.author = 'Eric Anderson'
66
s.email = '[email protected]'

0 commit comments

Comments
 (0)