当网站的某个页面时,如何从不同的根提供图像。 nginx

时间:2021-07-19 15:49:44

标签: regex image nginx

我对正则表达式没有太多经验,在 Nginx 上下文中也没有太多经验,所以这对我来说很困难。

我想要的是,当我在特定页面(而不是子文件夹)上时,服务器从不同的根获取图像。

我目前处理网站所有页面中的图像的配置是这个。

location ~* \.(png|jpg|jpeg|gif) {
        root /usr/share/nginx/nginxTestPHP/media;
    }

我希望当我在页面 example.com/My-Profile 中时,服务器处理来自 /usr/share/nginx/nginxTestPHP/media/ 的图像上传,我的想法是这样的,如果有道理的话。

location ~* /My-Profile/*\.(png|jpg|jpeg|gif) {
        root /usr/share/nginx/nginxTestPHP/media/uploads;
    }

显然这一个不起作用,否则我不会在这里问这个问题,所以对于那里的正则表达式专家,这个解决方案是什么?

此外,我将如何在同一个正则表达式的 2 个不同页面中应用它,例如 (My-Profile|Configuration) 是我的想法。

我的 Nginx 配置

server {
    listen       81;
    listen  [::]:81;
    server_name  IP;

    client_max_body_size 4M;

    charset UTF-8;

    include /etc/nginx/mime.types;

    error_log /var/log/nginx/error_log warn;
    access_log /var/log/nginx/access_log main;

        rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
        rewrite ^/(.*)/$ /$1 permanent;

        root /usr/share/nginx/nginxTestPHP/PHP;

        index index.html index.htm Inicio.php Index.php;

        location / {
                try_files $uri/index.html $uri.html $uri/ @extensionless-php;
        }

        location @extensionless-php {
            rewrite ^(.*)$ $1.php last;
        }

        # pass the PHP scripts to FastCGI
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_intercept_errors on;
                fastcgi_pass app:9000;
                fastcgi_index Inicio.php;
                fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
                include fastcgi_params;
        }

        location ^~ /vendor/ {
                deny all;
                return 403;
        }

        # deny access to .htaccess files
        location ~ /\.ht {
                deny all;
        }

location ^~ /en/ {
    try_files $uri/.php $uri.php $uri/ @rewrite;
    fastcgi_intercept_errors on;
    fastcgi_pass app:9000;
    fastcgi_index Index.php;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    include fastcgi_params;
    }

location ^~ /Blog/ {
    try_files $uri/.php $uri.php $uri/ @rewrite;
    fastcgi_intercept_errors on;
    fastcgi_pass app:9000;
    fastcgi_index Index.php;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    include fastcgi_params;
    }


    location @rewrite {
        rewrite ^/(en|Blog)(/.*\.(png|jpg|svg|ico))$ $2 last;
        return 404;
    }


    location ~ \.css {
        root /usr/share/nginx/nginxTestPHP/CSS;
        default_type text/css;
        add_header  Content-Type    text/css;
    }

    location ~ \.js {
        root /usr/share/nginx/nginxTestPHP/JavaScript;
        default_type text/javascript;
        add_header  Content-Type    application/x-javascript;
    }

    location ~ \.txt {
        root /usr/share/nginx/nginxTestPHP/PHP;
    }
    
    location ~* ^/Mi-Perfil/([^/]+\.(png|jpg|jpeg|gif))$ {
        alias /usr/share/nginx/nginxTestPHP/media/uploads/$1;
    }

    location ~* \.(png|jpg|svg|ico|jpeg|gif) {
        root /usr/share/nginx/nginxTestPHP/media;
    }

    error_page  405     =200 $uri;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/nginxTestPHP/PHP;
        try_files $uri /Inicio.php;
    }

}

2 个答案:

答案 0 :(得分:0)

这里对 Nginx 的经验很少,但正则表达式部分看起来修复起来相对简单 - 尝试使用:

location ~* /My-Profile/.+\.(png|jpg|jpeg|gif)$ {
    root /usr/share/nginx/nginxTestPHP/media/uploads;
}

注意 .+ 之后的 My-Profile/ 和字符串结尾锚点 ($) - 我假设您只想匹配图像,而不是像 {{1} 这样的 URI }}。

从我所做的小研究来看,您可能还想查看 nginx 映射,尤其是如果您希望对所有遵循相同 URI 模式的多个个人资料页面执行此操作。

答案 1 :(得分:0)

  1. 您的正则表达式已损坏,它是 PCRE 模式中的一个点 (.),用作通配符,而不是星号。如果要匹配除 / 之外的任何非零字符数,可以使用 [^/]+ 正则表达式模式:

    location ~* /My-Profile/[^/]+\.(png|jpg|jpeg|gif)$ { ... }
    

    或确保 URI 以 /My-Profile/ 开头,添加 ^ 锚点(匹配字符串的开头):

    location ~* ^/My-Profile/[^/]+\.(png|jpg|jpeg|gif)$ { ... }
    
  2. 检查 rootalias nginx 指令之间的区别。使用您的配置,nginx 将搜索 /usr/share/nginx/nginxTestPHP/media/uploads/My-Profile 文件夹中的文件。改用 alias 指令:

    location ~* ^/My-Profile/[^/]+\.(png|jpg|jpeg|gif)$ {
        alias /usr/share/nginx/nginxTestPHP/media/uploads;
    }
    

更新

我想我犯了一个严重的错误。由于这是一个正则表达式匹配位置,因此 alias 指令参数应包含图像文件的完整路径 with 文件名:

location ~* ^/My-Profile/([^/]+\.(png|jpg|jpeg|gif))$ {
    alias /usr/share/nginx/nginxTestPHP/media/uploads/$1;
}
相关问题