使用 nginx 和 Namecheap 设置子域

Setting up subdomains with nginx and Namecheap

似乎有很多不同的相互冲突的做事方式,所以我有点困惑,需要一些帮助。 我在服务器上的不同端口上有几个站点 运行。 我在端口 8085 上有一个项目管理站点,在 3000 上有一个仪表板,等等。 我想为其中的每一个创建子域,我希望能够使用 nginx 而不是屏蔽来做到这一点。

现在我有一条 DNS A 记录指向我的 IP 地址,它成功显示了 "Welcome to nginx!" 页面。 我如何做到这一点,以便如果我访问带有 projects.url.com 的页面,nginx 将代理将其传递给 http://localhost:8085 并且正确的 url 将继续在浏览器中显示?

感谢您的帮助!

这是我当前的站点文件片段,但它似乎无法解决问题:

server {
listen 80;
server_name projects.calben.xyz www.projects.calben.xyz;
location / {
    proxy_pass http://localhost:8085/;
}
}

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

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}

首先设置一个通配符子域以指向您的服务器 IP。或者,如果您愿意,可以设置您想要的特定子域。

https://www.namecheap.com/support/knowledgebase/article.aspx/597/2237/how-can-i-set-up-a-catchall-wildcard-subdomain

假设您有一个 nginx,其配置保持在 /etc/nginx/sites-available

您将备份并删除 /etc/nginx/sites-available/default.conf。然后您将使用您的子域名添加文件

sub.example.com.conf

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

   location / {
      proxy_pass http://127.0.0.1:8085;
   }
}

然后在 /etc/nginx/sites-enabled/sub.example.com.conf 中对这个文件进行符号链接并重新启动 nginx。现在您的网站应该可以在 http://sub.example.com

访问