Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails HTTP源报头(http://app.example.local:8080)没有';与request.base\u url不匹配(http://app.example.local)_Ruby On Rails_Reactjs_Api_Nginx - Fatal编程技术网

Ruby on rails HTTP源报头(http://app.example.local:8080)没有';与request.base\u url不匹配(http://app.example.local)

Ruby on rails HTTP源报头(http://app.example.local:8080)没有';与request.base\u url不匹配(http://app.example.local),ruby-on-rails,reactjs,api,nginx,Ruby On Rails,Reactjs,Api,Nginx,我正在创建一个包含两部分的应用程序: 后端(使用Rails 6) 前端(带React) 因此,我在本地设置nginx服务器进行代理 但是,当我尝试执行POST请求时,出现了以下错误: HTTP Origin header (http://app.example.local:8080) didn't match request.base_url (http://app.example.local) nginx主机(app.example.local)具有以下配置: server { li

我正在创建一个包含两部分的应用程序:

  • 后端(使用Rails 6)
  • 前端(带React)
因此,我在本地设置nginx服务器进行代理

但是,当我尝试执行POST请求时,出现了以下错误:

HTTP Origin header (http://app.example.local:8080) didn't match request.base_url (http://app.example.local)
nginx主机(app.example.local)具有以下配置:

server {
  listen 8080;
  listen [::]:8080;

  server_name app.example.local;

  access_log /usr/local/var/log/nginx/app.example.local_access.log;
  error_log /usr/local/var/log/nginx/app.example.local_error.log;

  location / {
    proxy_set_header Host $host;
    #proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-Host $host;

    proxy_pass http://127.0.1:3001;

    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  location /api {
    proxy_pass http://app.example.local:3000;

    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-Host $host;

    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}
http://app.example.com:8080/api/posts例如,
,工作正常。 但是当我尝试向
POST请求时http://app.example.com:8080/api/posts
我收到以下错误:

Started POST "/api/posts" for 127.0.0.1 at 2020-07-29 15:20:11 +0100
Processing by API::PostsController#create as JSON
  Parameters: {"post"=>{"title"=>"Demo 2", "content"=>"Post Demo 2"}}
HTTP Origin header (http://app.example.local:8080) didn't match request.base_url (http://app.example.local)
Completed 422 Unprocessable Entity in 0ms (ActiveRecord: 0.0ms | Allocations: 432)


  
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  
actionpack (6.0.3.2) lib/action_controller/metal/request_forgery_protection.rb:215:in `handle_unverified_request'
...
我像这样设置后端COR:

    allow do
      origins %r{https?://(.*?)\.example\.local:8080$}

      resource '*',
        headers: :any,
        methods: %i[get post put patch delete options head],
        credentials: true
    end
我正在正确发送CSRF_令牌

我已经在这里看到了类似的错误,但解决方案并不能解决我的问题。大多数问题都是由SSL引起的。但由于我没有使用SSL,这不应该是原因


我缺少什么?

您的
API::PostsController
从哪个控制器继承?使用
ActionController::API
时,默认情况下会禁用一些检查。我有一个类似的设置,回想起来,使用
ApplicationController
ActionController::Base
,我遇到了一些问题。但是不能说是否发生了相同的错误。@Archer my
API::PostsController
继承自
API::BaseController
,继承自
ApplicationController
,继承自
ActionController::Base
,可能是nginx错误配置,这可能很常见,请参阅此问题,您的
API::PostsController
从哪个控制器继承?使用
ActionController::API
时,默认情况下会禁用一些检查。我有一个类似的设置,回想起来,使用
ApplicationController
ActionController::Base
,我遇到了一些问题。但是不能说是否发生了相同的错误。@Archer my
API::PostsController
继承自
API::BaseController
,继承自
ApplicationController
,继承自
ActionController::Base
,可能是nginx错误配置,这可能很常见,请参阅此问题,