Ubuntu

如何將我的子域指向更正的根文件夾

  • March 28, 2019

我已經設置了生產站點,現在我正在嘗試設置一個開發/登台站點來執行實時測試。

我在將我的子域指向我的 Laravel 應用程序上的正確根路徑時遇到問題。

我需要以下內容:

  • dev.example.com 指向root /var/www/dev/public;
  • example.com 和 www.example.com 指向根目錄 /var/www/laravel/public;
  • 目前一切都將/var/www/laravel/public

伺服器:

  • Ubuntu 18.10 x64
  • Nginx

我有一個域example.com,並且我通過 certbot/LetsEncrypt 創建了一個子域,稱為dev.example.com 該域的每個引用都在我的 SSL 證書中。www.example.com、example.com 和 dev.example.com

子域的 DNS:

A (Record) | dev (Name) | 140.xx.xx.xx (Data)

我在這裡有 2 個文件夾:

  • /var/www/ laravel/public(我的主要生產站點)
  • /var/www/ dev/public(我的開發站點)

在站點可用/etc/nginx/sites-available中,我有default具有以下配置的文件。

(我已經嘗試過多種變體,但這是我離開的地方)

server {

   root /var/www/laravel/public;

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

   server_name example.com www.example.com dev.example.com;

   location / {
       try_files $uri $uri/ /index.php?$query_string;
       client_max_body_size 200M;
   }

   location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
   }

   location ~ /\.ht {
       deny all;
   }

   listen [::]:443 ssl ipv6only=on; # managed by Certbot
   listen 443 ssl; # managed by Certbot
   ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/example.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

}

# Virtual Host configuration for example.com

server {

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

   listen 80;
   listen [::]:80;
   server_name dev.example.com;
   root /var/www/dev/public;
   return 404; # managed by Certbot

}

server {

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

   listen 80;
   listen [::]:80;
   server_name www.example.com;
   return 404; # managed by Certbot

}

server {

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

   listen 80 default_server;
   listen [::]:80;
   server_name example.com;
   return 404; # managed by Certbot

}

再次…

  • 我需要dev.example.com指出root /var/www/dev/public;
  • 我需要example.com and www.example.com指出root /var/www/laravel/public;

任何幫助弄清楚這一點將不勝感激。

您需要創建一個新server塊以提供root您想要的新主機名。您可以從現有塊中複製其餘配置,或者稍後使用includes 將其乾燥。

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