前一段時間打算嘗試一下微信開發,需要在阿里云上使用多個項目,于是查了一下資料,找到如下實現方案:
利用nginx泛域名解析配置二級域名和多域名
網站的目錄結構為
code
├── www
└── bbs
code為nginx的安裝目錄下默認的存放源代碼的路徑。
www為主頁程序源代碼路徑
bbs為論壇程序源代碼路徑
把相應程序放入上面的路徑通過
http://www.youdomain.com 訪問的就是主頁
http://bbs.yourdomain.com 訪問的就是論壇
其它二級域名類推。
具體配置如下,找到/etc/nginx/sites-available/default文件,修改為如下:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/$subdomain/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name ~^(?<subdomain>.+).yourdomain.com$;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
接著解析域名,到對應的域名管理處新增加子域名的解析,添加一條記錄。記錄類型為A,主機記錄為你的子域名(對應上面的目錄),記錄值為ip地址,其他保持默認即可。同時,如果之前添加了@或空的主機記錄,記得刪除,以免產生混淆。
重啟nginx,等待域名解析生效即可。
其他問題:
如果按照上面的步驟后出現500錯誤,可能是Laravel沒有對應的存儲權限導致的,可以使用下面的命令來給storage賦予權限chmod -R 777 storage
參考資料