簡體   English   中英

將參數傳遞給Nginx網址

[英]Passing parameters to nginx url

如何在Nginx網址中傳遞參數。 當我點擊http://127.0.0.1:1000/samp/test1/result?num1=10&num2=30 ,它應該將我重定向到http://127.0.0.1:80/samp/test1/result?num1=10&num2=30 這可能嗎? 以下是我的Nginx配置文件。

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;
}
}

我認為您想做的是可能的,但是您必須在apache末尾進行配置更改。

如果“ apache”直接處理進入端口1000的請求,它將看到如下所示的URI:

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

但是,如果通過nginx發送,它將看起來更像這樣:

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

因此,您可以檢查傳入的URL是否為:1000,然后在apache端重寫請求以轉至端口80(這是默認設置,因此您不需要:80您可以完全不指定端口。

如果后端確實是apache,則可以在此處使用重寫規則來處理重寫。

但是,如果您尚未使用端口80,則說明您未連接到nginx-因此nginx無法為您重寫此端口。

希望有道理!

這是我的測試方式:

Apache方面,我使用了一個快速的sinatra(ruby)應用程序來打印它看到的請求的完整URI:

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

然后,就這樣配置了nginx:

upstream apache {
    server 192.168.70.1:1025;
}

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

}

注意我使用端口1025,因為端口1000是特權端口。

我使用curl生成了測試請求:

$ 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>

如果我想執行您正在描述的重定向,則可以將其與IPV4地址和端口的正則表達式匹配,並進行如下重定向:

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

現在,我告訴curl遵循重定向(-L),我們看到它將我重定向到“正確”路由。

$ 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>

我知道這不是您使用的語言,但希望它能幫助您入門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM