我有一个带有节点后端的react前端,但我的nginx设置目前无法工作。我想做的是将< code > https://sales . example . com/api/stats/get _ customer _ count/2323232 重定向到< code > http://127.0.0.1:3000/stats/get _ customer _ count/2323232 (节点后端服务器运行在http://127 . 0 . 0 . 1:3000上),但我一直收到404错误,指出找不到API路径。不知道如何着手解决这个问题。感激不尽。谢谢
server {
root "/home/sales/frontend/dist";
index index.html index.htm;
server_name sales.example.com;
error_log /var/log/nginx/error.log;
listen 80;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location ~* ^/api/ {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
这个好像在这里已经回答过了,@Dayo写的解释不错。
将其转换到您的示例中,看起来是这样的:(注意,主要区别是代理传递中的结尾斜杠)
location ~* ^/api/ {
proxy_pass http://127.0.0.1:3000/;
}
但是请在抄写之前通读链接的答案。