reactjs - 我正在运行 nginx,但它不服务于我的 React 构建

标签 reactjs ubuntu nginx npm vps

我在ubuntu上运行。我的电脑上运行着 nginx。当我在浏览器中访问我的 IP 地址时,我看到了 nginx 的欢迎页面,但我看不到我的 React 应用程序。我刚刚收到 404 错误,上面写着 nginx。我的应用程序是一个使用 create-react 应用程序构建的 react 应用程序。我使用 npm run build 进行了构建。我在 site-available with nano 中创建了一个网站。看起来像

   server {
   server_name 192.168.43.177;
   root home/a/Documents/d/dapp1/mainDapp/client/build/;
   index index.html;
   location / {
   try_files $uri /index.html =404;
   }
}

一开始我跑

sudo ln -s /etc/nginx/sites-available/sdft /etc/nginx/sites-enabled/sdft

这会在浏览器中显示 404 错误页面,上面显示 nginx。然后我运行了这个命令

sudo ln -s /etc/nginx/sites-available/sdft.nginx /etc/nginx/sites-enabled/sdft.nginx

当我在浏览器中访问 IP 地址时,显示“无法访问此网站”

每次编辑文件时,我都会重新启动服务器,但错误页面上没有提及 nginx。

systemctl restart nginx

请帮助我。

最佳答案

相信您已经使用浏览器直接加载了index.html来运行...

我相信 nginx 会寻找 .conf 文件。 请尝试将/etc/nginx/sites-available/sdft.nginx 重命名为 sdft.conf 然后请尝试将您的符号链接(symbolic link)设置为

ln -s /etc/nginx/sites-available/sdft.conf /etc/nginx/sites-enabled/sdft.conf

如果您的问题仍未解决,请尝试

server {
   server_name 192.168.43.177;
   root home/a/Documents/d/dapp1/mainDapp/client/build/;
   index index.html;
   location / {
       try_files $uri $uri/ =404;
   }
}

注意从index.html 到$uri/位置的变化。 另请验证您的系统正在监听端口 80,并且没有其他服务在竞争。

由于声誉限制,我无法向您提问。

关于reactjs - 我正在运行 nginx,但它不服务于我的 React 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62904721/

相关文章:

java - 当奴隶试图联系它时, Jenkins 返回 403 禁止

nginx - 为什么我的部分http请求不使用http2

amazon-web-services - 如何为 WebSocket 协议(protocol)配置 AWS ELB 和 Nginx?

redirect - Nginx 配置代理传递给外部 ip 和端口 (https -> http)

reactjs - React - 通过单击按钮获取输入的值

javascript - react -路由器withRouter不注入(inject)路由器

reactjs - React Material-ui 响应式布局

javascript - 如何将本地自定义字体应用于 Stripe Elements?

git - 如何在没有互联网连接的情况下在 ubuntu 上提取 git 存储库?

java - Linux 播放视频命令在几秒钟后卡住