繁体   English   中英

反向代理和Rails应用程序相对路径

[英]Reverse Proxy and Rails App Relative Path

我需要创建一个使用反向代理Rails应用程序 ,并能够记录所有请求。 请求是到远程站点的,但域必须始终是我的应用程序所在的位置。 我已经尝试过诸如rack-reverse-proxyrails-reverse-proxy之类的项目,但相对链接对我不起作用。 全部都指向域的根,而不是指向发出请求的地址。 例如,如果我的应用程序位于/ blog /中,则受测代理的响应始终转到/ ,并且页面显示错误。

有谁知道我该如何解决这个问题?

最重要的是,域始终保持不变,并且可以记录请求

嘿,如果您使用的是nginx,puma。 请按照以下步骤操作。
1.在config.ru文件中

map DemoApp::Application.config.relative_url_root || "/" do
run Rails.application
end


2.在config / environments / production.rb中添加资产主机和站点主机。

asset_host = ENV['SITE_HOST']
asset_host += ENV['APP_SUBURI'] if ENV['APP_SUBURI'].present?
config.action_controller.asset_host = asset_host
config.action_mailer.default_url_options = {
    :host => ENV['SITE_HOST'] 
}


3.在config / initializers / config.rb中

#App sub uri for rails app
if ENV['APP_SUBURI'].present?
  APP_SUB_URi = ENV['APP_SUBURI']
  SITE_URL = ENV['SITE_HOST'] + ENV['APP_SUBURI']
elsif ENV['SCRIPT_NAME'].present?
  APP_SUB_URi = ENV['SCRIPT_NAME']
  SITE_URL = ENV['SITE_HOST'] + ENV['SCRIPT_NAME']
end
#if Puma Server
if ENV['PASSENGER'].blank? && (ENV['RAILS_ENV'] == 'production' || ENV['RAILS_ENV'] == 'staging')
  DemoApp::Application.config.relative_url_root = APP_SUB_URi
end


4.设置环境变量

export SITE_HOST=localhost
export SCRIPT_NAME=/blog
export APP_SUBURI=/blog 


5.更改nginx配置文件。

upstream puma_main {
server unix:/var/www/demo_app/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/change_with_your_root_app;
index index.html index.htm;
server_name localhost server_name;
location / {
    try_files $uri $uri/ =404;
}
location /blog {
    alias /var/www/demo_app/public;
    try_files $uri @main;
}
location @main {
    proxy_http_version 1.1;
    chunked_transfer_encoding off;
    proxy_buffering off;
    proxy_cache off;
    proxy_redirect     off;
    proxy_set_header   Host             $http_host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_pass http://puma_main;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/www;
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM