RESTful API的Nginx配置 [英] nginx configuration for a RESTful API

查看:1224
本文介绍了RESTful API的Nginx配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用nginx和php的初学者,所以请原谅我的基本问题.

I am a beginner with nginx and php, so please excuse my basic question.

对于基于RESTful的API(nginx + php),我需要一些有关nginx配置的帮助.

For a RESTful based API (nginx + php) I would need some help with nginx configuration.

以下是nginx配置的相关代码段(如建议的此处),用于重定向所有/api/v1/*对我的apiv1.php脚本的请求:

Here is the relevant snippet of the nginx configuration (as suggested here) for redirecting all /api/v1/* requests to my apiv1.php script:

    server {
        server_name myServer;
        root /usr/share/nginx/html;
        location /api/v1/ {
          try_files $uri $uri/ /apiv1.php?$args;
        }

        location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

现在的问题是,当我键入 http://myServer//api/v1/resource/GetInfo 时在我的浏览器中,apiv1.php脚本似乎没有收到"resource/GetInfo".实际上,_GET和_REQUEST为空,但是_SERVER看起来不错!

Now the issue is that when I type http://myServer//api/v1/resource/GetInfo in my browser, the apiv1.php script doesn't seem to receive the "resource/GetInfo". Actually, _GET and _REQUEST are empty, but _SERVER looks OK!

在我的/etc/php5/fpm/php.ini中,启用了以下相关配置:

In my /etc/php5/fpm/php.ini, the following relevant config is enabled:

request_order = "GP"
variables_order = "GPCS"
register_argc_argv = Off
auto_globals_jit = On.

您是否知道为什么php _GET和_REQUEST为空?这仅与我的php配置有关吗?

Do you maybe know why the php _GET and _REQUEST are empty? Is this related to my php configuration only?

最诚挚的问候, M.

推荐答案

替换此:

location /api/v1/ {
    try_files $uri $uri/ /apiv1.php?$args;
}

在服务器块中包含以下内容:

With the following inside your server block:

rewrite ^/api/v1/([^/]+)/([^/]+)/?$ /apiv1.php?class=$1&method=$2? last;

创建一个名为apiv1.php的php文件,并使用以下代码行将其放置在Web服务器的根目录中:

Create a php file called apiv1.php and place in the root directory of your web server with the following lines of code:

<?php
$class  = filter_input(INPUT_GET, 'class',  FILTER_SANITIZE_STRING);
$method = filter_input(INPUT_GET, 'method', FILTER_SANITIZE_STRING);

echo $class;
echo '<br />';
echo $method;

通过在浏览器中访问以下链接进行测试:

Test by visiting the following link in your browser:

http://myServer/api/v1/members/getInfo

这篇关于RESTful API的Nginx配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆