0

There are two scenario that I'm trying to achieve.

Scenario A : If client request URL that contains .jpeg or .jpg file, redirect the user to a single .jpg file that are on the server in this case myimage.jpg

Scenario B : If client request URL that contains /abc/ directory, redirect the user to other domain through proxy while keeping the URL in tact.

Below is the content of my nginx.conf

http {

    server {
        listen 80;
        root /usr/share/nginx/html;

        #Scenario A
        location ~* \.(jpg|jpeg){
           rewrite ^(.*) http://$server_name/myimage.jpg last;
        }

        #Scenario B
        location ^~ /abc/ {
            proxy_pass http://cd.mycontent.com.my;
            proxy_redirect localhost http://cd.mycontent.com.my;
            proxy_set_header Host $host;
            }
    }
......

Most of it I referred to Nginx redirect to a single file The config does not contain error in /var/log/nginx/error.log but it does not perform as intended to.

AsipAMN
  • 1
  • 1
  • 2

1 Answers1

0

If I understood you correctly the config should be

Scenario A

server {
    listen *:8090;
    server_name www.example.net;

    location =/test.png {
        root /vhosts/default/static;
    }

    location ~ \.(jpg|png|jpeg)$ {
        rewrite ^(.*) $scheme://$server_name:$server_port/test.png last;
    }
}

Basic tests

# curl -I http://www.example.net:8090/test.png
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Mon, 07 Mar 2016 09:23:05 GMT
Content-Type: image/png
Content-Length: 101667
Last-Modified: Fri, 19 Feb 2016 15:59:19 GMT
Connection: keep-alive
ETag: "56c73bd7-18d23"
Accept-Ranges: bytes

# curl -I http://www.example.net:8090/some_another.png
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.8.1
Date: Mon, 07 Mar 2016 09:23:28 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
Location: http://www.example.net:8090/test.png

Scenario B

server {
    listen *:8090;
    server_name www.example.net;

    location ~ /jenkins {
       proxy_pass http://bucket.s3.amazonaws.com;
    }
}

Basic test

# curl -I http://www.example.net:8090/jenkins/aws.png
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Mon, 07 Mar 2016 10:03:14 GMT
Content-Type: image/png
Content-Length: 16458
Connection: keep-alive
x-amz-id-2: zhHJFv/OuWaqTJ2+k1lTqoZO1Vlgt5JKFsvDP7u7ApIjzcvbruFQFcDw4Yrtz+NWyzLpaSOJqpc=
x-amz-request-id: 493B73E4048AB53A
Last-Modified: Fri, 04 Dec 2015 21:40:18 GMT
ETag: "2d6109836712d3f5425e5f2a1190f631"
Accept-Ranges: bytes
ALex_hha
  • 7,193
  • 1
  • 25
  • 40
  • I've followed the very basic of rewrite that you test using location =/test.png and point it to root /usr/share/nginx/html/myimage.jpg But it is not working. When requesting using my browser to the server ip x.x.x.x/test.png, the server return 404. – AsipAMN Mar 08 '16 at 02:53
  • The answer in the error.log – ALex_hha Mar 08 '16 at 08:11