AngularJS nginx的配置PHP API [英] AngularJS nginx configuration for PHP api

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

问题描述

我在我的Apache切换到nginx的服务器的过程,一切似乎已经从我的API配置顺利,一边。我在Apache旧的配置依赖于在根目录中的文件.htacess重定向到角入口点的所有请求(在这种情况下的index.php)如下:

I am in the process of switching my apache to an nginx server, and everything appears to have gone smoothly, aside from the configuration for my api. My old configuration under apache relied on an .htacess file in the root directory to redirect all requests to the angular entry point (in this case index.php) as follows:

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# If the request is a file, folder or symlink that exists, serve it up
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ - [S=1]
# otherwise, serve your index.html app
RewriteRule ^(.+)$ /index.php
# - See more at: http://newmediascientist.com/posts/redirecting-into-angularjs-for-friendly-urls/#sthash.5Oln1Wzh.dpuf

我的API位于 HTTP下://localhost.local/api 目录中,有一个的index.php文件,该文件作为控制器,所以角使得其REST调用的http://localhost.local/api/的index.php / {{CTRL}} / {{ID}} q = {{其他PARAMS}}

My api is located under the http://localhost.local/api directory, has a index.php file which acts as the controller, so Angular makes its REST calls to http://localhost.local/api/index.php/{{ctrl}}/{{id}}?q={{other params}}

我似乎无法得到相同的配置下,nginx的工作,任何请求将被自动重定向到的index.php文件(角入口点),而不是API的入口点。现在我不使用死心塌地localhost.local / API /的index.php?{{CTRL}} ...其实我也preFER清洁localhost.local / API的/ etc {{CTRL}} ..

I cannot seem to get this same configuration to work under nginx, any request is automatically redirected to the /index.php file (the Angular entry point) and not to the api entry point. Now I am not dead set on using localhost.local/api/index.php?{{ctrl}}... in fact I would prefer a cleaner localhost.local/api/{{ctrl}} etc...

我只是无法实现我的previous服务器上。我现在的非工作nginx的配置如下:

I was just unable to achieve that on my previous server. My current non-working nginx configuration is as follows:

server {
    listen 80;
    root /var/www/htdocs;
    index index.php;
    server_name localhost.local;
    access_log /var/log/nginx/temperatures.access.log;
    error_log /var/log/nginx/temperatures.error.log debug;
    add_header "X-UA-Compatible" "IE=Edge,chrome=1";
    location / {
            try_files $uri $uri/ /index.php;
    }
    location /api {
            index /api/index.php;
            try_files /api/index.php/$uri?$args;
    }
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/tmp/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
    location ~ /\.ht {
            deny all;
    }
}

任何帮助将大大pciated这个AP $ P $!

Any help would be greatly appreciated on this!

修改同时尝试这给正确的URL服务器以下,但它不将它传递给PHP。

Edit Also tried the following which gives the correct url to the server, however it doesn't pass it to php.

location ~ /api/ {
    rewrite ^/api(/.*)$ /api/index.php$1 break;
    }

编辑2
修改code这样:

Edit 2 Modifying the code to this:

location ~ /api/ {
            try_files $uri $uri/ /api/index.php$is_args$args;
            location ~ ^/api/index.php(.*)$ {
                    fastcgi_index index.php;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass unix:/tmp/php5-fpm.sock;
                    include fastcgi_params;
            }
    }

得到最好的结果,因为我可以得到服务器正常到正常的请求(API / {{收集}} / {{ID}}但是如果我在一个GET参数添加它打破了整个过程,因为响应不再是REQUEST_URI和GET PARAMS,因为有与Apache的PATH_INFO之间的分离。

yields the best results, as I can get the server to respond properly to a normal request (api/{{collection}}/{{id}} however if I add in a GET param it breaks the entire process as there is no longer a seperation between the request_uri and the get params as there was with Apache's PATH_INFO.

推荐答案

作为一个从这个帖子年前这里跟进的是我一直在用我现在的角度应用程序使用PHP的API:

As a follow up from this post years ago here is what I have been using for my current Angular applications with php apis:

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;
  root /usr/share/nginx/html;
  index index.html;
  server_name localhost;
  location / {
    try_files $uri $uri/ /index.html =404;
  }

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    fastcgi_pass localhost:9000;
    include fastcgi_params;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

另一种选择,我发现,如果你的API所在的API目录下,你想有一个更简洁的API语法(如本地主机/ API /职位以代替localhost / API / index.php文件/个)是:

Another option I found if your api is located under an api directory and you want a cleaner api syntax (e.g. localhost/api/posts instead of localhost/api/index.php/posts) is:

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;
  root /usr/share/nginx/html;
  index index.html;
  server_name localhost;
  location / {
    try_files $uri $uri/ /index.html =404;
  }

  location /api/ {      
    index /api/index.php;
    try_files $uri /api/index.php/$uri;
  }

  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    fastcgi_pass localhost:9000;
    include fastcgi_params;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }

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

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