我現在想配置 兩個站點,通過域名來區分訪問不同的網站目錄
比如有個域名 baidu.com 第二個域名 google.com,我有兩個網站目錄,
/opt/web/baidu; /opt/web/google;
訪問 baidu.com的時候訪問 地一個目錄的網站,google.com 訪問第二個目錄;
首先這兩個域名都不是我的,為了達到講解效果,先修改本地 hosts文件 ,讓這兩個域名暫時屬於我;
1 sudo vim /etc/hosts 2 添加: 3 127.0.0.1 baidu.com google.com
查看配置文件 /etc/nginx/nginx.conf (通過 apt安裝的 nginx 配置文件位置),里面有一行
include /etc/nginx/sites-enabled/*;
如果前面有 # 注釋了 就打開,有了這句話 ,所有放在 sites-enabled下面的文件都會被當作是配置文件來讀取;
在下面新建兩個文件 baidu.conf ; google.conf ;
在 baidu.conf中 填充以下內容:
1 server { 2 listen 80 ; 3 4 root /opt/web/baidu; 5 6 # Add index.php to the list if you are using PHP 7 index index.html index.php ; 8 9 server_name baidu.com www.baidu.com; 10 11 location / { 12 try_files $uri $uri/ /index.php?$query_string; 13 } 14 15 # 16 location ~ \.php$ { 17 include snippets/fastcgi-php.conf; 18 19 fastcgi_pass unix:/run/php/php7.0-fpm.sock; 20 } 21 22 location ~ /\.ht { 23 allow all; 24 } 25 }
google.conf和上面一樣,只需把 相應的 baidu 改為 google
通過 apt 安裝的 nginx 在 sites-enabled下面會有一個默認的 default 文件 ,里面有一個默認的配置,會有影響,把里面內容全比注釋了,或者刪除;
好了 ,重啟 nginx ;
sudo service nginx restart 或者 sudo nginx -s reload
打開瀏覽器 輸入 google.com 顯示的是 /opt/web/google/index.html 的內容,
baidu.com 顯示的是 /opt/web/baidu/index.html 的內容;