The routing script of my application expects the URI to be
/api/v0/create
My request goes to
http://my-server.test/subdir/api/v0/create
How can I configure nginx to remove the "/subdir" from the URI, providing the expected path to the application's router?
I tried this one, but got only "404 File not found" from the server (not the app):
location /subdir/api/ {
rewrite ^/subdir(.*)$ $1 last;
try_files $uri $uri/ /subdir/api/public/index.php$is_args$args;
}
With the rewrite flag break
and without the rewrite
part altogether I get a 404 from my application for the subdir still in URI.
Edit:
I need a way without redirect to /api for there is another analogue URI http://my-server.test/anothersubdir/api/v0 which would have the same /api part.
All this comes down to the question: Is there a way to configure nginx to give the target application a rewritten URI in a way that for example $_SERVER['REQUEST_URI'] in php reflects it?
Edit:
Here is the complete nginx configuration:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# configuration file /etc/nginx/mime.types:
types {
text/html html htm shtml;
... omitted
}
# configuration file /etc/nginx/conf.d/php.conf:
server {
listen 80;
server_name my-server.test;
root /var/www;
location /subdir/api/ {
rewrite ^/subdir(.*)$ $1 last;
try_files $uri $uri/ /subdir/api/public/index.php$is_args$args;
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
Thank you for helping!