1

I have a config that handles different "websites" a main root handles the requests for the main url and I specify a specific path for it, in another location /mailtracker I handle a different app and point it to a different directory. My problem is php-fpm is not finding this second location.

Basically I want to have several locations pointing to different roots.

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

    root /var/www/sites/dorero/;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

location /mailtracker {
    alias /var/www/sites/mailtracker/web/;
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 192.168.10.3:9000;
                fastcgi_index index.php;
            include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
 }

    location ~ ^/(index|app|app_dev|config)\.php(/|$) {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
    ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #       # With php5-cgi alone:
   fastcgi_pass 192.168.10.3:9000;
    #       # With php5-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    }

I tryied specifying instead of $document_root the path to the alias /var/www/sites/mailtracker/web/ and it didn't work.

Output from the php-fpm to this config says:

192.168.10.4 -  06/Feb/2018:19:04:49 +0000 "GET /mailtracker/index.php" 404
Fernando André
  • 1,213
  • 3
  • 19
  • 32

1 Answers1

1
  1. Put location /mailtracker above location / because nginx matched route /mailtracker for / before checking /mailtracker
  2. You can remove location ~ \.php$ inside location /mailtracker
  3. Add try_files $uri $uri/ =404; to location /mailtracker

edit:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/sites;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;
    location @rewriteapp {
           rewrite ^(.*)$ /app.php/$1 last;
    }

    location /mailtracker {
        alias /var/www/sites/mailtracker/web;
        set $subfolder "mailtracker/web";
        try_files $uri @rewriteapp;
    }

    location / {
            root /var/www/sites/dorero;
            set $subfolder "dorero";
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }


    location ~ ^/(index|app|app_dev|config)\.php(/|$) {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            #
            #       # With php5-cgi alone:
            fastcgi_pass 192.168.10.3:9000;
            #       # With php5-fpm:
            #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME    $document_root/$subfolder/$fastcgi_script_name;
    }
}
ttomalak
  • 1,936
  • 13
  • 19
  • Didn't work, I get File Not found from php-fpm . GET /mailtracker/index.php" 404 – Fernando André Feb 06 '18 at 19:44
  • there is a index.php – Fernando André Feb 06 '18 at 19:58
  • Didn't work, I already had tryied that, I'm doing something wrong. – Fernando André Feb 06 '18 at 20:08
  • nginx is placing the /mailtracker/ in the request to php-fpm – Fernando André Feb 06 '18 at 20:21
  • Sorry, only now I see your edit, I tried the above config and the main location / isn't working giving 403 Forbiden and /mailtracker/ is not interpreting the php – Fernando André Feb 06 '18 at 20:26
  • On your example I had to move set $subfolder "dorero"; to the main server { so it would serve the correct page the location / isn't being accessable. – Fernando André Feb 07 '18 at 12:53
  • I changed the location ~ ^/(index... to just ~ \.php(/$) { and the file under mailtracker started being served to php-fpm but the location of the file being passed is wrong, it's sending the path has /mailtracker/index.php . – Fernando André Feb 07 '18 at 13:00
  • @FernandoAndré I see that I not placed semicolon in `location /` did you corrected that and I was still not working? About this route and passing it you probably need some @rewriteapp to serve that url properly. I added sample rewrite for symfony applications and also corrected that semicolon – ttomalak Feb 07 '18 at 14:07