nginx反向代理node_一个简单的Nginx反向代理,用于从子文件夹提供多个Node.js应用

nginx反向代理node

I recently set up a VPS on DigitalOcean to run a few different Node.js scripts under the same domain.

我最近在DigitalOcean上设置了VPS,以在同一域中运行一些不同的Node.js脚本。

Now, you can’t have two different Node.js apps listen on the same port, so you have to use a reverse proxy. Nginx is commonly used for that.

现在,您不能有两个不同的Node.js应用程序在同一端口上侦听,因此您必须使用反向代理。 Nginx通常用于此目的。

I set up each Node app to run on its own subfolder, so I had to edit the Nginx configuration:

我将每个Node应用程序设置为在其自己的子文件夹中运行,因此我必须编辑Nginx配置:

sudo nano /etc/nginx/sites-available/default

which was this:

这是:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name hellonode;

        location ^~ /assets/ {
                gzip_static on;
                expires 12h;
                add_header Cache-Control public;
        }

        location / {
                proxy_http_version 1.1;
                proxy_cache_bypass $http_upgrade;

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                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_pass http://localhost:3000;
        }
}

This configuration allows one Node.js app, running on port 3000, to be the main app served on /.

此配置允许在端口3000上运行的一个Node.js应用成为/服务的主要应用。

I wanted to have an app running under /myservice, so I created a Node app listening on port 3001 and I added this configuration:

我想让一个应用程序在/myservice下运行,所以我创建了一个监听端口3001的Node应用程序,并添加了以下配置:

location /myservice {
        rewrite ^/myservice/(.*)$ /$1 break;

        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        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_pass http://localhost:3001;
}

I checked the configuration was fine using

我检查配置是否正确使用

sudo nginx -t

and I restarted nginx:

然后我重新启动了nginx:

sudo systemctl restart nginx

翻译自: https://flaviocopes.com/nginx-reverse-proxy/

nginx反向代理node

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设我们有两个服务器:一个应用服务器,另一个是数据库服务器。我们想要在应用服务器和客户端之间使用Nginx反向代理。以下是一个简单的例子: 1. 配置应用服务器 我们假设应用服务器地址为192.168.10.10。我们需要安装和配置应用程序(例如Node.jsJava应用程序)。然后,我们需要将应用程序绑定到localhost地址和端口,例如localhost:3000。请确保应用程序可以在该地址和端口上正常运行。 2. 安装和配置Nginx 我们需要在客户端计算机上安装Nginx服务器。然后,我们需要编辑Nginx配置文件nginx.conf并添加以下内容: ``` server { listen 80; server_name example.com; location / { proxy_pass http://192.168.10.10:3000; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 该配置将向应用服务器转发所有HTTP请求,同时从Nginx服务器上的80端口接收所有请求。我们使用其中的proxy_pass指令指定了应用服务器的地址和端口(http://192.168.10.10:3000)。通过这种方式,客户端将不会直接连接到应用服务器,而是通过Nginx间接地进行连接。我们也添加了两个Header头(X-Real-IP和X-Forwarded-For),以保护应用程序免受欺骗攻击。 3. 配置DNS 我们需要将DNS记录中的主机名指向Nginx服务器的公共IP地址。我们假设域名为example.com,Nginx服务器的公共IP地址为123.456.789.10。 4. 测试反向代理 现在我们已经配置了Nginx反向代理。我们需要测试一下它是否起作用。打开浏览器并访问http://example.com。如果一切正常,您将看到应用程序的网站。同时,您可以在应用服务器上的应用程序日志文件中看到关于反向代理的请求信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值