前言:有时前后端分离时,前后端用一个域名,这时就希望把后端的服务设置成二级目录

一.实际例子:

http://yourdomain.com/admin/system/captcha/get  反向到http://127.0.0.1:4000/admin/system/captcha/get 

nginx配置

location /admin/ {
    proxy_pass http://127.0.0.1:4000;  # ❗末尾不带斜杠
    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_set_header X-Forwarded-Proto $scheme;
}

注意:proxy_pass http://127.0.0.1:4000;  # ❗末尾不带斜杠 一定不要带斜杠,如果带上->会去掉admin后端就是http://127.0.0.1:4000/system/captcha/get ,这样就找不到对应地址报错,就会出错。

二。如果知道是否走了二级域名。

location /admin/ {
    return 200 "admin location hit\n";
    add_header Content-Type text/plain;
}

访问如果返回 200200 "admin location hit\n" ,代表走了二级目录

更多推荐