简体   繁体   中英

Rewrite url to sub domain in NGINX

I currently have a image being outputted by server side PHP at a url like this:

domain.com/m/image.jpg

I'd like to have this image viewable at the url:

i.domain.com/image.jpg

Is this possible in the nginx conf?

Note: I currently have my "i" subdomain remapped to a /images/ folder. 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

Also within nginx.conf I have this for the subdomain currently:

    #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:

Ok, so the actual files are in /images/size/image.jpg and not actually /m/ , /m/ is just a psuedo directory.

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. 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.

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.

#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.


In your domain conf, you just need to add a location for /m

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

Should do it, pending any minor mistakes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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