简体   繁体   中英

Passing parameters to nginx url

How to pass parameters in nginx url. When I hit http://127.0.0.1:1000/samp/test1/result?num1=10&num2=30 , it should redirect me to http://127.0.0.1:80/samp/test1/result?num1=10&num2=30 . Is this possible? Below is my nginx config file.

upstream apache {
server 127.0.0.1:1000;
}
server {
listen       80;
server_name  127.0.0.1;
location / {
    #root   html;
    #index  index.html index.htm;
    #return 503;
    proxy_pass          http://apache;
}
}

I think what you want to do is possibe, but you'd have to make that configuration change on the apache end of things.

If 'apache' handles a request coming into port 1000 directly, it will see a URI like this:

http://127.0.0.1:1000/samp/test1/result?num1=10&num2=30

However, if it is sent through nginx, it will look more like this:

http://apache/samp/test1/result?num1=10&num2=30

So, you could check the incoming URL for :1000 and then rewrite the request on the apache side to go to port 80 instead ( which is the default, so you don't need :80 - you can just leave the port unspecified entirely.

If the backend really is apache, you can use a rewrite rule there to handle the rewriting.

However, if you're not already on port 80, you're not connecting to nginx - so nginx can't rewrite this for you.

Hope it makes sense!

Here's how I tested:

Apache side I used a quick sinatra (ruby) app to print out the full URI of the request it sees:

require 'sinatra'

set :bind, "0.0.0.0"
set :port, 1025

get "/*" do
"<p>Hello from \"apache\"!  You've just requested:
<code>#{request.url}</code></p>
"
end

Then nginx is configured thusly:

upstream apache {
    server 192.168.70.1:1025;
}

server {
    server_name localhost;
    location / {
        proxy_pass http://apache;
    }

}

Note I used port 1025, because port 1000 is a privileged port.

I used curl to generate the requests to test:

$ curl 'http://192.168.70.1:1025/samp/test1/result?num1=10&num2=30'
<p>Hello from "apache"!  You've just requested:
<code>http://192.168.70.1:1025/samp/test1/result?num1=10&num2=30</code></p>
$curl 'http://127.0.0.1:80/samp/test1/result?num1=10&num2=30'
<p>Hello from "apache"!  You've just requested:
<code>http://apache/samp/test1/result?num1=10&num2=30</code></p>

If I wanted to do the redirect you're describing, I could match with a regular expression of IPV4 address and port and redirect as such:

get "/*" do

    if request.url =~ %r|^http://([0-9]{1,3}\.){3}[0-9]{1,3}:1025/|
        redirect "http://localhost:80/#{request.fullpath}"
    else
"<p>Hello from \"apache\"!  Tou've just requested:
<code>#{request.url}</code></p>
"
    end
end

Now I tell curl to follow redirects (-L) and we see it redirect me over to the "correct" route.

$ curl -L 'http://192.168.70.1:1025/samp/test1/result?num1=10&num2=30'
<p>Hello from "apache"!  Tou've just requested:
<code>http://apache/samp/test1/result?num1=10&num2=30</code></p>

I know it's not the language you're using, but I hope it iwll help you get started.

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