17

I have been trying to configure multiple webapp on my nginx webserver but I can't get working one Laravel app that requires $document_root set to laravel public folder. I am currently trying to configure it using alias directive but for an obscure reason this doesn't work. Here is what I am trying to do.

# Default server configuration
#
server {
    listen 80;

    # SSL configuration
    #
    listen 443 ssl;

    error_log /var/log/nginx/error.log warn;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;


    set $root_path '/var/www/html';
    root $root_path;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;

    server_name localhost;

    location /paperwork {
        alias /var/www/html/paperwork/frontend/public;
        try_files $uri $uri/;
        #location ~ \.php {
        #   fastcgi_split_path_info ^(.+\.php)(.*)$;
        #   fastcgi_pass unix:/var/run/php5-fpm.sock;
        #   include /etc/nginx/fastcgi_params;
        #   #fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        #   #fastcgi_intercept_errors on;
        #}
    }

    #location @paperwork {
    #   rewrite /paperwork/(.*)$ /paperwork/index.php/$1 last;
    #}

    location / {

    }


    location /wallabag {
        try_files $uri $uri/ /index.php;
    }

    location /laverna {
        try_files $uri/ /index.php;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #try_files $uri $uri/ =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }



    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\.ht {
        deny all;
    }
}

To test my "alias" config I put a 'test.php' files in /var/www/html/paperwork/frontend/public/test.php and tried to access it via https://IP/paperwork/test.php. I get a 404 error and nothing in nginx error log. If I try https://IP/paperwork/frontend/public/test.php in browser it displays the test.php file without errors. Nothing change if I uncomment try_files line in php location.

If I copy test.php to /var/www/html/paperwork/test2.php and access to https://IP/paperwork/test2.php the file is displayed without errors so I can see here that alias is not working as there is not a test2.php in paperwork public directory.

I can have a different behaviour if I uncomment php location inside paperwork location. With this, requests like https://IP/paperwork/test.php do not display a 404 but a blank screen.

I have been through a lot of forums / questions related to this but I couldn't get a working config for a simple task like displaying test.php...

Thanks !

1
  • 1
    Thanks for such a helpful question.. sir, you made my day Mar 29, 2021 at 21:58

2 Answers 2

57

I found the solution. It seems that a wrong request was sent for php files. When alias is used it is recommend to use $request_filename instead of $fastcgi_script_name.

Here is my location block :

location /paperwork {

      alias /var/www/html/paperwork/frontend/public;
      #try_files $uri $uri/;
      location ~ \.php$ {
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          include fastcgi_params;                       
          fastcgi_param SCRIPT_FILENAME $request_filename;
          #fastcgi_intercept_errors on;
      }
}

This solved my problem for my 'test.php' file which is now executed while reaching https://IP/paperwork/test.php. So alias is working and php is well executed. I still have a problem when trying to reach 'index.php' (which is my laravel app index). File is found but instead of executing it is downloaded. So when I reach https://IP/paperwork/index.php I get a login file downloaded which is index.php file. I get same behaviour if I try /paperwork/index.php/login or /paperwork/login.

5
  • 8
    Yes! thank you. The key was using $request_filename. Every other example had fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name but that didn't work.
    – heisian
    May 2, 2016 at 20:31
  • I am having the same issue and not able to to execute the index.php file. I also tried setting up index index.php in my location but no luck. May 19, 2018 at 11:00
  • 1
    directive alias /some/path; + fastcgi_param SCRIPT_FILENAME $request_filename; needs directive index index.php;at a location level to work with index.php also
    – hrvoj3e
    Nov 23, 2018 at 8:44
  • There doesn't seem to be a way to make this (or anything else) work with index files. Adding index index.php; will just download the file.
    – bmakan
    Feb 21, 2020 at 9:20
  • 1
    OK, important note: file downloads are cached! Adding index index.php; to the /paperwork location does work. But you need to clear the cache or index.php will not get executed and will be downloaded instead.
    – bmakan
    Feb 21, 2020 at 10:02
0

try this:

location /api/ {
     index  index.php index.html index.htm;
     alias /app/www/;
     location ~* "\.php$" {
         try_files      $uri =404;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_param  SCRIPT_FILENAME  $request_filename;
     }
 }

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.