首页 > 解决方案 > Nginx 和代理子域的相对路径

问题描述

我是 Nginx 的新手,正在努力学习。

我有服务器mydomain.com和我的静态站点my-app.mydomain.com 所有路径都是相对的,所以images/image.png解析为my-app.mydomain.com/images/image.png.

我还有第二个应用程序,new-app.mydomain.com它有同样的问题,试图解决相对路径mydomain.com

我不知道如何解决这个问题,我想避免让所有路径都成为绝对路径。另外,我想要一个解决方案,让我可以继续locations为新应用程序添加新块并加载资源。我想避免一些可能适用于主应用程序但不适用于其他应用程序的限制。

    location /new-app {
        proxy_ssl_server_name on;
        proxy_pass "mydomain.com";
    }

我会很感激帮助。

标签: nginxnginx-reverse-proxynginx-location

解决方案


服务器内的位置可以帮助您配置需要在网站子路由上显示的网站/内容。子域需要在单独的 nginx 文件中配置,类似于主域,您可以根据需要在其中添加任意数量的位置。

Nginx 文件:-

  1. mydomain.com
server{
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name mydomain.com;

    location / {
        proxy_ssl_server_name on;
        proxy_pass "mydomain.com"; # This should be server with port on which your server is running on VM(virtual machine)
        root /var/www/html/mydomain.com; #In case you want to server static files
        try_files $uri $uri/ /index.html;
    }
}

  1. new-app.mydomain.com
server{
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name new-app.mydomain.com;

    location / {
        proxy_ssl_server_name on;
        proxy_pass "new-app.mydomain.com"; # This should be server with port on which your server is running on VM(virtual machine)
        root /var/www/html/new-app.mydomain.com; #In case you want to server static files
        try_files $uri $uri/ /index.html;
    }
}


推荐阅读