简体   繁体   中英

Nginx configuration for Laravel 5.2 and PHP 7: Routes not working

I'm trying to set up an AWS Ubuntu server to host a Laravel project. I'm having issues getting the routes working, and I suspect my Nginx virtual host config file is the culprit.

I think specifically the try_files line is the issue? I've tried try_files $uri $uri/ /index.php$is_args$args; as well as try_files $uri $uri/ /index.php?$query_string; , as well as a few others, with several wrong results, including:

  • 404 errors
  • 500 errors
  • Being linked to a download of the source code
  • And it's current behavior, being redirected to /index.php/ (which shows the Laravel welcome page).

Here's my /etc/nginx/sites-enabled/default:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html/public;
    index index.php index.html index.htm;

    # Make site accessible from amazon aws
    server_name xxxx.us-west-2.compute.amazonaws.com;

    location / {
            # Try file, then try folder, then pass to Laravel's /public/index.php
            try_files $uri $uri/ /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

This is the current status of the file, and like I said, it currently redirects any real routes to blahblah.com/index.php/ and displays the Laravel welcome page. Fake routes that haven't been set up result in the Laravel 404 page.

I'm stumped because there's several guides to this and I've tried all the suggestions I can find, so I must be missing something.

It would appear php7 is working correctly and isn't the issue, or could this be a problem in a php config file?

DEVELOPMENTS: It seems to work correctly if I access the site using its IP (Thanks Oliver Queen), any idea how I can get this to work when using the domain name?

Replace your current location / with the following code:

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

And then replace the location below it with:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

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