ruby-on-rails - 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, delete
s). 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;
}
解决方案
推荐阅读
- python - 如何在字典理解中读取字节
- git - 需要在没有远程的情况下共享本地 Git 存储库 - 删除所有凭据
- windows - 使用 wsl tmux ssh vim 复制/粘贴
- java - Spring Boot @Retryable maxAttempts 根据异常
- events - “登录”已注册,但未找到提供者,Keycloak 中的错误
- python - 在python中自动生成空列表
- unity3d - 在 2D Unity 图像中创建孔
- google-apps-script - 在 Google 脚本中使用 rtl
- kernel-module - LKM 如何解除对 vfs_read() 的阻塞?
- sql - postgres查询的想法