gpt4 book ai didi

ruby-on-rails - Passenger 5 Nginx X-Accel-Redirect

转载 作者:行者123 更新时间:2023-12-04 05:55:32 30 4
gpt4 key购买 nike

要对 passenger 使用 Nginx 的 X-Accel-Redirect 功能,显然您使用 passenger_set_header,如果映射到另一个位置,则使用 passenger_set_cgi_param。例如,这是一个 configuration which apparently used to work for someone else :

passenger_set_cgi_param HTTP_X_ACCEL_MAPPING "/home/user/rails_app/shared/files/=/documents/";
passenger_pass_header X-Accel-Redirect;

location ~ ^/documents/(\d\d\d)/(\d\d\d)/(\d\d\d)/(.*)$ {
alias /home/user/rails_app/shared/files/$1/$2/$3/$4;
internal;
}

但是对于乘客 5,他们在 release notes 中说:

[Nginx] The passenger_set_cgi_param option has been removed and replaced by passenger_set_header and passenger_env_var.

关于如何将两者一起用于 X-Accel-Redirect 的信息不多。似乎也没有最新的教程或博客展示如何做到这一点。这是怎么做到的?我可以获得以下 nginx.conf 来为 Rails 开发服务器(非乘客)工作,但它不适用于乘客。

upstream api_server {
server localhost:5000;
# (starting passenger with ``` RAILS_ENV=development passenger start -a 127.0.0.1 -p 5000 -d ```) not using unix:socket for a good reason
}

server {
listen 9000;
server_name $host;
return 301 https://$host:9443$request_uri;
#error_page 497 https://$host:9443$request_uri;
}

server {
charset UTF-8;

server_name localhost 0.0.0.0;

root /var/www/html/app;

listen 9443 ssl;
ssl on;
ssl_certificate /opt/nginx/conf/ssl/app.chain.pem;
ssl_certificate_key /opt/nginx/conf/ssl/app.key.pem;

error_page 497 https://$host:9443$request_uri;

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Expose-Headers' 'Content-Length, Content-Type, Keep-Alive, Date, Server, Transfer-Encoding, Cache-Control';
add_header 'Access-Control-Allow-Headers' 'Content-Length, Content-Type, Keep-Alive, Date, Server, Transfer-Encoding, Cache-Control';


passenger_env_var X-Sendfile-Type "X-Accel-Redirect";
passenger_env_var X-Accel-Mapping "/special/place/on/filesystem/=/protected_files/";
passenger_pass_header X-Accel-Redirect;
passenger_pass_header X-Sendfile-Type;


# --------- Serve static applications --------

location / {
try_files $uri $uri/ /index.html;
}


# --------- API --------

location /protected_files/{
# Used for X-Accel-Redirect
internal;
add_header Pragma "no-cache";
alias /special/place/on/filesystem/;
}

location ~ /(api|auth|raw)/ {

# Host + forwarding headers
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

passenger_pass_header Host;
passenger_pass_header X-Real-IP;
passenger_pass_header X-Forwarded-For;


# Configuration for X-Sendfile style fast & authenticated static serving
proxy_set_header X-Sendfile-Type X-Accel-Redirect;
# proxy_set_header X-Accel-Mapping /mounts/test_data_filesystem/=/protected_files/;
proxy_set_header X-Accel-Mapping /special/place/on/filesystem/=/protected_files/;

passenger_env_var X-Sendfile-Type "X-Accel-Redirect";
passenger_env_var X-Accel-Mapping "/special/place/on/filesystem/=/protected_files/";
passenger_pass_header X-Accel-Redirect;
passenger_pass_header X-Sendfile-Type;


proxy_pass http://api_server;
}

最佳答案

看起来您找到了替代解决方案,但发布此解决方案以防其他人遇到相同问题并希望从 Passenger 4 语法升级到 Passenger 5 以及 X-Accel-Redirect。

以下是对我有用的更改:

乘客 4 版本:

passenger_set_cgi_param HTTP_X_ACCEL_MAPPING /path/to/railsapp/public/=/storage/;
passenger_pass_header X-Accel-Redirect;

location /storage {
root /var/www/shared;
internal;
}

乘客 5 版本:

passenger_set_header X-Sendfile-Type "X-Accel-Redirect";
passenger_env_var HTTP_X_ACCEL_MAPPING /path/to/railsapp/public/=/storage/;
passenger_pass_header X-Accel-Redirect;

location /storage {
root /var/www/shared;
internal;
}

此外,/var/www/shared 中有指向 Rails 应用程序的符号链接(symbolic link),ln -s/path/to/railsapp/public/var/www/shared/storage ,但是根据您的 nginx 配置,这可能会有所不同。

希望这对您有所帮助!

关于ruby-on-rails - Passenger 5 Nginx X-Accel-Redirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30745339/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com