简体   繁体   English

如何连接主机的本地主机?

[英]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.

相关问题 当我尝试连接到本地主机时,它开始打印出我无法理解的疯狂输出 - When I try to connect to a localhost it starts printing out crazy outputs that I can't understand 如何在本地主机中获取 api,即 react js 中的 localhost/9563 - How to fetch api in local host i.e localhost/9563 in react js 如何在本地主机中连接到两个不同的Web服务器 - How To Connect to two different web server in localhost 如何让我的 React 应用程序在 localhost:3000 中编译 - How can I get my react app to compile in localhost:3000 我该怎么做<link>停止将外部链接附加到本地主机 - How can I make <Link> to stop appending external link to localhost 如果我部署我的 React 应用程序,指向 localhost 的链接是否仍然有效,还是我还需要托管 localhost 应用程序? - If I deploy my react app, will the link to a localhost still be valid, or will I also need to host the localhost app? 如何合并connect()的2种用法? - How can I combine 2 uses of connect()? 如何将 mongodb 与 reactjs 连接? - How can I connect mongodb with reactjs? 如何将帖子连接到其用户名? - How can I connect a post to its username? 如何使用 GitHub 托管我的 React 应用程序? - How can I host my React application using GitHub?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM