繁体   English   中英

PHP api的AngularJS nginx配置

[英]AngularJS nginx configuration for PHP api

我正在将我的apache切换到nginx服务器,除了我的api的配置之外,一切看起来都很顺利。 我在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

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

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

我只是无法在以前的服务器上实现这一点。 我目前的非工作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;
    }
}

任何帮助将非常感谢!

编辑还尝试了以下为服务器提供正确的URL,但它没有将它传递给PHP。

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

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

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

将第一个位置块移动到其余位置块下方。 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;
}

作为几年前这篇文章的后续文章,这里是我用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;
  }
}

如果您的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