nginx配置pathinfo访问模式以及多站点

最近使用ThinkPHP,偏偏自己又使用nginx服务器。由于nginx无法使用pathinfo模式,给我带来了很大的困扰,几经周折终于解决了。所以打算记录一下,以免以后遇到相同的问题又满世界找答案。

nginx配置编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name 127.0.0.1;

root /home/www;
index index.php;

location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}


location / {
index index.html index.php;
if ( -f $request_filename) {
break;
}

if ( !-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}

location ~ \.php {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
include fastcgi_params;
fastcgi_index index.php?IF_REWRITE=1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $script;
try_files $uri =404;
}

}

这样就可以让nginx支持TP5的pathinfo模式了。

多站点设置

在本地进行站点配置对web的开发是大有益处的,我们通过对不同项目的nginx配置可以实现多站点配置。

1
2
#进入nginx文件夹进行配置文件的编写
$cd /etc/nginx

可以看到有很多文件夹,默认的启用配置文件夹在sites-enabled的default文件中,由于我们使用多站点配置,无需理会。我们要在conf.d文件夹中通过建立.conf的配置文件实现多站点。比如我们有两个TP5项目,tp1和tp2在/home/www中,这我们在conf.d文件夹中新建两个名为tp1.conf和tp2.conf的配置文件。分别为如下配置。

tp1.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name tp1.test;

root /home/www/tp1/public;
index index.php;

location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}


location / {
index index.html index.php;
if ( -f $request_filename) {
break;
}

if ( !-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}

location ~ \.php {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
include fastcgi_params;
fastcgi_index index.php?IF_REWRITE=1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $script;
try_files $uri =404;
}

}

tp2.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name tp2.test;

root /home/www/tp2/public;
index index.php;

location ~* \.(eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}


location / {
index index.html index.php;
if ( -f $request_filename) {
break;
}

if ( !-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}

location ~ \.php {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
include fastcgi_params;
fastcgi_index index.php?IF_REWRITE=1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $script;
try_files $uri =404;
}

}

然后在主机的host文件添加两个域名

1
2
3
4
#我的主机为windows,故目录在c/windows/system32/drivers/etc/host

127.0.0.1 tp1.test
127.0.0.1 tp2.test

这样就实现了多站点,方便了调试。