I am creating an app which has two parts:
- Backend (with Rails 6)
- FrontEnd (with React)
So, locally I setup nginx server for proxying.
But when I try to do a POST request, I got this error:
HTTP Origin header (http://app.example.local:8080) didn't match request.base_url (http://app.example.local)
The nginx host (app.example.local) has the following configuration:
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";
}
}
Making a GET request to http://app.example.com:8080/api/posts, for example, works fine.
But when I try a POST request to http://app.example.com:8080/api/posts I am getting the following error:
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'
...
I setup backend CORs like this:
allow do
origins %r{https?://(.*?)\.example\.local:8080$}
resource '*',
headers: :any,
methods: %i[get post put patch delete options head],
credentials: true
end
And I am sending correctly the CSRF_TOKEN.
I already saw similar error here but the solutions are not fixing my problem. Most of them the problem are being caused by SSL. But since I am not using SSL this should not be the cause.
What am I missing?