NGINX服务器指令到达API


NGINX server directives to reach an API

我有一个LEMP服务器,我想安装RespondCMS (http://respondcms.com/documentation/install-on-digital-ocean)。我在 API 的mod_rewrite部分遇到了一些困难。我已经尝试了几次迭代,但无法让它显示"API 工作"消息,指示 PHP 应用程序正在运行。我得到一个404。到目前为止,我已经在我的/etc/nginx/sites-enabled/中确定了以下设置。我认为它没有正确访问/api 文件夹。任何有助于理解它是如何相互作用以及如何使其工作的,都是值得赞赏的。

app.domain.com
server {
    listen 80;
    server_name app.domain.com;
    root /srv/www/domain_app/app;
    index index.php index.html index.htm;
    access_log /var/log/nginx/respond.access.log;
    error_log /var/log/nginx/respond.error.log;

    location /api/ {
            try_files $uri $uri/ /api/dispatch.php$args;
    }
    location / {
            try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /srv/www;
    }
    location ~ '.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}


domain.com
server {
    listen 80 default_server;
    root /srv/www/html;
    index index.php index.html index.htm;
    server_name domain.com;
    location / {
            try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/html;
    }
    location ~ '.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+'.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

URL http://app.domain.com/api/dispatch.phplocation ~ '.(hh|php)$ 块处理。而且该块似乎(合理地)配置正确。

您应该将include fastcgi_params;移动到任何fastcgi_param指令之上,以避免后者被无意中覆盖。在这种情况下,fastcgi_index指令是多余的。

location /块和location /api/块都有问题。alias指令是不必要的和错误的。所有三个位置都从外部server块继承其root。您应该删除这两个alias指令。

try_files $uri $uri/ $uri.php /api/dispatch.php$args; 语句中,$uri.php 元素将导致下载而不是执行 PHP 文件。只有 try_files 指令的最后一个元素可以执行未在同一位置块内处理的操作。如果您确实需要执行无扩展名的 PHP URI 并且还具有默认的 PHP URI,则可能需要使用命名位置。

这里有所有nginx指令的有用资源。