【问题标题】:NGINX: How to redirect from img/file.jpg to public/img/file.jpgNGINX:如何从 img/file.jpg 重定向到 public/img/file.jpg
【发布时间】:2022-01-20 17:48:34
【问题描述】:

我有一个这样的 nginx 配置:

server{
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/all-my-projects;

location / {
    try_files $uri $uri/ =404;
    autoindex on;
    autoindex_localtime on;
    autoindex_exact_size off;
}

location ~ \.php$ {
    fastcgi_pass $php_fastcgi_pass;
    fastcgi_index /index.php;
    include fastcgi_params;
    # regex to split $uri to $fastcgi_script_name and $fastcgi_path
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    try_files $fastcgi_script_name =404;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

# These are the locations I'm havin' trouble
location /project {
    try_files $uri @store-deprecated;
}
location @project {
    rewrite /project/(.*)$ /project/public/index.php?_url=/$1 last;
}
}

但是当我转到localhost/project 时,浏览器上的图像不会加载到任何视图上。 我知道当我转到 project 文件夹时我重写了,它重定向到 project/public/index.php 并运行该站点。

我把图片放在 html 中是这样的:

<img src="img/image.png">

如何重写public/img/ 文件夹以通过键入localhost/project/img/image.png 访问所有图像?

cssjs 文件夹以及 favicon.ico 也是如此

【问题讨论】:

    标签: nginx redirect nginx-config nginx-location public-folders


    【解决方案1】:
    server {
      rewrite ~ ^(/img/.*) /public$1 break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-20
      • 2020-10-06
      • 2018-10-14
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多