我已经部署了我的react应用程序,它也有一个后端。我已经使用npm start命令从我的服务器启动了前端和后端。前端运行正常,但后端运行不正常。(在localhost中完美工作)
前端从端口3000打开,后端从端口9000打开。
这是我的nginx配置
server {
server_name myservername.com www.myservername.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
server {
if($host = www.myservername.com){
return 301 https://$host$request_uri;
}
if($host = myservername.com){
return 301 https://$host$request_uri;
}
listen 80 default_server;
listen [::]:80 default_server;
server_name myservername.com www.myservername.com;
return 404;
}正在使用的端口

我在控制台中遇到的错误
POST http://localhost:9000/bot net::ERR_CONNECTION_REFUSED如何解决此问题?
问题是尝试执行此获取操作
fetch("http://localhost:9000/bot", {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
}).then(res => res.text())
.then(res => {
res = JSON.parse(res);
this.setState({ apiResponse: res.response, apiIntent: res.intent}, () => {this.setBotMsg()});
console.log(`This is the response: ${res.response}, ${res.intent}`);
;
});发布于 2021-01-13 17:32:01
这将向计算机端口9000上的本地环回适配器发出调用。我不认为这是你想要的。在localhost的位置使用您的服务器IP或域名。
fetch("http://myservername.com:9000/bot", {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
}).then(res => res.text())
.then(res => {
res = JSON.parse(res);
this.setState({ apiResponse: res.response, apiIntent: res.intent}, () => {this.setBotMsg()});
console.log(`This is the response: ${res.response}, ${res.intent}`);
;
});我会使用NGINX将请求代理到你的后端,而不是直接暴露端口9000。
fetch("https://myservername.com/bot", {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
}).then(res => res.text())
.then(res => {
res = JSON.parse(res);
this.setState({ apiResponse: res.response, apiIntent: res.intent}, () => {this.setBotMsg()});
console.log(`This is the response: ${res.response}, ${res.intent}`);
;
});nginx的配置取决于你想要做什么。子域或位置?子域可以是类似bot.myserver.com的域名。一个位置myserver.com/bot。
server {
listen 443
.....
location /bot {
proxy_pass http://localhost:9000;
proxy_set_header Host $host;
....
}
}补充说明:我已经更新了配置,以便从前端使用https。
https://stackoverflow.com/questions/65698714
复制相似问题