0

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 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this configuration as well to no effect, please see my edit above I am not sure if this is closer to what I need, but the url is coming out correctly anyway, it is just not passed to the php index.php for controller processing.
1

As a follow up from this post years ago here is what I have been using for my current Angular applications with php apis:

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

Another option I found if your api is located under an api directory and you want a cleaner api syntax (e.g. localhost/api/posts instead of localhost/api/index.php/posts) is:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.