[英]Rewrite url to sub domain in NGINX
我目前有一个图像由服务器端PHP输出,如下所示:
domain.com/m/image.jpg
我想在url上查看此图像:
i.domain.com/image.jpg
在nginx conf中这可能吗?
注意:目前,我的“ i”子域已重新映射到/ images /文件夹。 另外,我目前正在提供静态图片,如下所示:
http://i.domain.com/simage.jpg (thumb)
http://i.domain.com/image.jpg (medium)
http://i.domain.com/oimage.jpg (full quality)
这是我的domain.conf文件: http : //pastebin.com/BBWUJFxu
同样在nginx.conf中,我目前有这个用于子域:
#setup subdomain i.domain.com
server {
server_name i.domain.com;
access_log /var/log/nginx/i.domain.com.access.log;
error_log /var/log/nginx/i.domain.com.error.log;
root /var/www/domain.com/html/images;
index index.php index.html index.htm;
location / {
#change this to a 404 img file .jpg
try_files $uri $uri/ /notfound.jpg;
rewrite "/s([A-Za-z0-9.]+)?" /small/$1 break;
rewrite "/o([A-Za-z0-9.]+)?" /orig/$1 break;
rewrite "/([A-Za-z0-9.]+)?" /medium/$1 break;
}
location = / {
rewrite ^ http://domain.com permanent;
}
}
第三次重写是我要替换为非静态投放的图像文件的文件,您知道如何解决吗?
更新2:
好的,因此实际文件位于/images/size/image.jpg
而不是/m/
, /m/
只是一个伪目录。
鉴于此,我认为它应该很简单:
server {
server_name i.domain.com;
access_log /var/log/nginx/i.domain.com.access.log;
error_log /var/log/nginx/i.domain.com.error.log;
root /var/www/domain.com/html/images;
location / {
rewrite /s([A-Za-z0-9.]+)? /small/$1 break;
rewrite /o([A-Za-z0-9.]+)? /orig/$1 break;
rewrite /([A-Za-z0-9.]+)? /medium/$1 break;
#change this to a 404 img file .jpg
try_files $uri $uri/ /notfound.jpg;
}
location = / {
rewrite ^ http://domain.com permanent;
}
}
但不是100%可以肯定。 试一试,看看。 我将尝试下面的内容移到下面,因为它尝试的是不存在的文件,然后首先出错。
更新
由于您试图将其传递给脚本,因此我们需要捕获该信息并将其传递给脚本。 我更改了根目录,因为您正在执行“ psuedo”图像目录,因此我们需要访问图像处理脚本。
好的,因为中间没有PHP脚本(我读错了),所以下面的代码应该可以实现您想要的。
#setup subdomain i.domain.com
server {
server_name i.domain.com;
access_log /var/log/nginx/i.domain.com.access.log;
error_log /var/log/nginx/i.domain.com.error.log;
root /var/www/domain.com/html;
location ~* \.jpg { # add more extensions if you need to
#change this to a 404 img file .jpg
try_files $uri $uri/ /m/$uri /m/notfound.jpg;
#get the main part working first
#rewrite "/s([A-Za-z0-9.]+)?" /small/$1 break;
#rewrite "/o([A-Za-z0-9.]+)?" /orig/$1 break;
#rewrite "/([A-Za-z0-9.]+)?" /medium/$1 break;
}
location = / {
rewrite ^ http://domain.com permanent;
}
}
如果这行得通,我不确定如何对小/ orig /介质进行重写(因为我不确定所需的逻辑),但是希望您能使它工作。
在您的域conf中,您只需为/m
添加位置
location /m {
rewrite ^/m/(.*)$ http://i.domain.com/$1 permanent;
}
应该这样做,等待任何小错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.