【问题标题】:Simply nginx reverse proxy from localhost:80 to localhost:8080 not working只是从 localhost:80 到 localhost:8080 的 nginx 反向代理不起作用
【发布时间】:2019-11-19 03:12:03
【问题描述】:

我第一次学习带 nginx 的反向代理,以下内容不适合我 我试图将来自 http://localhost 的请求重新路由到我在 http://localhost:8080 运行的 api 服务器

server {
    listen 80;
    location / {
        proxy_pass http://localhost:8080;
    }
}

当我点击http://localhost 时,我只会看到欢迎使用 nginx 初始屏幕。 如果我点击http://localhost:8080,我会看到我的 api

我有一个运行在 :8080 的节点快速服务,我可以手动点击它,但 http://localhost 不应该也被代理吗?

【问题讨论】:

  • 更改后是否重新加载或重启nginx?你试过nginx -t 看看是否有配置问题?
  • 是的,两者都做了,但没有成功。
  • 没有服务器名称,所以我猜default 配置正在接受请求。删除它或给这个一个server_name
  • 我添加了一个答案,显示了我是如何解决它的......但我不知道为什么 :80 不起作用但 [::]:80 起作用

标签: node.js express nginx reverse-proxy


【解决方案1】:

发现将其添加到我的 nginx.conf 可以解决问题:

listen [::]:80;

由于某种原因,listen 80; 没有收到我的 http://localhost 请求。

【讨论】:

    【解决方案2】:

    当我设置一个将请求转发到节点服务器的 nginx 域时,它看起来像这样,对于 server_name,您可以使用 localhost 作为通过 localhost 访问它的参数。您也可以通过 default_server 将其设为默认服务器配置。

    注意:只有一个活动配置可以包含default_server,否则 Nginx 会抛出错误。

    注意:当使用default_server 时,Nginx 将在该服务器配置中捕获localhost。否则,您需要在server_name 的列表中指定localhost(以空格分隔)。

    server {
      # Setup the domain name(s)
      server_name example.com;
      listen 80 default_server;
    
      # If you would like to gzip your stuff
      gzip on;
      gzip_min_length 1;
      gzip_types *;
    
      # Setup the proxy
      # This will forward all requests to the server
      # and then it will relay the servers response back to the client
      location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-16
      • 2018-08-07
      • 2019-06-04
      • 1970-01-01
      • 2013-05-16
      • 2015-05-25
      • 2018-01-08
      • 2021-09-15
      相关资源
      最近更新 更多