当网站的某个页面时,如何提供来自不同根的图像.Nginx



我对regex没有太多的经验,在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;
}

显然,这个不起作用,否则我不会在这里问这个问题,所以对于regex专业人士来说,这个的解决方案是什么?

此外,我将如何在同一regex中的两个不同页面中应用这一点,比如(我的配置文件|配置(就是我的想法。

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

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

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

    location ~* ^/My-Profile/[^/]+.(png|jpg|jpeg|gif)$ { ... }
    
  2. 检查rootaliasnginx指令之间的差异。使用您的配置,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指令参数应包含带有文件名的图像文件的完整路径:

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

这里使用Nginx的经验不多,但regex部分看起来相对简单,请尝试使用:

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

注意My-Profile/之后的.+和字符串末尾锚($(——我假设您只想匹配图像,而不是像stackoverflow.com/test.png/somepage这样的URI。

从我做的一点研究来看,你可能还想研究nginx映射,尤其是如果你想为多个遵循相同URI模式的配置文件页面做这件事。

最新更新