1

I am new at nginx and need help with redirect (basically I am a Java Dev), so here is what I need suggestions for : I have a url https://www.mysite.com/abc?server=10.2.2.10

I want nginx to check if server param is set then it should rewrite url to

https://10.2.2.10/abc

here is what I have tried but it isn't working:

location /abc/{  # I have also tried with /abc
    if ($arg_server != ""){
        rewrite https://$arg_server/$1 permanent;
    }
}

any suggestions please? Thank you for any helps guys

best Regards Sajid

4
  • by not working you mean doesn't do any thing or does it show an error ? Commented Mar 5, 2014 at 15:14
  • yes it does not do anything @MohammadAbuShady Commented Mar 5, 2014 at 15:22
  • It's really strange idea, to provide nginx a server name and have nginx redirect to that server. Why not just go to that server in first place? Commented Mar 5, 2014 at 16:57
  • actually first site is suppose to get data from 2nd one ...... Commented Mar 5, 2014 at 19:39

1 Answer 1

1

Actually the rewrite seems wrong, try replacing the rewrite line with this

if ($arg_server != ""){
    return 301 https://$arg_server/$1;
}

Adding a ^ to your rewrite probably would have worked but it's better to use return instead of rewrite here's why

Also I believe running a simple if($arg_server) would work without the != part, you can try it, Not sure though if it would work if it was empty string, like example.com?server=, if it does then just ignore what i said

Sign up to request clarification or add additional context in comments.

8 Comments

sure I am gonna give it a try right away Thank you :)
curl mysite.com/abc?server=someip -k <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>ngx_openresty/1.4.3.1</center> </body> </html> I get above when I use CURL but I do not get any request on the ip where nginx is suppose to redirect
probably because curl doesn't follow redirects, what does the browser do ? or try adding -L to curl
I guess it should be https://$arg_server$uri, because I don't see any regexp that fills $1.
@MohammadAbuShady basically I am using AJAX call here is what I get: XMLHttpRequest cannot load https://[new_ip_address]/abc. Origin mysite.com is not allowed by Access-Control-Allow-Origin. I know It can be handled by CORs but I was wondering if there is any other way around?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.