2

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:

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/....."

sf_tristanb
  • 8,725
  • 17
  • 74
  • 118
  • 1
    try adding `try_files $uri $uri/ /index.php?$args; ` in the `location ~ \.php$ { ` block – Stem Florin Oct 29 '19 at 11:02
  • Thank you, the page `https://www.example.com/mag/fr/contact.php` is now working but `https://www.example.com/product-1.htm` still gives 404. – sf_tristanb Oct 29 '19 at 16:36
  • See edits for details; – sf_tristanb Oct 29 '19 at 16:47
  • what about changing `location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico)$` to `location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico|htm|html)` – Stem Florin Oct 30 '19 at 09:05
  • Well If i do that, the URL containing .htm will be served trying to access the file (as if it was static files); but I want all .htm URL to go through /index.php – sf_tristanb Oct 30 '19 at 12:36
  • 1
    Well then I think you can update `location ~ \.php$ {` to `location ~ \.(php|htm)$ {` And that should also send requests for htm files to the index.php – Stem Florin Oct 30 '19 at 13:13
  • Thank you; the application is sending a 404 response and I thought it was nginx; that was misleading; Thanks very much ! – sf_tristanb Oct 30 '19 at 15:49

1 Answers1

0

You have an error in your try_files statement. Remove the =404 so that the /index.php$is_args$args is the last parameter.

For example:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

The last parameter of try_files statement can be a URI, status code or a named location. Choose one! See this document for details.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81