HTTP Origin 标头与 request.base_url 不匹配

won*_*bie 6 ruby-on-rails nginx ruby-on-rails-5

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

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
Run Code Online (Sandbox Code Playgroud)

如果我必须更新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
Run Code Online (Sandbox Code Playgroud)

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;
        #       }
        #}
Run Code Online (Sandbox Code Playgroud)

Зел*_*ный 8

您需要在 nginx 配置中添加标头(还有另一个带有服务器配置的文件,而不是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
    }

}
Run Code Online (Sandbox Code Playgroud)

Source