首页 > 解决方案 > 当网站的某个页面时,如何从不同的根目录提供图像。Nginx

问题描述

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

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

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

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

我希望当我在页面example.com/My-Profile中时服务器处理来自/usr/share/nginx/nginxTestPHP/media/uploads的图像,我的想法是这样的,如果它使任何感觉。

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

}

标签: regeximagenginx

解决方案


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

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

请注意字符串.+后面My-Profile/的锚点 ( $) - 我假设您只想匹配图像,而不是像stackoverflow.com/test.png/somepage.

从我所做的小研究中,您可能还想查看 nginx 映射,特别是如果您希望为多个配置文件页面执行此操作,所有这些页面都遵循相同的 URI 模式。


推荐阅读