2015-12-18 350 views
1

我怎样才能解决这个问题:我想成立Nginx的的c​​onf文件,以满足以下条件:nginx的重写URL子

http://www.example.com/site1/article/index.php?q=hello-world - >http://www.example.com/site1/article/hello-world

HTTB://www.example.com/site2








.php?q = open-new-world - > httb://www.example.com/site3/article/open-new-world

还有多在example.com之后的网站,我想通过使用nginx配置使网址看起来干净。

但我的下面的配置不起作用。谁来帮帮我?

server { 
listen 80; 
listen [::]:80; 

root /var/www/example.com/public_html; 
index index.php index.html index.htm; 

server_name www.example.com;  
location ~ /article/ { 
    try_files $uri /site1/article/index.php?q=$1; 

    location ~ \.php$ { 
      try_files $uri =404; 
      fastcgi_split_path_info ^(.+\.php)(/.+)$; 
      fastcgi_pass unix:/var/run/php5-fpm.sock; 
      fastcgi_index index.php; 
      include fastcgi_params; 
    } 
} 

}

回答

0

你想在客户端提供URL像/xxx/article/yyy,然后在内部改写为/xxx/article/index.php?q=yyy

您需要捕获源URI的组件以便稍后使用它们。您的问题中有一个$1,但您错过了实际为其赋值的表达式。随着变化的最小数量,这个工程:

location ~ ^(.*/article/)(.*)$ { 
    try_files $uri $1index.php?q=$2; 
    location ~ \.php$ { ... } 
} 

但是,您不需要使用PHP嵌套位置,只要出现在PHP正则表达式的位置上述其他正则表达式的位置,它会处理所有的PHP文件。例如:

location ~ \.php$ { ... } 

location ~ ^(.*/article/)(.*)$ { 
    try_files $uri $1index.php?q=$2; 
} 
+0

现在工作!谢谢@理查德史密斯 – Jonash