我想编写一个URL,例如mysite.com/l360/profile/55,然后让NGINX加载mysite.com/l360/home.php?path=profile/55。
不幸的是,这次我不能只是切换到apache2。 所以我必须find一种方法来使NGINX工作。
我现在的问题是,mysite.com/l360加载home.php和mysite.com/l360/home.php正常工作。 当我尝试执行mysite.com/l360/profile时,它会尝试从服务器下载BIN文件。
这是我的configuration:
location / { try_files $uri $uri/ $uri.html $uri.php?$query_string; client_max_body_size 50M; client_body_buffer_size 128k; } location /l360 { try_files $uri $uri/ @l360rw; } location @l360rw { return 302 /home.php?path=$uri; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; fastcgi_index home.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; include fastcgi_params; }
我会尝试一个捕获组和重写语句的正则expression式。 像这样的东西
location ^/l360/profile/([0-9]+)$ { rewrite /l360/home.php?path=profile/$1 } location ~ \.php$ { try_files $uri =404; // I removed the next line mostly because my config doesn't have it // REMOVED fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; fastcgi_index home.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; include fastcgi_params; }
我没有testing过这个,如果它的工作是写的,我会感到惊讶。 它应该给你一些线索虽然做到这一点。
毫无疑问,其他方法可以做到这一点,其他人可能会给出更好的答案,或者可能有助于改进我的答案,以适应这一点。
我得到了一个工作解决scheme,只问我改变了一下我的代码。 以下将给出参数为path = l360 / test /,因此所需要的只是从path名称中去除文件主目录的方法:
location /l360 { index home.php; try_files /$uri /$uri/ /l360/home.php?path=$uri; } location / { try_files $uri $uri/ $uri.html $uri.php?$query_string; client_max_body_size 50M; client_body_buffer_size 128k; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; fastcgi_index home.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; include fastcgi_params; }