问题原文
原帖链接 · WoooHaaaa · CC BY-SA 4.0
What I want to do is: When someone visits http://localhost/route/abc the server responds exactly the same as http://localhost:9000/abc
Now I configure my Nginx server like this:
location /route {
proxy_pass http://127.0.0.1:9000;
}
The HTTP request is dispatched to port 9000 correctly, but the path it receives is http://localhost:9000/route/abc not http://localhost:9000/abc.
Any suggestions?
社区高分解答
原帖链接 · user2428107 · CC BY-SA 3.0
I believe you can use rewrite to remove the extra part of the URL. In your case I think you could use:
location /route/ {
rewrite ^/route/?(.*)$ /$1 break;
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;
}
However if your app has internal links in it, they may still point to /abc/foo , and if you do this they instead need to point to /route/abc/foo so that the raw request comes in correctly. You may be better off leaving the nginx config as it is and instead configuring your app to be aware it lives at a subdirectory, if you can.
I know this is an old question, but it was the top google hit for me when I was trying to solve the same issue!
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
