Just started working with Nginx (previously used Apache) and encountered the problem of configuring the project. There are 2 main files in the public folder:
- index.html - SPA application with History Api
- api.php - application API.
On Apache, the routing looked like this:
SPA (index.html)
- example.com/user
- example.com/user/1
- example.com/user/1/edit
- example.com/user/1/delete
API (api.php)
- example.com/api.php/user.list
- example.com/api.php/user.view
- example.com/api.php/user.edit
- xample.com/api.php/user.delete
How to configure such a routing operation in Nginx, so that if there is a / api prefix / data goes to api.php? and the rest in index.html?
I give examples of how it should work:
- example.com/param_1/param_2/param_n ==> index.html
- example.com / api / param_1 / param_2 / param_n.method ==> api.php
UPD # 1:
server { listen 8.8.8.8:80; server_name example.com; access_log /home/example/data/logs/example.access.log; error_log /home/example/data/logs/example.error.log; root /home/example/data/www/example.com/www; index index.html; rewrite ^/api$ /api.php last; charset utf-8; location / { if (-f $request_filename) { expires max; break; } if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") { rewrite ^(.*) /index.html last; } } location /api/ { try_files $uri $uri/ /api.php?$args; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ \.php($|/) { set $script $uri; set $path_info ""; if ($uri ~ "^(.+\.php)(/.+)") { set $script $1; set $path_info $2; } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
UPD # 2: - Public Folder Structure
- www /
- css /
- fonts /
- js /
- img /
- template /
- temp /
- uploads /
- api.php
- index.html