跳至主要內容

利用一個 NGINX 設定多個網站

環境 : Ubuntu20.04 | php7.4 | Nginx

第一步 : 伺服器已安裝好nginx 和網站所需軟件

第二步 : 開啟 nginx 設定檔

  • 以下為nginx 設定檔內容
  • Linux 設定檔通常在 /etc/nginx/sites-available/default 
  • Windows 設定檔通常在nginx資料夾下,會有一個 /nginx/conf/nginx.conf
  • Mac m1/Mac m2 設定檔通常在 /opt/homebrew/etc/nginx/nginx.conf
  • 將專案與該網域做綁定
server {
 listen 80;

    root /var/www/project1;

    index index.php;

    client_max_body_size 16M;

    server_name project1.com;

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

    location ~ \.php$ {
       try_files $uri =404;

       include fastcgi_params;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root concurs with nginx's one
    location ~ /\.ht {
        deny all;
    }
  }

server {
 listen 80;

    root /var/www/project2;

    index index.php;

    client_max_body_size 16M;

    server_name project2.com;

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

    location ~ \.php$ {
       try_files $uri =404;

       include fastcgi_params;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root concurs with nginx's one
    location ~ /\.ht {
        deny all;
    }
  }

第三步 : 重新載入nginx

sudo service nginx reload
sudo nginx -t 
  • 若沒有輸出error 則表示正常運作了

在你的瀏覽器中輸入 http://project1.com 或 http://project2.com

就會顯示兩個不同的專案了

以上為個人實際操作經驗分享,若有不同的情況發生

歡迎以下留言討論

分類:伺服器安裝

搶先發佈留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

由 Compete Themes 設計的 Author 佈景主題