您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。