Nginx-Reverse-Proxy und fügen Sie ein Verzeichnis hinzu
天蓬老师
天蓬老师 2017-05-16 17:21:03
0
2
373

Zum Beispiel möchte ich über 127.0.0.1/play/ auf 127.0.0.1:9000 zugreifen.
Meine aktuelle Konfiguration ist wie folgt:

location / {
        root   F:\Personal\ck;
        index  index.html;
}
location ~ ^/play/ {
        proxy_pass   http://127.0.0.1:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
}

Als ich tatsächlich auf 127.0.0.1/play/ zugegriffen habe, war ich sehr verwirrt. Hat es nicht direkt auf den 9000-Port zugegriffen, sondern den Verzeichnisnamen am Ende angegeben?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(2)
阿神

因为你请求了 /play/ 而这个请求给pass到了 http://127.0.0.1:9000. 这个请求的path同样也会pass过去。

你想要的可以实现,

location ~ ^/play(/?)(.*){
    proxy_pass http://127.0.0.1:9000/$;
}

这个代码的意思是,把 /play/xx 的请求 给我pass 到 http://127.0.0.1:9000/xx

更好的方式

location /play {
        proxy_pass   http://127.0.0.1:9000/;
}

注意结尾的'/'

看这里
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

滿天的星座

因为proxy_pass只是反向代理,没法重写URL规则,它只是修改主机名而已。
想去掉后面的东西,你得用rewrite

location ~^/play/ {
    proxy_pass    http://127.0.0.1:9000;
    rewrite       "^/play(.*)$" $1 break;
    ...
    }
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!