nginx 正在運作但未提供服務?

nginx 正在運作但未提供服務?

我一直在嘗試將 nginx 設定為 jetty 的代理。我有一台筆記型電腦運行 ubuntu 伺服器。現在我讓 jetty 在 localhost:8080 上工作,它為應用程式的主頁提供服務http://192.168.1.5:8080/my-webapp-0.1.0-standalone/

我這樣設定 nginx (我從這個頁面改編而來):

server {
  listen 80;
  server_name nomilkfor.me;
  rewrite ^(.+?)/?$ http://nomilkfor.me$1 permanent;
}

server {
  listen 80;
  server_name www.nomilkfor.me;
  root /usr/share/nginx/html;

  location / {
    try_files $uri @my-webapp;
  }

  location @my-webapp {
    proxy_pass http://localhost:8080;
  }
}

我可以從我的家庭網路連接到 nginx,並且看到 nginx 歡迎畫面。

我也嘗試過$ sudo netstat -tanpl|grep nginx

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3264/nginx: worker 

我看到 nginx 正在監聽 80 連接埠。

但是當我嘗試載入 nomilkfor.me 時,出現“Chrome 無法連接到 nomilkfor.me”錯誤。

我究竟做錯了什麼?



編輯

我創建了一個非常簡單的配置,這個配置也透過jetty服務於index.htmlin/usr/share/nginx/而不是應用程式:

server {
    listen 80;
    server_name nomilkfor.me;

    # access_log /var/log/nginx/localhost.access.log;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }

    location /html { 
        root /usr/share/nginx/;
    }
}


編輯2

nginx 似乎使用了另一個比我想像的conf 檔案。我在conf檔案中添加了一個拼寫錯誤/etc/nginx/sites-available/nomilkfor.me(我刪除了右花括號)並運行$ nginx -s reload,它編譯時沒有錯誤,並在瀏覽器上顯示了nginx啟動頁面。某個地方可以有另一個conf檔嗎?有沒有辦法找到 nginx 正在使用哪個conf檔?



編輯3

按照帕齊斯評論我添加了root但不確定它應該在哪裡或應該是什麼。我添加了一些建議。哪一個是正確的?/usr/share/nginx/html是我有的地方index.html

server {
    listen 80;
    server_name nomilkfor.me;
    # root /home/z/jetty/jetty-dist/webapps
      root /usr/share/nginx/html;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }

    #location /html {
    #    root /usr/share/nginx/;
    }
}


編輯4

這是我的碼頭配置。它是在/home/z/jetty/jetty-dist/jetty-distribution-9.1.0.v20131115/demo-base/webapps/my-webapp.xml

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Required minimal context configuration :                        -->
  <!--  + contextPath                                                  -->
  <!--  + war OR resourceBase                                          -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="contextPath">/test</Set>
  <Set name="war"><Property name="jetty.webapps" default="."/>/my-webapp-0.1.0-standalone.war</Set>

答案1

您可以嘗試以下步驟嗎?

  1. 確保父 nginx 目錄中的 nginx.conf 檔案具有指向 site-available 目錄中的預設檔案的 include 指令

  2. 如果您需要代理到另一個服務(例如jetty),請使用下面共享的上游選項(在sites-available/default檔案中)。您可以參考上游的伺服器部分。

一旦基本配置工作正常,您就可以檢查重寫選項以及是否需要對主機名稱執行任何操作。希望能幫助你。

Nginx.conf:

include /etc/nginx/sites-enabled/*;

在網站可用目錄(預設檔)中:

upstream nomilkforme {  
        server 0.0.0.0:8080; ##nomilkforme running on 0.0.0.0:8080;
        keepalive 500;
}

server {
        listen       80;
        server_name  localhost;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $http_host;
                proxy_set_header X-Nginx-Proxy true;
                proxy_set_header Connection "";
                proxy_http_version 1.1;
                proxy_pass http://nomilkforme
        }

另外,可以檢查 nginx conf 檔案(在重新載入/重新啟動之前),如下所示: nginx -t -c /etc/nginx/nginx.conf

更新: 當您嘗試 nginx 設定時,您可以檢查 jetty 是否在 0.0.0.0:8080 上運行(也許使用 netstat 進行確認)。另外,當您透過瀏覽器存取URL時,您可以分享一下nginx的存取/錯誤日誌嗎?

相關內容