douyoupingji7238 2013-12-14 03:54
浏览 38

PHP api的AngularJS nginx配置

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

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

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...

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

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.

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

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

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.

  • 写回答

2条回答 默认 最新

  • du7133 2013-12-14 18:01
    关注

    Move the first location block below the rest of the location blocks. 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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘