首页 > 解决方案 > Rails reverse proxied by Nginx: change of origin problem

问题描述

I reverse proxied a full stack Rails app (port:3000) with Nginx(port:8080). Without Nginx, localhost:3000 works (I can get, post, patch, deletes). On localhost:8080, I can only get but I can't post, delete, patch. For example, an ajaxified 'update' method uses this snippet:

import { csrfToken } from "@rails/ujs";
const postGenreToResto = async (obj = {}) => {
  try {
    const response = await fetch("/updateGenre", {
      method: "PATCH",
      headers: {
        Accept: "application/json",
        "X-CSRF-Token": csrfToken(),
        "Content-Type": "application/json",
      },
      credentials: "same-origin",
      body: JSON.stringify(obj),
    });
    return await response.json();
  } catch (err) {
    console.log(err);
  }
};
export { postGenreToResto };

and my error is:

Started PATCH "/updateGenre" for 127.0.0.1 at 2020-10-07 17:04:04 +0200
Processing by RestosController#updateGenre as JSON
  Parameters: {Parameters: {"authenticity_token"=>"s+bpdFxhy2Sr...","genre_id"=>"7", "id"=>"1", "resto"=>{"id"=>"1", "genre_id"=>"7"}}
HTTP Origin header (http://localhost:8080) didn't match request.base_url (http://localhost)
Completed 500  in 2ms (ActiveRecord: 0.0ms | Allocations: 1787)
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

The csrf-token is ok. Could it be an Nginx settings problem, especially the headers?

---- EDITED ---

With the modified /etc/nginx/nginx.conf file below, it works. On localhost, when the service Nginx is running and listening to port:8080, I can post/patch/delete just like on localhost:3000 which Rails serves. I pushed this config on Heroku with a buildpack and it works as well.

The Nginx docs are not so easy to read and I am not sure I really understood what I did :)

upstream app_server {
    server 127.0.0.1:3000 fail_timeout=0; # version tcp/ip <=> port
    } 
    
    server {
       listen 8080;
       location / {
          try_files $uri @rails;  
       }
       location @rails {    
          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_set_header  Host $http_host; 
          proxy_redirect    off;    
          proxy_pass        http://app_server;  
       }

标签: ruby-on-railsnginx-reverse-proxy

解决方案


推荐阅读