gpt4 book ai didi

ruby-on-rails - HTTP Origin header 与 request.base_url 不匹配

转载 作者:行者123 更新时间:2023-12-03 15:57:07 27 4
gpt4 key购买 nike

我目前花了几天时间试图解决这个问题。目前,每当我通过 Chrome 登录到生产环境中的 Rails 5 应用程序时,都会出现此错误。我在运行 rails version 5.1.5 , ruby 2.3.1 , 和 nginx/1.10.3 .我的应用程序在 ELB(弹性负载均衡器)后面的 EC2 实例上运行,转发规则为端口 80 上的 http 到端口 443 上的 https。我很清楚这个问题源于这样一个事实,即我的请求 header 指示出发地与目的地不同。我也知道这可以通过更新我的 nginx.conf 来纠正。文件或禁用 Rails 的 CSRF 保护(据我了解,出于安全考虑,这对我来说不是可行的解决方案)。我试图通过 before_action 在 application_controller 中手动设置 header 来解决这个问题,但这不起作用。我也尝试更新我的 nginx.conf包含我在 SO 和其他地方找到的示例的文件,但这只会导致 502 网关错误。问题是我发现的示例的语法在某种程度上不兼容,或者我只是犯了所有可能的笔误,我合法地一次添加一行重新启动服务器并重新部署,但仍然没有运气。理想情况下,如果我尝试设置标题不起作用,我想解决 rails 侧的问题:
application_controller.rb

protect_from_forgery with: :exception, prepend: true
before_action :set_https_header

def set_https_header
response.set_header('X-Frame-Options', 'SAMEORIGIN')
end

如果我必须更新 ngnix.conf有人可以围绕语法提供一些押韵或推理。
production.rb
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true

config.read_encrypted_secrets = true
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = Uglifier.new(harmony: true)

config.assets.compile = false
config/initializers/assets.rb

# config.force_ssl = true
config.log_level = :debug
config.log_tags = [ :request_id ]

config.action_mailer.perform_caching = false
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

最佳答案

您需要在 nginx 配置中添加 header (还有另一个带有服务器配置的文件,而不是 nginx.conf ),这是一个示例:

server {
listen 80;
server_name server.com www.server.com;
# some configuration here

location @server {
# ... some configuration here
# this set proper header
proxy_set_header Host www.my_actual_domain_name.com;
# ... some configuration here
}

}

Source

关于ruby-on-rails - HTTP Origin header 与 request.base_url 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52529593/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com