如何配置NGINX使用本地网络上的多个子域来服务多个应用程序?

问题描述 投票:0回答:1

我在同一网络上有以下设置:

  • 具有本地IP的Windows计算机
    192.168.1.23
  • 具有本地IP的arch服务器
    192.168.1.15
    • 端口上的 homarr 仪表板应用程序
      9000
    • 端口上的 nextcloud 应用程序
      9001
    • 端口上的 gitea 应用程序
      9002

这些应用程序在服务器上的 Docker 容器上运行,并且可以使用以下 URL 在 Windows 计算机上访问

http://192.168.1.15:9000/
http://192.168.1.15:9001/
http://192.168.1.15:9002/

现在,我希望配置 NGINX 反向代理,以便通过输入

http://gitea.test.dev/
http://homarr.test.dev/
http://nextcloud.test.dev/
来访问各种服务。

为了在本地执行此操作,我修改了两个平台上的主机文件:

# windows
192.168.1.15 test.dev
192.168.1.15 gitea.test.dev
192.168.1.15 homarr.test.dev
192.168.1.15 nextcloud.test.dev

# linux
127.0.0.1 localhost
127.0.0.1 gitea.localhost
127.0.0.1 homarr.localhost
127.0.0.1 nextcloud.localhost

在 archlinux 服务器上,我可以使用上述方法访问此时的各个服务。但是,我无法使用类似于

http://<service name>.test.dev/
的 URL 从 Windows 计算机访问这些服务,尽管这是目前的预期行为。

然后,我使用以下配置配置并启动了 NGINX 服务:

# /etc/nginx/nginx.conf
user http;
worker_processes auto;
worker_cpu_affinity auto;

events {
    multi_accept on;
    worker_connections 1024;
}

http {
    charset utf-8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    log_not_found off;
    types_hash_max_size 4096;
    client_max_body_size 16M;

    # MIME
    include mime.types;
    default_type application/octet-stream;

    # logging
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log debug;

    # load configs
    # include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

目录

/etc/nginx/sites-enabled/
包含指向各个服务配置的链接。

# /etc/nginx/sites-enabled/homarr.localhost.conf
server {
        listen 80;
        server_name homarr.localhost;
        location / {
                proxy_pass http://localhost:9000;
        }
}
# /etc/nginx/sites-enabled/nextcloud.localhost.conf
server {
        listen 80;
        server_name nextcloud.localhost;
        location / {
                proxy_pass http://localhost:9001;
        }
}
# /etc/nginx/sites-enabled/gitea.localhost.conf
server {
        listen 80;
        server_name gitea.localhost;
        location / {
                proxy_pass http://localhost:9002;
        }
}

我相信,这些是非常简单的

proxy_pass
配置。但我希望它们能够按预期工作。现在,我的问题是,使用
http://test.dev/
http://<service>.test.dev/
> 从 Windows 计算机只能使用其中一项服务。在我看来,
default_server
是唯一可用的,因为可用服务始终遵循服务名称的字母顺序,而且这肯定是由
include
语句加载的顺序。配置。

您知道为什么我的个人服务不能同时提供吗?有没有什么方法可以实现工作配置以实现在本地网络的不同子域上提供所有三种服务的预期目的?

docker nginx-reverse-proxy nginx-config
1个回答
0
投票

您的站点配置文件中的

server_name
值是错误的。你有

  • homarr.localhost
  • nextcloud.localhost
  • gitea.localhost

但他们应该是

  • homarr.test.dev
  • nextcloud.test.dev
  • gitea.test.dev

如果您希望这两个名称都起作用,您可以将两个名称都包含在配置文件中,如下所示

server_name homarr.localhost homarr.test.dev;

但要使其正常工作,

server_name
必须与传入请求中的
Host
标头中的内容相匹配。

© www.soinside.com 2019 - 2024. All rights reserved.