这篇文章将为大家详细讲解有关怎么在nginx中实现一个域名配置多个laravel项目,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
域名
假设域名为:http://www.dev.com
实验环境
阿里云ECS + centos + Nginx + php-fpm
项目1
1.工程路径: /data/wwwroot/project1/
2.访问路径:http://www.dev.com/project1/
项目2
1.工程路径: /data/wwwroot/project2/
2.访问路径:http://www.dev.com/project2/
项目3
1.工程路径: /data/wwwroot/project3/
2.访问路径:http://www.dev.com/project3/
涉及的知识点
Nginx的location指令,用法可以参考:https://www.jb51.net/article/154637.htm
Nginx的alias指令,用法可以参考:https://www.jb51.net/article/154640.htm
实现步骤
为了实现以上的访问形式,我们需要用到nginx里面的location指令和alias指令,配置如下
location ^~ /${PROJECT}/ { alias {$PATH}; try_files $uri $uri/ @${PROJECT}; location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } location @${PROJECT}{ rewrite /${PROJECT}/(.*)$ /${PROJECT}/index.php?/$1 last; }
说明: 上面的这个配置中的${PROJECT}和{$PATH}都是属于在实际过程中需要替换的部分,其中${PROJECT}为url需要访问的path部分,如project1,{$PATH}则代表的是项目的真实访问路径,如/data/wwwroot/project1,以http://www.dev.com/project1 访问为例,那么对应的Nginx的配置是这样子的
location ^~ /project1/ { alias /data/wwwroot/project1/public; try_files $uri $uri/ @project1; location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } location @project1{ rewrite /project1/(.*)$ /project1/index.php?/$1 last; }
对于project2和project3的配置只需要按照上面的配置模板依葫芦画瓢就可以了,最后完整nginx配置如下
server { listen 80; server_name http://www.dev.com; access_log /data/wwwlogs/nginx/access_log/www.dev.com_nginx.log combined; error_log /data/wwwlogs/nginx/error_log/www.dev.com_errr_log; index index.html index.htm index.php; # project1开始的配置 location ^~ /project1/ { alias /data/wwwroot/project1/public; try_files $uri $uri/ @project1; location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } location @project1{ rewrite /project1/(.*)$ /project1/index.php?/$1 last; } # project2开始的配置 location ^~ /project2/ { alias /data/wwwroot/project2/public; try_files $uri $uri/ @project2; location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } location @project2{ rewrite /project2/(.*)$ /project2/index.php?/$1 last; } # project2开始的配置 location ^~ /project3/ { alias /data/wwwroot/project3/public; try_files $uri $uri/ @project3; location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } location @project3{ rewrite /project3/(.*)$ /project3/index.php?/$1 last; } # 解析所有的.php location ~ \.php$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } #图片、视频的的链接,此处是做缓存 ,缓存30天,不写入访问日志 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { expires 30d; access_log off; } #js css文件的配置,此处是做缓存 ,缓存7天,不写入访问日志 location ~ .*\.(js|css)?$ { expires 7d; access_log off; } location ~ /\.ht { deny all; } }
关于怎么在nginx中实现一个域名配置多个laravel项目就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。