Nginx Url重写:从URL中删除文件夹

我试图重写像下面的url

https://example.com/products/xperia-z5/ to--> https://example.com/xperia-z5/ 

但我想在同一时间的urlhttps://example.com/products/是可访问的,没有任何修改,因为它的产品目录。

为了组织的原因,我把我的文件在/ products / file1,file2等。也许我应该使用“别名”,而不是“重写”?

也许我必须改变try_files指令中的某些内容,或者@ extensionless-php位置有问题,我完全困惑。 请指教。

谢谢。

以下是我的server.confconfiguration

 server { server_name 192.168.10.1; listen 80; root /home/webmaster/example.com/html_public; charset UTF-8; # replace .php extension with trailing slash location @extensionless-php { rewrite ^(.*)/$ $1.php last; rewrite ^(.*[^/])$ $1/ permanent; } location / { try_files $uri $uri/ @extensionless-php; } error_page 404 /404.php; #pass the PHP scripts to FastCGI server listening on php-fpm unix socket location ~ \.php$ { try_files $uri =404; fastcgi_index index.php; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; include fastcgi_params; } location /products/$ { rewrite ^/products/(.*)$ /$1 break; } } 

当我尝试请求http://192.168.10.1/xperia-z5/在日志即时通讯(404):

 2016/04/25 16:50:19 [notice] 10191#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1" 2016/04/25 16:50:19 [notice] 10191#0: *1 "^(.*)/$" matches "/xperia-z5/", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1" 2016/04/25 16:50:19 [notice] 10191#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1" 2016/04/25 16:50:19 [notice] 10191#0: *1 rewritten data: "/xperia-z5.php", args: "", client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1" 2016/04/25 16:50:19 [info] 10191#0: *1 recv() failed (104: Connection reset by peer), client: 192.168.10.2, server: 192.168.10.1, request: "GET /xperia-z5/ HTTP/1.1", host: "192.168.10.1" 

经过大量的反复试验,结果是:

 location / { try_files $uri $uri/ @extensionless-php; rewrite /(.+$) /products/$1 break; } location = /products/ { index index.php; } 

我希望这将有助于未来的人。