我正在将我的apache切换到nginx服务器,除了api的配置之外,一切似乎都很顺利。我在apache下的旧配置依赖于根目录中的.htaccess文件将所有请求重定向到角度入口点(在本例中为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对http://localhost.local/api/index.php/{{ctrl}}/{{id}}?q={{其他参数}}
我似乎无法在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 param,它会中断整个过程,因为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;
}
作为几年前这篇文章的后续内容,以下是我在当前使用phpapi的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;
}