背景
隨著公司的子項目越來越多,會有大大小小十幾個工程(僅后端),按照原先的做法,每上線一個項目,那么必須要有一個二級域名映射到對應(yīng)的工程上,十個工程那么就意味著需要有十個二級域名(還不包含測試環(huán)境,次生產(chǎn)環(huán)境等),如此多的域名不僅僅是難于管理,更重要的是比較浪費資源,這個問題困擾了我很久,今天終于解決了這個問題,特此記錄一下采坑日記,本文不會講nginx中各個指令的原理,而是用實際的項目配置來練習(xí)nginx指令的用法并舉一反三。
事先準(zhǔn)備
域名
假設(shè)域名為:http://www.dev.com
實驗環(huán)境
阿里云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
實現(xiàn)步驟
為了實現(xiàn)以上的訪問形式,我們需要用到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 訪問為例,那么對應(yīng)的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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。