Usage of nginx rewrite

Case:

In the previous server, the image path address was as follows:

/convention2/upload/image/20160815185505_274_2016-08-15_18-36-13.png

convention2 is the project name of the previous java web

The server is now migrated to

http://upload.yhskyc.com/

 

simply say:

i need to put

http://i.yhskyc.com/convention2/upload/image/2aa.png

maps to:

http://upload.yhskyc.com:8084/convention2/upload/image/2aa.png

In the nginx configuration file i.yhskyc.com.conf add:

if ( $uri ~* "^/convention./upload/" ) {
                rewrite ^/[^/]+/(.*)$ http://upload.yhskyc.com:8084/convention2/$1 redirect;
        }

Explanation :

The first line: "^/convention./upload/" is a regular expression, for example, the following paths all match:

/convention1/upload/image/2aa.png

/convention2/upload/image/2aa.png

/conventiona/upload/image/2aa.png

The following mismatches:

/convention/upload/image/2aa.png

/convention22/upload/image/2aa.png

 

^/ [^/]+ / (.*) $ on the second line is also a regex

The red part matches " http://i.yhskyc.com/convention2/upload/image/2aa.png "

convention2,

The green part matches "http://i.yhskyc.com/convention2/upload/image/2aa.png"

upload/image/2aa.png

will replace $1 in " http://upload.yhskyc.com:8084/convention2/ $1 ", so the result is:

http://upload.yhskyc.com:8084/convention2/upload/image/2aa.png ( this is the result )

Using rewrite, the browser address will jump ,

Our input is: http://i.yhskyc.com/convention2/upload/image/2aa.png,

But the browser will eventually jump to http://upload.yhskyc.com:8084/convention2/ upload/image/2aa.png ,

 

Syntax :

~ is a case-sensitive match
~* is a case-insensitive match
! and !* are a case-sensitive mismatch and a case-insensitive mismatch, respectively

refer to:

https://moonbingbing.gitbooks.io/openresty-best-practices/ngx/nginx_local_pcre.html

http://nginx.org/en/docs/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026726&siteId=291194637