Skip to content
Advertisement

How to properly configure alias directive in nginx?

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.

JavaScript

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 !

Advertisement

Answer

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 :

JavaScript

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement