前一段时间打算尝试一下微信开发,需要在阿里云上使用多个项目,于是查了一下资料,找到如下实现方案:

利用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


参考资料


Laravel Nginx

登陆发表评论