nginx 正在运行但未提供服务?

Zey*_*nel 8 ubuntu jetty nginx java

我一直在尝试将 nginx 设置为码头的代理。我有一台运行 ubuntu 服务器的笔记本电脑。现在我让码头在 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以从我的家庭网络连接到 nginx 并看到 nginx 欢迎屏幕。

我也试过 $ sudo netstat -tanpl|grep nginx

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3264/nginx: worker 
Run Code Online (Sandbox Code Playgroud)

我看到 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/;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 2

似乎 nginx 使用的 conf 文件比我想象的要多。我在 conf 文件中添加了一个错字/etc/nginx/sites-available/nomilkfor.me(我删除了一个右花括号)并运行$ nginx -s reload并编译没有错误,并在浏览器上显示了 nginx 启动页面。某处可以有另一个conf文件吗?有没有办法找到nginx正在使用的conf文件?



编辑 3

根据我添加的Pazis 评论,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/;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 4

这是我的码头 conf 文件。它在/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>
Run Code Online (Sandbox Code Playgroud)

ali*_*der 10

您可以尝试以下步骤吗?

  1. 确保父 nginx 目录中的 nginx.conf 文件具有指向 sites-available 目录中默认文件的包含指令

  2. 如果您需要代理到另一个服务(例如 jetty),请使用以下共享的上游选项(在 sites-available/default 文件中)。您可以从服务器部分参考上游。

一旦你的基本配置工作,你可以检查重写选项以及是否需要为主机名做任何事情。希望能帮助到你。

nginx.conf:

include /etc/nginx/sites-enabled/*;
Run Code Online (Sandbox Code Playgroud)

在站点可用目录(默认文件)中:

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
        }
Run Code Online (Sandbox Code Playgroud)

此外,可以按如下方式检查 nginx conf 文件(在重新加载/重新启动之前): nginx -t -c /etc/nginx/nginx.conf

更新: 当您尝试 nginx 配置时,您能否检查 jetty 是否在 0.0.0.0:8080 上运行(也许可以使用 netstat 进行确认)。另外,当您通过浏览器访问 URL 时,您可以共享来自 nginx 的访问/错误日志吗?