Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly configure alias directive in nginx?

Tags:

alias

php

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.

# 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 !

like image 491
Lich4r Avatar asked Feb 13 '15 00:02

Lich4r


People also ask

What is alias directive in NGINX?

NGINX Alias Directive Alias directive allows you to remap URLs to a different directory other than root location. It is useful for serving static files from a different directory. For example, if Alias is /var/www/html/css for location /static/, then when a user requests /static/style.

Where do I put NGINX directive?

NGINX location directive syntax The NGINX location block can be placed inside a server block or inside another location block with some restrictions. The syntax for constructing a location block is: location [modifier] [URI] { ... ... } The modifier in the location block is optional.

What is var www html in NGINX?

Introduction. On Ubuntu, the Nginx web server stores its documents in /var/www/html , which is typically located on the root filesystem with the rest of the operating system. Sometimes, though, it's helpful to move the document root to another location, such as a separate mounted filesystem.

What is Sendfile on in NGINX?

Enabling sendfile By default, NGINX handles file transmission itself and copies the file into the buffer before sending it. Enabling the sendfile directive eliminates the step of copying the data into the buffer and enables direct copying data from one file descriptor to another.


1 Answers

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.

like image 105
Lich4r Avatar answered Sep 18 '22 17:09

Lich4r