Nginx 代理将 Nodejs 传递给 React 应用程序
Nginx Proxy Pass Nodejs to React Application
我有一个反应前端和节点后端,出于某种原因它不会向后端发出正确的请求。
nginx给出的错误日志
111: Connection refused) while connecting to upstream, server: _, request: "GET /api/info HTTP/1.1", upstream: "http://127.0.0.1:5000/info"
我注意到它发出了错误的请求,因为 http://127.0.0.1:5000/info
应该是 http://127.0.0.1:5000/api/info
我的默认配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/{{AppName}}/frontend/build;
server_name {{myDomainName}};
location / {
try_files $uri $uri/ =404;
}
location /api/ {
proxy_pass http://localhost:5000/;
}
当我访问我的网站时,它只是让我出错并显示错误 404
I noticed that it makes the wrong request because the
http://127.0.0.1:5000/info should be http://127.0.0.1:5000/api/info
动作
去掉代理地址末尾的'/'
location /api/ {
proxy_pass http://localhost:5000; # remove the '/' at the end
}
解释一下
To pass a request to an HTTP proxied server, the proxy_pass directive
is specified inside a location. For example:
location /some/path/ {
proxy_pass http://www.example.com/link/;
}
Note that in the first example above, the address of the proxied server is followed by a URI, /link/.
If the URI is specified along with the address, it replaces the part
of the request URI that matches the location parameter. For example,
here the request with the /some/path/page.html URI will be proxied to
http://www.example.com/link/page.html
在您的例子中,URI 是 /
,它替换了请求 URI 中的 /api/
。所以:
http://yourserver/api/info
将被代理到 http://127.0.0.1:5000/info
我有一个反应前端和节点后端,出于某种原因它不会向后端发出正确的请求。
nginx给出的错误日志
111: Connection refused) while connecting to upstream, server: _, request: "GET /api/info HTTP/1.1", upstream: "http://127.0.0.1:5000/info"
我注意到它发出了错误的请求,因为 http://127.0.0.1:5000/info
应该是 http://127.0.0.1:5000/api/info
我的默认配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/{{AppName}}/frontend/build;
server_name {{myDomainName}};
location / {
try_files $uri $uri/ =404;
}
location /api/ {
proxy_pass http://localhost:5000/;
}
当我访问我的网站时,它只是让我出错并显示错误 404
I noticed that it makes the wrong request because the http://127.0.0.1:5000/info should be http://127.0.0.1:5000/api/info
动作
去掉代理地址末尾的'/'
location /api/ {
proxy_pass http://localhost:5000; # remove the '/' at the end
}
解释一下
To pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location. For example:
location /some/path/ { proxy_pass http://www.example.com/link/; }
Note that in the first example above, the address of the proxied server is followed by a URI, /link/. If the URI is specified along with the address, it replaces the part of the request URI that matches the location parameter. For example, here the request with the /some/path/page.html URI will be proxied to http://www.example.com/link/page.html
在您的例子中,URI 是 /
,它替换了请求 URI 中的 /api/
。所以:
http://yourserver/api/info
将被代理到 http://127.0.0.1:5000/info