How to properly configure alias directive in nginx

2019-03-20 17:02发布

问题:

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:

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.



标签: php nginx alias