簡體   English   中英

我有 nginx 正在運行,但它不適合我的反應構建

[英]I have nginx running but it doesn't serve my react build

我在 ubuntu 上運行。 我有 nginx 在我的電腦上運行。 當我 go 到瀏覽器中的 ip 地址時,我得到了 nginx 的歡迎頁面,但我看不到我的反應應用程序。 我只是收到一個 404 錯誤,上面寫着 nginx。 我的應用程序是使用 create-react 應用程序構建的反應應用程序。 我使用 npm 運行構建進行構建。 我在站點可用 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

當我在瀏覽器中將 go 轉到 ip 地址時,會顯示“無法訪問此站點”

每次編輯文件時,我都會重新啟動服務器,但錯誤頁面上沒有提及 nginx。

systemctl restart nginx

請幫我。

相信您已經使用瀏覽器直接加載了 index.html...

我相信 nginx 會尋找 .conf 文件。 請嘗試將您的 /etc/nginx/sites-available/sdft.nginx 重命名為 sdft.conf 然后請嘗試將您的符號鏈接設置為

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,並且沒有其他服務在競爭。

由於聲譽限制,我無法向您提問。

首先檢查 nginx 的所有配置是否通過:

sudo nginx -t

沒事吧?

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

這些文件名必須匹配。

並嘗試添加listen 80; ,以便它知道要監聽的端口。

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

我正在添加另一個答案。 您想要導航到 IP (192.168.43.177)。 Web 服務器偵聽端口 80(默認情況下)。

當我 go 到我的 ip 地址時,我得到了 nginx 的歡迎頁面......是這樣的嗎?

在此處輸入圖像描述

如果是,nginx 工作正常。 轉到 /etc/nginx/sites-available/default 並查看根的路徑是什么。 在此處輸入圖像描述

在上面的示例中,路徑是 /var/www/html。 您應該將您的構建復制到此路徑,即您的 index.html 必須在此處。

您不必更改默認值中的任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM