【问题标题】:AngularJS nginx configuration for PHP apiPHP api的AngularJS nginx配置
【发布时间】: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


    【解决方案1】:

    作为多年前这篇文章的后续,这是我在当前的 Angular 应用程序中使用 php api 所使用的内容:

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

    【讨论】:

      【解决方案2】:

      将第一个位置块移动到其余位置块的下方。即:

      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 index.php 进行控制器处理。
      猜你喜欢
      • 2015-07-28
      • 1970-01-01
      • 2015-03-21
      • 2014-08-31
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2021-08-14
      相关资源
      最近更新 更多