Home  >  Q&A  >  body text

Can nginx request an off-site address?

I am new to nginx. I have tried proxy_pass many times but cannot jump outside the site. Can nginx request external links?

Currently there is an external API interface address: http://122.23.21.212/api/v2?args=others&argc=4

Now I want to implement this function:

When I access http://localhost/myapi?args=others&args=4, nginx actually accesses the http://122.23.21.212/api/v2?args=others&argc=4 interface internally. Please tell me. How does this need to be configured?

shellserver {

    ...

    location /myapi {
        proxy_pass http://122.23.21.212/api/v2?$request_uri;
    }

    ...
}

Please help me, thank you

伊谢尔伦伊谢尔伦2734 days ago477

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-16 17:26:30

        server {
            ...
    
        location /myapi {
            if ( $query_string ~ "(args=others&args=4)$") {
                rewrite ^/ http://122.23.21.212/api/v2;
            }
        }
    
        ...
        }
    

    [fifilyu@archlinux ~]$ curl -I 'http://localhost/myapi?args=others&args=4'
    HTTP/1.1 302 Moved Temporarily
    Server: nginx/1.8.0
    Date: Wed, 06 May 2015 01:04:15 GMT
    Content-Type: text/html
    Content-Length: 160
    Connection: keep-alive
    Location: http://122.23.21.212/api/v2?args=others&args=

    reply
    0
  • Cancelreply