【发布时间】:2014-01-01 23:42:41
【问题描述】:
我正在将我的 apache 切换到 nginx 服务器,除了我的 api 配置之外,一切似乎都很顺利。我在 apache 下的旧配置依赖于根目录中的 .htacess 文件将所有请求重定向到 angular 入口点(在本例中为 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= {{其他参数}}
我似乎无法让相同的配置在 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 参数它会破坏整个过程不再像 Apache 的 PATH_INFO 那样将 request_uri 和 get 参数分开。
【问题讨论】:
标签: php apache api angularjs nginx