
事先准备
域名
假设域名为:http://www.example.com
实验环境
云服务器 + 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/
为了实现以上的访问形式,我们需要用到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 访问为例,那么对应的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;
}
}


