I'm refactoring a very old application and i'm stuck on Nginx configuration; All my tries ended up the PHP script being downloaded instead of executed, or ended up with "File not found"
Directory Structure
public
│
└─── front
│ │ index.php
│ │
│ │
│ └─── web_www.example.com
│ │ index.php
│ │ style.css
│ │ export/
...
The GOAL
All URL, except static files (.js
, .css
...) must be routed to public/front/index.php
Example:
- http://www.example.com/ ->
public/front/index.php
- http://www.example.com/index.php ->
public/front/index.php
- https://www.example.com/mag/fr/contact.php ->
public/front/index.php
- https://www.example.com/product-1.htm ->
public/front/index.php
- https://www.example.com/style.css ->
serve the file
Why some of the URL look likes as if it is a php script to be executed ?
Because it's an old app which worked this way; Now, only 1 front controller (public/front/index.php
) is responsible of executing the code
Base, not working vhost
server {
...
root /srv/app/public/front;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args =404;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.app.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico)$ {
try_files $uri =404;
}
rewrite ^/(.*)$ /web_$http_host$uri break;
}
What am I doing wrong / missing please ?
EDIT 1
Adding try files to the location, like suggested by @Florentin Stemate :
location ~ \.php$ {
try_files /index.php?$args $uri;
....
}
(I switched the order and placed $uri
at the end so we always try to go through front controller when the url contains .php
)
Doing so is working well for all pages except:
https://www.example.com/product-1.htm
=> now gives:
*2 rewrite or internal redirection cycle while internally redirecting to "/web_www.test-boutique.vm/web_www.test-boutique.vm/web_www.test-boutique.vm/web_www.test-boutique.vm/web_www.test-boutique.vm/....."