【问题标题】:Configring Nginx for rest api为rest api配置Nginx
【发布时间】:2016-07-10 10:32:16
【问题描述】:

我已经在 apache 服务器中实现了 Rest Api(Post),但是当我将它移到 nginx 时它不起作用。这是我在名为 .htaccess 的根目录中使用的配置文件。

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]   
</IfModule>

它所做的只是如果一个请求到达我的服务器的 ipaddess,例如 example.com/promocode_api/validate,它会向 api.php 发送请求来处理它。 但是无法在 nginx 中进行配置以使其正常工作。

这是我来自 etc/nginx/sites-enabled/defalut 的 nginx 代码。

server {
    listen   80;


    root /purple_dev;
    index index.php index.html index.htm;

    server_name example.com;

   #     location / {
   #             try_files $uri $uri/ /index.html;
   #     }

    location /promocode_api {

    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /purple_dev;
    }
  # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

    }

}

【问题讨论】:

    标签: php .htaccess rest nginx


    【解决方案1】:

    您可能需要以下内容:

    location /promocode_api  {
        index       api.php;
        include     mime.types;
        try_files   $uri $uri/ api.php$is_args$args;
    }
    

    我不明白为什么你对同一个目标文件有 3 条不同的规则。我会在 php 中处理这些。

    【讨论】:

      【解决方案2】:

      我终于解决了。

      这是需要添加的

      location /promocode_api {
           rewrite ^/promocode_api/(.*)$ /promocode_api/api.php?rquest=$1;
          }
      

      【讨论】:

        猜你喜欢
        • 2017-10-01
        • 2020-10-13
        • 1970-01-01
        • 2015-07-28
        • 2019-11-17
        • 2018-02-23
        • 1970-01-01
        • 1970-01-01
        • 2015-03-21
        相关资源
        最近更新 更多