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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)