【问题标题】:Nginx configuration with Slim Framework, How to keep the .php url使用 Slim 框架进行 Nginx 配置,如何保留 .php url
【发布时间】:2014-05-14 02:30:04
【问题描述】:

我的服务器刚刚被提供商刷掉了,我没有Nginx配置文件的备份。很幸运我让它工作了,现在我不知道该怎么办了。

所以我有多个文件来做一些特定的事情,并且我在每个文件上都使用 Slim 框架。这个想法是在 URL 上保留 php 扩展名。像这样的:

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

有人对此有所了解吗?我真的忘记了我之前对配置做了什么。

提前谢谢你

【问题讨论】:

  • 不脏吗?为什么要保留这个.php?
  • 因为我需要它。我已经使用该 url 在我所有已发布的应用程序上设置了静态变量,所以我无法更改它。
  • 你也可以设计一些 url 重写来替换你的 url 中的 .php ,这样你就可以保持你的 url 干净,如果有人来自你已经发布的网站之一,他就会到达页。你觉得这听起来如何?
  • 任何有效的,我都会很高兴。你能指出怎么做吗?

标签: php nginx configuration slim


【解决方案1】:

如果有人来这里,我终于找到了答案。

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;
}}

【讨论】:

    【解决方案2】:

    正如 cmets 中所建议的,重写你的 url 以便删除 '.php' 可能是一个更好的主意:

    您的 NGINX 配置文件应如下所示:

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

    有关详细信息,请参阅:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

    编辑配置后不要忘记重新启动 NGINX 服务。

    【讨论】:

    • 我还有什么需要做的吗?我仍然得到 404。
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多