Nginx

使用 nginx 在同一個域名下執行兩個不同的網站

  • November 23, 2020

我是伺服器部署的新手。對於我的電子商務應用程序,我有兩個 React 項目,一個是storefront針對客戶的,另一個是dashboard針對管理員的。我已經使用以下配置設置了我的 nginx 伺服器。我可以通過 https://shop.saleortest.com 訪問我的店面但我想使用https://shop.saleortest.com/dashboard訪問我的儀表板。這可以通過在塊內reverse proxy添加來完成。但我不知道如何實現這一點。我試過添加這個塊,在這裡https://admin.shop.saleortest.com執行相同的伺服器。但它不工作proxy_pass``location

location /dashboard/ {
  proxy_pass https://admin.shop.saleortest.com;
}

例如,我可以去這個網站https://demo.saleor.io/>這是店面,如果我去<https://demo.saleor.io/dashboard它帶我去dashboard應用程序,這是兩個不同的反應應用程序使用同一個域。這里店面和儀表板都在單個伺服器上執行?我怎樣才能實現這樣的目標。謝謝。

  
   server {
   server_name shop.saleortest.com;
   root /var/www/html/storefront;
   index index.html;
   location / {
       try_files $uri $uri/ /index.html?$args;
}

   location /graphql/ {
   proxy_pass http://127.0.0.1:8000/graphql/;
 }

location /dashboard/ {
  proxy_pass https://admin.gethookedseafood.com;
}


   listen [::]:443 ssl; # managed by Certbot
   listen 443 ssl; # managed by Certbot
   ssl_certificate /etc/letsencrypt/live/shop.saleortest.com/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/shop.saleortest.com/privkey.pem; # managed by Certbot
   include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

   server {
   if ($host = shop.saleortest.com) {
       return 301 https://$host$request_uri;
   } # managed by Certbot


   listen 80;
   listen [::]:80;
   server_name shop.saleortest.com;
   return 404; # managed by Certbot


}

如果您的主要 React 應用程序位於/var/www/html/storefront目錄下,並且您可以將儀表板應用程序放在/var/www/html/storefront/dashboard目錄下,最簡單的方法是使用以下配置:

server {
   server_name shop.saleortest.com;
   root /var/www/html/storefront;

   # SSL configuration by certbot here

   index index.html;

   # frontend app
   location / {
       try_files $uri $uri/ /index.html?$args;
   }
   location /graphql/ {
       proxy_pass http://127.0.0.1:8000/graphql/;
   }

   # dashboard app
   location /dashboard/ {
       try_files $uri $uri/ /dashboard/index.html?$args;
   }
}

您需要根據您的/dashboardURI 前綴重建儀表板 React 應用程序,因為它的所有路由都應該使用該前綴,並且到其資產的所有連結都應該使用該前綴生成(或者它們顯然將與您的前端應用程序一起提供)。查看本文以獲取有關如何完成此操作的範例。

引用自:https://serverfault.com/questions/1043375