首页 > 解决方案 > NGINX 默认站点工作,但另一个端口上的一个不工作

问题描述

您好我正在使用 nginx 设置两台服务器。一个是前端端口 80,另一个是一个 IP 地址上的后端端口 8080。

默认的工作,通过端口 8080 的后端给我一个连接超时/尝试访问服务器时无响应。

我认为这是我的执行,但是为了测试后端,如果我在浏览器中转到 xxx.xxx.xxx.xxx:8080,我应该会出现一个简单的 index.php,除非我得到连接超时/无响应.

Nginx 说它是开放和倾​​听的。如果没有端口号,它将转到我的前端/默认服务器以获取该 IP 地址。

错误日志什么也没显示!即使是我的服务器中的日志也可以访问和上游,也没有显示任何内容。

Ubuntu 18.04

这是我的配置服务器 1 默认端口 80

   server {
  # Replace this port with the right one for your requirements
  listen 80 default_server;

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name www.domain.com domain.com *.domain.com;

  root /var/www/my_project/html;

  error_page 404 errors/404.php;
#  access_log logs/my_project.ventures.access.log;
  access_log  /var/log/nginx/server.intra-upstream.log upstream buffer=16k;
  access_log  /var/log/nginx/server.intra-access.log combined buffer=16k;

  index index.php index.html index.htm;

  # static file 404's aren't logged and expires header is set to maximum age
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:9000;
  }

  location ~ /\.ht {
    deny  all;
  }
}

这是无法通过 client.domain2.com 或 www.domain2.com 访问的第二个服务器 - URL 正确,但显示的是服务器 1 htdocs。client.domain2.com:8080 或任何其他组合带来Cannot reach siteConnection Timed out

    server {
  # Replace this port with the right one for your requirements
  listen 8080;

  # Multiple hostnames separated by spaces.  Replace these as well.
  server_name client.domain2.com www.domain2.com;

  root /var/www/my_project/backend;

  error_page 404 errors/404.php;
#  access_log logs/my_project.fun.access.log;
  access_log  /var/log/nginx/server.intra-upstream.log upstream buffer=16k;
  access_log  /var/log/nginx/server.intra-access.log combined buffer=16k;
  index index.php index.html index.htm;

  # static file 404's aren't logged and expires header is set to maximum age
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    # By all means use a different server for the fcgi processes if you need to
    fastcgi_pass   127.0.0.1:9000;
  }

  location ~ /\.ht {
    deny  all;
  }
}

标签: phpnginxubuntu-18.04

解决方案


您必须在防火墙中允许您的 8080 端口

如果你没有ufw安装它像

$ sudo apt install ufw

然后检查ufw状态

$ sudo ufw status

检查您需要的端口是否已添加到列表中。

然后使用

 $ sudo ufw allow 8080 

然后再次检查ufw状态。现在它向您显示允许的 ipv4、ipv6 端口和连接。重新启动您的 nginx 服务器并检查端口是否正常工作

$ sudo systemctl nginx restart
$ curl -I 'http://{youraddress}:8080'

或点击浏览器中的网址。

如果您使用 GCP,AWS 会检查您的服务器控制台中的外部防火墙设置。像 http_server 允许 80. 并将这条记录编辑为 80, 8080 并保存。

注意:不要ufw在 GCP、AWS中使用内部防火墙设置,因为它们已经有自己的引擎防火墙设置。

谢谢


推荐阅读