在NGINX中将url重写为子域

时间:2012-02-13 18:30:38

标签: url-rewriting nginx uri

我目前有一个图像由服务器端PHP输出,如下所示:

domain.com/m/image.jpg

我想在网址上查看此图片:

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

}

第三次重写是我要用非静态服务图像文件替换的那个,任何想法如何去做?

1 个答案:

答案 0 :(得分:0)

更新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%肯定。给那个镜头看看。我移动了下面的尝试,因为它正在尝试不存在的文件,先是然后错误。


<强>更新

由于您试图将其传递给脚本,我们需要捕获它并将其传递给脚本。我更改了根目录,因为您正在执行“伪”图像目录,我们需要访问图像处理脚本。

好的,因为中间没有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 /媒体进行重写(因为我不确定所需的逻辑),但希望你可以让它工作。


在您的域名配置中,您只需添加/m

的位置即可
location /m {
    rewrite ^/m/(.*)$ http://i.domain.com/$1 permanent;
}

应该这样做,等待任何小错误。

相关问题