简体   繁体   English

使用Slim Framework配置Nginx,如何保留.php网址

[英]Nginx configuration with Slim Framework, How to keep the .php url

My server has just wiped out by the provider, and I don't have the backup of the Nginx configuration file. 我的服务器刚刚被提供程序清除,我没有Nginx配置文件的备份。 It was completely lucky I got it working, now I don't know what to do. 我真的很幸运,现在它能工作了,现在我不知道该怎么办。

So I am having multiple files to do some specific things, and I am using Slim Framework on each file. 因此,我有多个文件要做某些特定的事情,并且我在每个文件上使用Slim Framework。 The idea is to keep the php extension on the URL. 这个想法是将php扩展名保留在URL上。 Something like this: 像这样:

www.example.com/service.php/customer/1 www.example.com/service.php/customer/1
www.example.com/api.php/data www.example.com/api.php/data
www.example.com/test.php/other-data/5 www.example.com/test.php/other-data/5

Does anyone have some clue on this? 有人对此有任何线索吗? I really forgot what I did before with the configuration. 我真的忘记了之前所做的配置。

Thank you in advance 先感谢您

If somehow someone goes here, I finally found the answer. 如果有人以某种方式去这里,我终于找到了答案。

server {
listen   80; ## listen for ipv4; this line is default and implied

root /usr/share/nginx/www;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
}

location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    deny all;
}

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
      return 404;
    }
    include fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}}

As suggested in the comments, it's probably a better idea to rewrite your urls so that '.php' is removed : 正如评论中所建议的那样,重写URL以删除“ .php”可能是一个更好的主意:

Your NGINX configuration file should then look like this : 您的NGINX配置文件应如下所示:

server {
    listen       80;
    server_name  example.org;
    rewrite ^/(.*).php/(.*) /$1/$2    //add this line to get rid of '.php'
}

See : http://nginx.org/en/docs/http/ngx_http_rewrite_module.html for in-depth information. 有关详细信息,请参见: http : //nginx.org/en/docs/http/ngx_http_rewrite_module.html

Don't forget to restart your NGINX service after editing its configuration. 编辑配置后,请不要忘记重新启动NGINX服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM