Url rewriting 在NGINX中将url重写为子域
我目前有一个图像由服务器端PHP以如下url输出: domain.com/m/image.jpg 我想在url上查看此图像: i.domain.com/image.jpg 在nginx conf中是否可以这样做 注意:我当前已将我的“I”子域重新映射到/images/文件夹。 此外,我目前正在提供静态图像,如下所示:Url rewriting 在NGINX中将url重写为子域,url-rewriting,nginx,uri,Url Rewriting,Nginx,Uri,我目前有一个图像由服务器端PHP以如下url输出: 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.dom
http://i.domain.com/simage.jpg (thumb)
http://i.domain.com/image.jpg (medium)
http://i.domain.com/oimage.jpg (full quality)
这是我的domain.conf文件:
此外,在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/
只是一个psuedo目录
有鉴于此,我认为应该简单到:
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%确定。试试看。我将try移到下面,因为它首先尝试的是不存在的文件,然后出错
更新 由于您正试图将其传递给脚本,因此我们需要捕获它并将其传递给脚本。我更改了根目录,因为您正在执行“psuedo”图像目录,我们需要访问图像处理脚本
OK,因为中间没有PHP脚本(在我的部分中读错了),那么下面应该做你想做的事情。p>
#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;
}
}
如果这样做行得通,我不确定如何为small/orig/medium进行重写(因为我不确定所需的逻辑),但希望您能让它正常工作
在域配置中,您只需要为
/m
location /m {
rewrite ^/m/(.*)$ http://i.domain.com/$1 permanent;
}
应该这样做,等待任何小错误 更新2: 好的,所以实际的文件在
/images/size/image.jpg
中,而不是实际上的/m/
,/m/
只是一个psuedo目录
有鉴于此,我认为应该简单到:
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%确定。试试看。我将try移到下面,因为它首先尝试的是不存在的文件,然后出错
更新 由于您正试图将其传递给脚本,因此我们需要捕获它并将其传递给脚本。我更改了根目录,因为您正在执行“psuedo”图像目录,我们需要访问图像处理脚本
OK,因为中间没有PHP脚本(在我的部分中读错了),那么下面应该做你想做的事情。p>
#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;
}
}
如果这样做行得通,我不确定如何为small/orig/medium进行重写(因为我不确定所需的逻辑),但希望您能让它正常工作
在域配置中,您只需要为
/m
location /m {
rewrite ^/m/(.*)$ http://i.domain.com/$1 permanent;
}
应该这样做,等待任何小错误 @eeek不确定你做错了什么,我只是在我的设置中尝试了一下,效果如期。如果
http://i.domain.com/image.jpg
不存在,则会出现404错误。因此,检查一下,还要确保您在更改后正在重新加载或重新启动nginx,还要确保您更改了domain.com
它除了domain.com/m/image.jpg(这是一个为图像服务的php脚本)之外,其他任何地方都不存在,我如何才能在i.domain.com/image.jpg上查看它?@eeek更新了答案。我会检查您的php脚本中是否存在该图像,如果不提供notfound.jpg图像,它看起来像是在“I”子域上查找php脚本?如果它不存在,那么它只在主域上。它应该访问它,您可能需要将原始服务器配置中的php/fastcgi位置添加到这个位置,它才能工作。我更新了服务器配置,在出现任何错误之前应该可以正常工作,但基本上它允许使用子域传递到images_脚本。@eeek不确定你做错了什么,我只是在我的安装程序上尝试了一下,它按预期工作。如果http://i.domain.com/image.jpg
不存在,则会出现404错误。因此,检查一下,还要确保您在更改后正在重新加载或重新启动nginx,还要确保您更改了domain.com
它除了domain.com/m/image.jpg(这是一个为图像服务的php脚本)之外,其他任何地方都不存在,我如何才能在i.domain.com/image.jpg上查看它?@eeek更新了答案。我会检查您的php脚本中是否存在该图像,如果不提供notfound.jpg图像,它看起来像是在“I”子域上查找php脚本?如果它不存在,那么它只在主域上。它应该访问它,您可能需要将原始服务器配置中的php/fastcgi位置添加到这个位置,它才能工作。我更新了服务器配置,在出现任何错误之前应该可以正常工作,但基本上它允许使用子域传递到images\u脚本。