nginx reverse proxy and add a directory
天蓬老师
天蓬老师 2017-05-16 17:21:03
0
2
420

For example, I want to access 127.0.0.1:9000 through 127.0.0.1/play/
My current configuration is as follows:

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

When I actually accessed 127.0.0.1/play/, it accessed 127.0.0.1:9000/play/. I was very confused. Didn't it directly access the 9000 port, but put the directory name behind it?

天蓬老师
天蓬老师

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

reply all(2)
阿神

Because you requested /play/ and this request was passed to http://127.0.0.1:9000. The path of this request will also be passed.

What you want can come true,

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

The meaning of this code is to pass the request for /play/xx to http://127.0.0.1:9000/xx

Better way

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

Pay attention to the '/' at the end

Look here
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

滿天的星座

Because proxy_pass is just a reverse proxy, it cannot rewrite URL rules. It only changes the host name.
If you want to remove the last thing, you have to use rewrite

location ~^/play/ {
    proxy_pass    http://127.0.0.1:9000;
    rewrite       "^/play(.*)$"  break;
    ...
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template