【问题标题】:How to create reverse proxy for multiple websites in nginx如何在 nginx 中为多个网站创建反向代理
【发布时间】:2021-06-30 13:55:18
【问题描述】:

我有许多不同的技术在我的本地机器上为 API 和站点提供服务。我希望能够通过人类可读的名称而不是端口来查看它们。

例如,我有:

  • localhost:8000 => 用于用户面板的 laravel api
  • localhost:8001 => 用于管理面板的 laravel api
  • localhost:3000 => 为用户面板反应客户端
  • localhost:3001 => 站点的 nextjs 客户端
  • localhost:3002 => 为管理面板反应客户端

这个列表还在继续。

记住所有这些端口当然是不可能的。因此我想为他们设置一个反向代理:

  • api.user.example.local
  • api.admin.example.local
  • example.local
  • user.example.local
  • admin.example.local

我知道我必须将这些主机头添加到/etc/hosts 文件中。我还阅读了关于how to configure nginx as a reverse proxy 一个域。

我不知道如何为许多网站做到这一点。而且只能作为反向代理,不能作为服务器。

【问题讨论】:

  • 本地地址使用 https 吗?
  • 如果可以,我更愿意这样做。但我还没有这方面的知识。

标签: nginx reverse-proxy


【解决方案1】:

请注意:我并不认为自己是真正的超级 nginx 专家,只是开始学习 nginx,但我认为我可以帮助您完成这项任务。

这是我的方法:

首先,确保您的默认 nginx 配置(通常为 /etc/nginx/nginx.conf)在其 http 块中有行 include /etc/nginx/conf.d/*.conf;,因此您可以在单独的配置文件中指定内部服务器以方便使用。

创建额外的配置文件/etc/nginx/conf.d/local_domains.conf 并在其中添加以下服务器块:

server {
    listen         80;
    server_name    api.user.example.local;

    location / {
    set $target http://localhost:8000;
    proxy_pass $target;
  }
}

server {
    listen         80;
    server_name    api.admin.example.local;

    location / {
    set $target http://localhost:8001;
    proxy_pass $target;
  }
}

server {
    listen         80;
    server_name    example.local;

    location / {
    set $target http://localhost:3000;
    proxy_pass $target;
  }
}

server {
    listen         80;
    server_name    user.example.local;

    location / {
    set $target http://localhost:3001;
    proxy_pass $target;
  }
}

server {
    listen         80;
    server_name    admin.example.local;

    location / {
    set $target http://localhost:3002;
    proxy_pass $target;
  }
}

在客户端机器上,将这些记录添加到hosts 文件中

192.168.1.1  api.user.example.local
192.168.1.1  api.admin.example.local
192.168.1.1  example.local
192.168.1.1  user.example.local
192.168.1.1  admin.example.local

192.168.1.1 是你的 nginx 服务器的地址。

就是这样,如果您的内部服务器使用 HTTP 协议,它应该可以工作。

但是如果你需要对内部服务器和主 nginx 服务器使用 HTTPS,修改每个服务器块如下:

server {
    listen         443 ssl http2;
    server_name    api.user.example.local;

    ssl_certificate          /usr/local/share/ca-certificates/example.local.crt;
    ssl_certificate_key      /usr/local/share/ca-certificates/example.local.key;
    add_header Strict-Transport-Security "max-age=31536000" always;

    location / {
    set $target https://api.user.example.local:8000;
    proxy_pass $target;
  }
}

and so on

ssl_certificate 和 ssl_certificate_key 应该指向域的正确证书和密钥文件。

如果您希望 nginx 主服务器监听 80 端口并将所有流量重定向到 https,请为每个服务器添加额外的服务器块:

server {
    server_name api.user.example.local;
    listen 80;

  # Force redirection to https on nginx side
  location / {
        return 301 https://$host$request_uri;
    }
}

and so on

有关 NGINX 反向代理的更多信息
NGINX Reverse Proxy
Module ngx_http_proxy_module

【讨论】:

  • 这是一个很好的解释@Dmitry。非常感谢。我会尽快测试它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 2020-04-16
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多