Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails nginx位置块理解和使用指定位置块的乘客_Ruby On Rails_Regex_Nginx_Passenger_Webp - Fatal编程技术网

Ruby on rails nginx位置块理解和使用指定位置块的乘客

Ruby on rails nginx位置块理解和使用指定位置块的乘客,ruby-on-rails,regex,nginx,passenger,webp,Ruby On Rails,Regex,Nginx,Passenger,Webp,关于我的nginx配置,我有几个问题,因为它涉及到服务webp文件以及使用带有try\u文件的命名位置 当前配置: server { listen 80; server_name assets.manager manager; passenger_app_env production; passenger_ruby /home/web-server/.rvm/gems/ruby-2.2.1@manager/wrappers/ruby; passenger

关于我的nginx配置,我有几个问题,因为它涉及到服务webp文件以及使用带有
try\u文件的命名位置

当前配置:

server {
    listen 80;
    server_name  assets.manager manager;
    passenger_app_env production;
    passenger_ruby /home/web-server/.rvm/gems/ruby-2.2.1@manager/wrappers/ruby;
    passenger_enabled on;

    error_log  /home/web-server/web-applications/manager/current/log/nginx-error.log;
    root       /home/web-server/web-applications/manager/current/public;

    satisfy any;
    allow 127.0.0.1;
    allow 192.168.0.0/24;
    deny all;

    location ~ ^/assets/ {
        gzip_static on;
        expires     max;
        add_header  Cache-Control public;
        add_header  Last-Modified "";
        add_header  ETag "";
    }

    location ~* .+\.(?:png|jpe?g|gif)$ {
        if ($webp_suffix != "") {
            add_header Vary Accept;
        }
        try_files $uri$webp_suffix $uri =404;
    }
}
目前,nginx不提供webp文件。如果我要放置
添加_头X-Webp-Uri“$Uri$Webp_后缀”
在第一个
位置
块中,添加了标题。如果我把它放在第二个png/jpeg匹配
location
块中,则不会设置标题。据我所知,正则表达式位置块是连续的(即,它不会在第一次匹配时停止匹配)

1) 如果存在,我想提供webp图像(我尝试的是第二个
位置
块)。在这种情况下,使用嵌套的
位置
块是否会有所帮助?我想为/assets/中的所有文件设置gzip_static、expires等,但我只想为/assets/中的某些文件扩展名提供webp版本

2) 关于另一个主题,我想提供静态的.html文件(如果有的话)。要做到这一点(在网上查找教程之后),我需要结合
try\u文件
和一个指向上游应用程序(Rails)的命名位置块。但是,如果我使用的是Passenger(使用
Passenger安装nginx模块安装),我似乎无法找到如何声明上游块。对于乘客/Nginx设置,我能找到的唯一配置是使用
Passenger\u enabled on


编辑:我找到了一个示例配置;下面是一个例子(这忽略了问题1):

编辑2:我发现nginx完全清除了
if
块之外的任何指令

这将导致仅设置
Vary Accept
标题:

server {
    location ~* ^/images/.+\.(png|jpe?g)$ {
        add_header X-Whatever "Yo";
        if ($webp_suffix != "") {
          add_header Vary Accept;
        }
    }
}
server {
    location ~* ^/images/.+\.(png|jpe?g)$ {
        if ($webp_suffix != "") {
          add_header Vary Accept;
          add_header X-Whatever "Yo";
        }
    }
}
这将导致设置两个标题:

server {
    location ~* ^/images/.+\.(png|jpe?g)$ {
        add_header X-Whatever "Yo";
        if ($webp_suffix != "") {
          add_header Vary Accept;
        }
    }
}
server {
    location ~* ^/images/.+\.(png|jpe?g)$ {
        if ($webp_suffix != "") {
          add_header Vary Accept;
          add_header X-Whatever "Yo";
        }
    }
}
编辑3:所以现在更让人困惑了。这就像之前的任何
add_header
一样,它不在最后一个代码块(location或if语句)内,完全被忽略。i、 例如,在以下情况下,仅设置X-Where标头:

location ~ ^/assets/ {
    gzip_static on;
    expires     max;
    add_header  Cache-Control public; # Ignored?!
    add_header  Last-Modified ""; # Ignored?!
    add_header  ETag ""; # Ignored?!

    location ~* ^/assets/.+\.(?:png|gif|jpe?g)$ {
        add_header X-Something "$uri$webp_suffix"; # Not ignored!
    }
}

我必须删除
Vary-Accept
标题条件并始终应用它。我不知道这是好是坏,但我没有任何其他选择考虑到它只是删除了我申请的每一个其他标题!我还必须将webp位置块移到资产块上方,并复制代码,这很糟糕

工作配置:

server {
    listen 80;
    server_name  assets.staging.pos staging.pos;
    passenger_app_env staging;
    passenger_ruby /home/vagrant/.rvm/gems/ruby-2.2.1@pos/wrappers/ruby;
    passenger_enabled on;

    error_log  /home/vagrant/rails/staging.pos/log/nginx-error.log;
    root       /home/vagrant/rails/staging.pos/public;

    # Try to return cached responses without hitting the app
    try_files $uri /cache/$uri /cache/$uri.html @app;

    location @app {
        proxy_set_header X-Forwarded-Proto http;
    }

    # Return webp images when possible
    location ~* ^/assets/.+\.(?:png|gif|jpe?g)$ {
        expires     max;
        add_header  Cache-Control public;
        add_header  Vary Accept;
        add_header  Last-Modified "";
        add_header  ETag "";

        try_files $uri$webp_suffix $uri =404;
    }

    # Regular asset headers
    location ~ ^/assets/ {
        gzip_static on;
        expires     max;
        add_header  Cache-Control public;
        add_header  Last-Modified "";
        add_header  ETag "";
    }
}