隨著公司的子項目越來越多,會有大大小小十幾個工程(僅后端),按照原先的做法,每上線一個項目,那么必須要有一個二級域名映射到對應(yīng)的工程上,十個工程那么就意味著需要有十個二級域名(還不包含測試環(huán)境,次生產(chǎn)環(huán)境等),如此多的域名不僅僅是難于管理,更重要的是比較浪費(fèi)資源,這個問題困擾了我很久,今天終于解決了這個問題,特此記錄一下采坑日記,本文不會講nginx中各個指令的原理,而是用實際的項目配置來練習(xí)nginx指令的用法并舉一反三。
事先準(zhǔn)備
域名
假設(shè)域名為:http://www.example.com
實驗環(huán)境
云服務(wù)器 + centos + nginx + php-fpm
項目1
1.工程路徑: /data/wwwroot/project1/2.訪問路徑:http://www.example.com/project1/項目2
1.工程路徑: /data/wwwroot/project2/2.訪問路徑:http://www.example.com/project2/項目3
1.工程路徑: /data/wwwroot/project3/2.訪問路徑:http://www.example.com/project3/為了實現(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.example.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.example.com;
access_log /data/wwwlogs/nginx/access_log/www.example.com_nginx.log combined;
error_log /data/wwwlogs/nginx/error_log/www.example.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;
}
}
單獨(dú)的云服務(wù)器安全等級保護(hù)評估小型網(wǎng)站建設(shè)通常存在什么錯誤看法我想自己買個阿里云服務(wù)器做購物網(wǎng)站務(wù)必要明白的事情云服務(wù)器的計費(fèi)方式是否劃算ntfs格式是什么意思 exfat和ntfs有什么區(qū)別為觸屏手機(jī)而設(shè)計系列1 拇指操作的“熱區(qū)/死角”與“控件尺寸”