简体   繁体   English

PHP api的AngularJS nginx配置

[英]AngularJS nginx configuration for PHP api

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. 我正在将我的apache切换到nginx服务器,除了我的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: 我在apache下的旧配置依赖于根目录中的.htacess文件将所有请求重定向到角度入口点(在本例中为index.php),如下所示:

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

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}} 我的api位于http://localhost.local/api目录下,有一个index.php文件作为控制器,因此Angular将其REST调用到http://localhost.local/api/index.php/ {{ctrl}} / {{id}}?q = {{other params}}

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. 我似乎无法在nginx下使用相同的配置,任何请求都会自动重定向到/index.php文件(Angular入口点)而不是api入口点。 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... 现在我还没有开始使用localhost.local / api / index.php?{{ctrl}} ...其实我更喜欢一个更干净的localhost.local / api / {{ctrl}}等...

I was just unable to achieve that on my previous server. 我只是无法在以前的服务器上实现这一点。 My current non-working nginx configuration is as follows: 我目前的非工作nginx配置如下:

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

Any help would be greatly appreciated on this! 任何帮助将非常感谢!

Edit Also tried the following which gives the correct url to the server, however it doesn't pass it to php. 编辑还尝试了以下为服务器提供正确的URL,但它没有将它传递给PHP。

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

Edit 2 Modifying the code to this: 编辑2修改代码:

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

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. 产生最好的结果,因为我可以使服务器正确响应正常请求(api / {{collection}} / {{id}}但是如果我添加GET参数它会打破整个过程,因为不再request_uri和get params之间的分离与Apache的PATH_INFO一样。

Move the first location block below the rest of the location blocks. 将第一个位置块移动到其余位置块下方。 IE: IE:

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;
}
location / {
        try_files $uri $uri/ /index.php;
}

As a follow up from this post years ago here is what I have been using for my current Angular applications with php apis: 作为几年前这篇文章的后续文章,这里是我用php apis的当前Angular应用程序所使用的:

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

Another option I found if your api is located under an api directory and you want a cleaner api syntax (eg localhost/api/posts instead of localhost/api/index.php/posts) is: 如果您的api位于api目录下并且您想要更清晰的api语法(例如localhost / api / posts而不是localhost / api / index.php / posts),我发现了另一个选项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM