node.js 使用 NGINX 将端口 80 转发到 8080

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24861311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:40:09  来源:igfitidea点击:

Forwarding port 80 to 8080 using NGINX

node.jsnginxproxydebian

提问by heron

I'm using LEMP stack and Node JS on my debian server. Nginx works on port 80 and Node JS on 8080. I created new subdomain: cdn.domain.com for nodejs app. Currently I can access to Node JS application only like cdn.domain.com:8080/. What I want to do is to configure Nginx so that, when I enter to cdn.domain.com I can get app working on port 80. I think it can be done using nginx upstream. But I can't figure out how.

我在我的 debian 服务器上使用 LEMP 堆栈和 Node JS。Nginx 在 80 端口和 8080 上的 Node JS 上工作。我为 nodejs 应用程序创建了新的子域:cdn.domain.com。目前我只能像 cdn.domain.com:8080/ 一样访问 Node JS 应用程序。我想要做的是配置 Nginx,以便当我进入 cdn.domain.com 时,我可以让应用程序在端口 80 上工作。我认为可以使用上游 nginx 来完成。但我无法弄清楚如何。

回答by Nyi Nyi

As simple as like this,

就这么简单,

make sure to change example.com to your domain (or IP), and 8080 to your Node.js application port:

确保将 example.com 更改为您的域(或 IP),并将 8080 更改为您的 Node.js 应用程序端口:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:8080";
    }
}

Source: https://eladnava.com/binding-nodejs-port-80-using-nginx/

来源:https: //eladnava.com/binding-nodejs-port-80-using-nginx/

回答by Tan Hong Tat

NGINX supports WebSockets by allowing a tunnel to be setup between a client and a backend server. In order for NGINX to send the Upgrade request from the client to the backend server, Upgrade and Connection headers must be set explicitly. For example:

NGINX 通过允许在客户端和后端服务器之间建立隧道来支持 WebSockets。为了让 NGINX 将升级请求从客户端发送到后端服务器,必须明确设置升级和连接标头。例如:

# WebSocket proxying
map $http_upgrade $connection_upgrade {
    default         upgrade;
    ''              close;
}


server {
    listen 80;

    # The host name to respond to
    server_name cdn.domain.com;

    location / {
        # Backend nodejs server
        proxy_pass          http://127.0.0.1:8080;
        proxy_http_version  1.1;
        proxy_set_header    Upgrade     $http_upgrade;
        proxy_set_header    Connection  $connection_upgrade;
    }
}

Source: http://nginx.com/blog/websocket-nginx/

来源:http: //nginx.com/blog/websocket-nginx/

回答by vaidik

This is how you can achieve this.

这就是您可以实现这一目标的方法。

upstream {
    nodeapp 127.0.0.1:8080;
}

server {
    listen 80;

    # The host name to respond to
    server_name cdn.domain.com;

    location /(.*) {
        proxy_pass http://nodeapp/$is_args$args;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Port $server_port;
        proxy_set_header X-Real-Scheme $scheme;
    }
}

You can also use this configuration to load balance amongst multiple Node processes like so:

您还可以使用此配置在多个 Node 进程之间进行负载平衡,如下所示:

upstream {
    nodeapp 127.0.0.1:8081;
    nodeapp 127.0.0.1:8082;
    nodeapp 127.0.0.1:8083;
}

Where you are running your node server on ports 8081, 8082 and 8083 in separate processes. Nginx will easily load balance your traffic amongst these server processes.

您在不同进程中的端口 8081、8082 和 8083 上运行节点服务器的位置。Nginx 将轻松地在这些服务器进程之间平衡您的流量。

回答by Daniel Garmoshka

Simple is:

简单的是:

server {
    listen   80;
    server_name  p3000;
    location / {
        proxy_pass http://0.0.0.0:3000;
        include /etc/nginx/proxy_params;
    }
}

回答by Ismaeel Akram

server {
  listen 80;
  server_name example.com;

  location / {
      proxy_set_header   X-Forwarded-For $remote_addr;
      proxy_set_header   Host $http_host;
      proxy_pass         "http://127.0.0.1:8080";
}   }

回答by Rohan

You can define an upstream and use it in proxy_pass

您可以定义一个上游并在 proxy_pass 中使用它

http://rohanambasta.blogspot.com/2016/02/redirect-nginx-request-to-upstream.html

http://rohanambasta.blogspot.com/2016/02/redirect-nginx-request-to-upstream.html

server {  
   listen        8082;

   location ~ /(.*) {  
       proxy_pass  test_server;  
       proxy_set_header Host $host;  
       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_redirect    off;  
   }  

}   

  upstream test_server  
     {  
         server test-server:8989  
}  

回答by SHIVAPUTRA UDAGATTI

you can do this very easy by using following in sudo vi /etc/nginx/sites-available/default

您可以通过使用以下内容轻松做到这一点 sudo vi /etc/nginx/sites-available/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _ your_domain;

    location /health {
            access_log off;
            return 200 "healthy\n";
    }

    location / {
            proxy_pass http://localhost: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;
    }
  }

回答by Vishrant

This worked for me:

这对我有用:

server {
  listen  80; 
  server_name example.com www.example.com;

  location / { 
    proxy_pass                          http://127.0.0.1:8080/;
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;  
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
  }
}

If it does not work for you look at the logs at sudo tail -f /var/log/nginx/error.log

如果它对您不起作用,请查看日志 sudo tail -f /var/log/nginx/error.log

回答by Krisalay

You can use Kong API gateway. Kong API gateway is built on top of NGINX. It exposes API which makes a reverse proxy server. You can specify your upstream URL. It also provides various useful plugins such as:

您可以使用 Kong API 网关。Kong API 网关建立在 NGINX 之上。它公开了 API,该 API 构成了一个反向代理服务器。您可以指定上游 URL。它还提供了各种有用的插件,例如:

  • oAuth2
  • jwt
  • authentication
  • HMAC Authentication
  • LDAP AUthentication
  • IP Restriction
  • CORS
  • ACL
  • Dynamic SSL
  • Bot Detection
  • Rate Limiting
  • Response Rate Limiting
  • Request size limiting
  • Request Termination
  • Request Transformer
  • Response Transformer
  • Correlation ID
  • Logging
  • oAuth2
  • jwt
  • 验证
  • HMAC认证
  • LDAP 认证
  • 知识产权限制
  • CORS
  • 访问控制列表
  • 动态 SSL
  • 机器人检测
  • 限速
  • 响应速率限制
  • 请求大小限制
  • 请求终止
  • 请求转换器
  • 响应变压器
  • 关联 ID
  • 日志记录