【问题标题】:HTTP Origin header didn't match request.base_urlHTTP Origin 标头与 request.base_url 不匹配
【发布时间】:2018-09-27 04:43:25
【问题描述】:

我目前花了几天时间试图解决这个问题。就目前而言,每当我通过 Chrome 在生产环境中登录 Rails 5 应用程序时,都会出现此错误。我正在运行rails version 5.1.5ruby 2.3.1nginx/1.10.3。我的应用程序在 ELB(弹性负载均衡器)后面的 EC2 实例上运行,其转发规则为端口 80 上的 http 到端口 443 上的 https。我很清楚这个问题源于我的请求标头指示的事实出发地与目的地不同。我也知道这可以通过更新我的nginx.conf 文件或禁用 Rails 的 CSRF 保护来纠正(据我所知,这对我来说不是安全问题的可行解决方案)。我试图通过 before_action 在 application_controller 中手动设置标头来解决此问题,但这不起作用。我还尝试使用我在 SO 和其他地方找到的示例更新我的 nginx.conf 文件,但这只会导致 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;
        #       }
        #}

【问题讨论】:

    标签: ruby-on-rails nginx ruby-on-rails-5


    【解决方案1】:

    您需要在 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
        }
    
    }
    

    Source

    【讨论】:

    • 这是否位于site-available sites-enabled 文件中?
    • 不管那里,都应该包含在nginx主配置中。
    • 要明确的是,我将标题添加到了站点可用
    • 它应该包含在主配置中,因为它是特定于站点的。此外,启用站点的站点包括可用站点的链接(如果按照建议配置),因此您编辑的文件无关紧要(实际上,它们是完全相同的文件,因此对一个文件的任何更改都会反映在另一个文件中)。如果它们不是同一个文件,则必须在启用站点时编辑该文件。
    猜你喜欢
    • 1970-01-01
    • 2019-04-23
    • 2022-06-19
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2021-08-11
    • 2020-09-21
    相关资源
    最近更新 更多