![Nginx:将目录路径重写为文件](https://linux22.com/image/596487/Nginx%EF%BC%9A%E5%B0%86%E7%9B%AE%E5%BD%95%E8%B7%AF%E5%BE%84%E9%87%8D%E5%86%99%E4%B8%BA%E6%96%87%E4%BB%B6.png)
我对 Nginx 还不太熟悉,所以请多包涵 -
我想要重写一个类似于foo.bar.com/newfoo?limit=30
的URL foo.bar.com/newfoo.php?limit=30
。
这样做似乎很简单rewrite ^([a-z]+)(.*)$ $1.php$2 last;
我感到困惑的是应该把它放在哪里——我尝试过一些位置指令,但做错了。
这是我现有的虚拟主机配置,我应该在哪里实现重写?
server {
listen 80;
listen [::]:80;
server_name foo.bar.com;
root /home/foo;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
}
}
谢谢!
答案1
try_files $uri $uri/ $uri.php /index.html;