2015-06-10 109 views
2

要使用Nginx的X-Accel-Redirect功能與乘客,顯然你使用passenger_set_header,如果映射到另一個位置passenger_set_cgi_param。舉例來說,這裏是一個configuration which apparently used to work for someone else乘客5 Nginx X-Accel-Redirect

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的該passenger_set_cgi_param選項已被刪除,取而代之的是passenger_set_headerpassenger_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; 
    } 
+0

如果您正在閱讀本文,我想還是沒有答案。我最終使用uwsgi而不是乘客,很大程度上是因爲這個問題。很棒! – Purrell

回答

6

看起來你找到了一個替代的解決方案,而張貼在此情況下,其他人遇到同樣的問題,並希望從乘客4語法與X-加速重定向一起升級到乘員5。

以下是爲我工作的變化:

乘客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; 
} 

此外,還有一個符號鏈接到Rails應用程序在/var/www/shared,ln -s /path/to/railsapp/public /var/www/shared/storage,但是這可以根據你的nginx配置不同。

希望這會有所幫助!

+0

非常希望它可以幫助別人! – Purrell

+0

關於此解決方案(我使用Rails 5)的唯一問題是現在我無法再利用ETag頭。顯然,Rails將etag頭文件發送回Nginx之前轉發接力棒。有任何想法嗎? – leandroico

+0

我檢查了它,它似乎仍然爲我生成ETags。你在使用nginx> = 1.7.3嗎?當與gzip結合使用時,在此版本之前[Nginx剝離ETags](http://stackoverflow.com/questions/31125888/nginx-missing-etag-when-gzip-is-used)。 – remo