简体   繁体   中英

Serve Laravel application from nginx /location block (virtual host)

I am trying to serve laravel from a /location block in a Ubuntu nginx virtual host configuration. I have the laravel application installed and working fine when accessed directly but the nginx location block seems like not doing what it is expected to do.

This works : https://www.madol.example.com/horizontal-laravel/public/index.php

This doesn't (403): https://www.madol.example.com/horizontal-laravel/

Note: Real address omitted.

Main snippets which might be wrong:

root /var/www/madol.example.com;
server_name madol.example.com www.madol.example.com;
location /horizontal-laravel {
    try_files $uri $uri/ /horizontal-laravel/public/index.php;
}

Here is the full code from my config file-

server {

     root /var/www/madol.example.com;
     index index.php index.html;

     server_name madol.example.com www.madol.example.com;

     location / {
             try_files $uri $uri/ /index.php?$query_string;
     }

     location ~ \.php$ {
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
            include snippets/fastcgi-php.conf;
     }

     **# Something wrong here?**
     location /horizontal-laravel {
           try_files $uri $uri/ /horizontal-laravel/public/index.php;
     }

     location ~*  \.(jpg|jpeg|png|gif|svg|ico|css|js)$ {
            expires 7d;
}

     listen [::]:443 ssl; # managed by Certbot
     listen 443 ssl; # managed by Certbot
     ssl_certificate /etc/letsencrypt/live/madol.madcoderz.com/fullchain.pem; # managed by Certbot
     ssl_certificate_key /etc/letsencrypt/live/madol.madcoderz.com/privkey.pem; # managed by Certbot
     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
     if ($host = www.madol.example.com) {
            return 301 https://$host$request_uri;
     } # managed by Certbot

     if ($host = madol.example.com) {
            return 301 https://$host$request_uri;
     } # managed by Certbot


     listen 80;
     listen [::]:80;
     server_name madol.example.com www.madol.example.com;
     return 404;
} # managed by Certbot

Is there any other config issues apart from this in the code?

The index directive is looking for a file at /var/www/madol.example.com/horizontal-laravel/index.php and not finding anything.

The simplest solution is to expand the index directive to look for index.php within the public folder. See this document for details.

For example:

location /horizontal-laravel {
    index index.php public/index.php index.html;
    ...
}

Alternatively, you could stop index processing for this location by removing the $uri/ file term from the try_files directive. See this document for details.

For example:

location /horizontal-laravel {
    try_files $uri /horizontal-laravel/public/index.php;
    ...
}

You could explicitly redirect this one URI with a rewrite...last statement. See this document for details.

location = /horizontal-laravel/ { 
    rewrite ^ /horizontal-laravel/public/index.php last; 
}
location /horizontal-laravel {
    ...
}

Finally, you could redesign the URI scheme to eliminate the /public/ bit from being exposed. This is probably best achieved using an alias directive. You will need a nested location to execute the PHP scripts under the new root.

For example:

location ^~ /horizontal-laravel {
    alias /var/www/madol.example.com/horizontal-laravel/public;
    if (!-e $request_filename) { rewrite ^ /horizontal-laravel/public/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

Notice that the if directives and SCRIPT_FILENAME uses $request_filename to obtain the path to the local file. You will need to check what is inside snippets/fastcgi-php.conf to ensure it doesn't break anything.

Using try_files with alias is problematic due to this issue . See this caution on the use of if .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM