隨着公司的子項目愈來愈多,會有大大小小十幾個工程(僅後端),按照原先的作法,每上線一個項目,那麼必需要有一個二級域名映射到對應的工程上,十個工程那麼就意味着須要有十個二級域名(還不包含測試環境,次生產環境等),如此多的域名不單單是難於管理,更重要的是比較浪費資源,這個問題困擾了我好久,今天終於解決了這個問題,特此記錄一下采坑日記,本文不會講nginx中各個指令的原理,而是用實際的項目配置來練習nginx指令的用法並觸類旁通。php
假設域名爲:http://www.dev.comcss
阿里雲ECS + centos + Nginx + php-fpmhtml
1.工程路徑: /data/wwwroot/project1/
2.訪問路徑:http://www.dev.com/project1/nginx
1.工程路徑: /data/wwwroot/project2/
2.訪問路徑:http://www.dev.com/project2/laravel
1.工程路徑: /data/wwwroot/project3/
2.訪問路徑:http://www.dev.com/project3/後端
爲了實現以上的訪問形式,咱們須要用到nginx裏面的location指令和alias指令,配置以下centos
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配置以下php-fpm
server { listen 80; server_name 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; } }