简体   繁体   English

将网址重写为NGINX中的子域

[英]Rewrite url to sub domain in NGINX

I currently have a image being outputted by server side PHP at a url like this: 我目前有一个图像由服务器端PHP输出,如下所示:

domain.com/m/image.jpg domain.com/m/image.jpg

I'd like to have this image viewable at the url: 我想在url上查看此图像:

i.domain.com/image.jpg i.domain.com/image.jpg

Is this possible in the nginx conf? 在nginx conf中这可能吗?

Note: I currently have my "i" subdomain remapped to a /images/ folder. 注意:目前,我的“ i”子域已重新映射到/ images /文件夹。 Also I'm currently serving static images like the following: 另外,我目前正在提供静态图片,如下所示:

http://i.domain.com/simage.jpg (thumb)
http://i.domain.com/image.jpg (medium)
http://i.domain.com/oimage.jpg (full quality)

Here's my domain.conf file: http://pastebin.com/BBWUJFxu 这是我的domain.conf文件: http : //pastebin.com/BBWUJFxu

Also within nginx.conf I have this for the subdomain currently: 同样在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;
    }

}

The third rewrite is the one I'm looking to replace with my non static served image file, any idea how to go about this? 第三次重写是我要替换为非静态投放的图像文件的文件,您知道如何解决吗?

Update 2: 更新2:

Ok, so the actual files are in /images/size/image.jpg and not actually /m/ , /m/ is just a psuedo directory. 好的,因此实际文件位于/images/size/image.jpg而不是/m//m/只是一个伪目录。

Given that, I think it should be as simple as: 鉴于此,我认为它应该很简单:

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

}

But not 100% sure on it. 但不是100%可以肯定。 Give that a shot and see. 试一试,看看。 I moved the try below, as it was trying the files, which did not exist, first and then erroring out. 我将尝试下面的内容移到下面,因为它尝试的是不存在的文件,然后首先出错。


UPDATE 更新

Since you are trying to just pass it through to a script, we need to capture that and pass it through to the script. 由于您试图将其传递给脚本,因此我们需要捕获该信息并将其传递给脚本。 I changed the root, since you are doing a "psuedo" images directory and we will need access to the image processing script. 我更改了根目录,因为您正在执行“ psuedo”图像目录,因此我们需要访问图像处理脚本。

Ok, since there is no PHP script in the middle (mis-read on my part) then the below should do what you want it to. 好的,因为中间没有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;
    }

}

If that works, I am not sure how to go about doing the rewrites for the small / orig / medium (as I am not sure about the logic needed), but hopefully you can get it working. 如果这行得通,我不确定如何对小/ orig /介质进行重写(因为我不确定所需的逻辑),但是希望您能使它工作。


In your domain conf, you just need to add a location for /m 在您的域conf中,您只需为/m添加位置

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

Should do it, pending any minor mistakes. 应该这样做,等待任何小错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM