[英]How can I connect localhost of the host machine?
I was trying to host react as a frontend and rails as backend.我试图将 React 作为前端,将 Rails 作为后端。
I host rails at http://localhost:3000.我在 http://localhost:3000 托管 Rails。
You can see that it is working.你可以看到它正在工作。
But when I call http://localhost:3000 from react, instead of going to server's localhost, it go to user's localhost.但是当我从 react 调用 http://localhost:3000 时,它不会转到服务器的本地主机,而是转到用户的本地主机。
How can I fix this?我怎样才能解决这个问题? I am using Nginx as web server.
我使用 Nginx 作为网络服务器。
I just solved by doing reverse proxy in Nginx.我只是通过在 Nginx 中做反向代理来解决。
location / {
try_files $uri /index.html;
}
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
first location / will find static frontend files and /api will go to localhost:3000.第一个位置 / 将找到静态前端文件, /api 将转到 localhost:3000。
For example, in my case例如,就我而言
this.url='https://yourdomain.com/api/' to access the backend. this.url='https://yourdomain.com/api/' 访问后端。
Also don't forget all of your backend route to start with 'api/'.也不要忘记所有后端路由都以“api/”开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.