Ruby on rails 重定向“重定向到错误路径,路径中有逗号”;http://myapp.com,myapp.com/foo“;
使用rails版本Ruby on rails 重定向“重定向到错误路径,路径中有逗号”;http://myapp.com,myapp.com/foo“;,ruby-on-rails,Ruby On Rails,使用rails版本 rails (5.1.7) actioncable (= 5.1.7) actionmailer (= 5.1.7) actionpack (= 5.1.7) actionview (= 5.1.7) activejob (= 5.1.7) activemodel (= 5.1.7) activerecord (= 5.1.7) activesupport (= 5.1.7
rails (5.1.7)
actioncable (= 5.1.7)
actionmailer (= 5.1.7)
actionpack (= 5.1.7)
actionview (= 5.1.7)
activejob (= 5.1.7)
activemodel (= 5.1.7)
activerecord (= 5.1.7)
activesupport (= 5.1.7)
bundler (>= 1.3.0)
railties (= 5.1.7)
sprockets-rails (>= 2.0.0)
使用config/environments/production.rb
config.app_domain = 'myapp.com'
config.action_mailer.default_url_options = { host: 'myapp.com', protocol: 'https' }
控制器内操作返回
D, [2019-07-30T17:53:11.422822 #1] DEBUG -- : [9a567258-d2de-48da-9f89-080bd5a57a2e] foo_path=/foo
D, [2019-07-30T17:53:11.422905 #1] DEBUG -- : [9a567258-d2de-48da-9f89-080bd5a57a2e] foo_url=http://myapp.com, myapp.com/foo
I, [2019-07-30T17:53:11.423239 #1] INFO -- : [9a567258-d2de-48da-9f89-080bd5a57a2e] Redirected to http://myapp.com, myapp.com/foo
什么会导致这种行为
更新 在应用@Benj comment后使用
url_options = { host: 'myapp.com', protocol: 'https' }
config.action_mailer.default_url_options = url_options
config.action_controller.default_url_options = url_options
Rails.application.routes.default_url_options = url_options
拥有
Rails.logger.debug("HERE admin_root_path #{admin_root_path}")
Rails.logger.debug("HERE admin_root_url #{admin_root_url}")
Rails.logger.debug("HERE new_session_path #{new_session_path}")
Rails.logger.debug("HERE new_session_url #{new_session_url}")
Rails.logger.debug("HERE root_url #{root_url}")
Rails.logger.debug("HERE Rails.configuration.action_mailer.default_url_options #{Rails.configuration.action_mailer.default_url_options}")
Rails.logger.debug("HERE Rails.configuration.action_controller.default_url_options #{Rails.configuration.action_controller.default_url_options}")
Rails.logger.debug("HERE Rails.configuration.default_url_options #{Rails.configuration.try(:default_url_options).inspect}")
Rails.logger.debug("HERE Rails.configuration.app_domain #{Rails.configuration.try(:app_domain).inspect}")
Rails.logger.debug("HERE Rails.application.routes.default_url_options #{Rails.application.routes.default_url_options}")
redirect_to admin_root_path
输出
D, [2019-07-31T07:50:36.461467 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE admin_root_path /admin
D, [2019-07-31T07:50:36.461605 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE admin_root_url https://myapp.com/admin
D, [2019-07-31T07:50:36.461728 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE new_session_path /session
D, [2019-07-31T07:50:36.461840 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE new_session_url https://myapp.com/session
D, [2019-07-31T07:50:36.461934 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE root_url https://myapp.com/
D, [2019-07-31T07:50:36.462000 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE Rails.configuration.action_mailer.default_url_options {:host=>"myapp.com", :protocol=>"https"}
D, [2019-07-31T07:50:36.462050 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE Rails.configuration.action_controller.default_url_options {:host=>"myapp.com", :protocol=>"https"}
D, [2019-07-31T07:50:36.462104 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE Rails.configuration.default_url_options nil
D, [2019-07-31T07:50:36.462158 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE Rails.configuration.app_domain nil
D, [2019-07-31T07:50:36.462215 #1] DEBUG -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] HERE Rails.application.routes.default_url_options {:host=>"myapp.com", :protocol=>"https"}
I, [2019-07-31T07:50:36.462421 #1] INFO -- : [33c730fe-8fe7-4873-99d3-aa77e6542287] Redirected to http://myapp.com, myapp.com/admin
您可以将url选项设置为
config.action\u controller.default\u url\u options
在config/environments/production.rb中:
url_options = { host: 'myapp.com', protocol: 'https' }
config.action_mailer.default_url_options = url_options
config.action_controller.default_url_options = url_options
此外,如果在使用管线帮助器时遇到问题,还可以设置:
Rails.application.routes.default_url_options = url_options
我已经检查了源代码 我已经检查了
请求。带有\u端口的主机\u
,这将导致我
我在一本书中发现了这一点
request.env['HTTP\u X\u FORWARDED\u HOST']
为空,但request.env['HTTP\u HOST']
为“myapp.com,myapp.com”
因此,我添加了proxy\u set\u头X-Forwarded-Host$Host;
至nginx配置
nginx condig之前
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_read_timeout 30;
nginx condig
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Host $host;
proxy_read_timeout 30;
def raw_host_with_port
if forwarded = env["HTTP_X_FORWARDED_HOST"]
forwarded.split(/,\s?/).last
else
env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
end
end
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_read_timeout 30;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Host $host;
proxy_read_timeout 30;