Laravel routes not found after nginx install

前端 未结 6 763
失恋的感觉
失恋的感觉 2020-12-24 02:17

After I changed ICG to nginx all routes except index page does not work.

Laravel Config:

#/etc/nginx/sites-enabled/laravel
server {
    listen 80;

          


        
相关标签:
6条回答
  • 2020-12-24 02:40

    I found that this solved my problem with Laravel routing. I nested the location ~ .php$ inside location /.

    Example:

    server{
        listen 9000;
    
        server_name _;
    
        root /var/www/myapp/public;
    
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
    
            location ~ \.php$ {
    
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-24 02:50

    I was also getting the same error of Routes not working on Nginx on my Ubuntu 16.04

    To solve Routes problem, i tried the following code and its just working fine for me.

    Open project conf file using following command

    sudo nano /etc/nginx/sites-available/projectname 
    

    Then do the following changes in this file

    server {
            listen 80;
            listen [::]:80;
    
           root /var/www/project_name/public;
    
           server_name server_name;
    
    location / {
      
                 try_files $uri $uri/ /index.php$is_args$args;
             }
    
    }
    

    Important thing is to change the try_files in location block.

    location / {
          
                     try_files $uri $uri/ /index.php$is_args$args;
                 }
    
    0 讨论(0)
  • 2020-12-24 02:52

    This is the correct basic config for Laravel and Nginx:

    server {
        listen   80 default_server;
    
        root /var/www/laravel/public/;
        index index.php index.html index.htm;
    
        location / {
             try_files $uri $uri/ /index.php$is_args$args;
        }
    
        # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
    }
    

    EDIT: Instead of:

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    

    As of November 2018, as PHP 7.2 is out, it would be:

    fastcgi_pass unix:/var/run/php7.2-fpm.sock;
    
    0 讨论(0)
  • 2020-12-24 02:52

    I had the same issue, but updating the default configuration made it work.

    location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }
        location / {
                try_files $uri $uri/ @rewrite;
        }  
    

    Let me know if this worked for you or not.
    sudo service nginx restart after changing the configuration.

    0 讨论(0)
  • I had same problem after updating few lines nginx working fine..

    It's for windows ( change root acording your file system )

    1.root html/laravel; #Update Here - add project folder name after html

    2.try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error

    server {
        listen 80; # IPv4
        server_name localhost;
    
        ## Parametrization using hostname of access and log filenames.
        access_log logs/localhost_access.log;
        error_log logs/localhost_error.log;
    
        ## Root and index files.
        root html/laravel; #Update Here - add project folder name after html
        index  index.php index.html index.htm;
    
        ## If no favicon exists return a 204 (no content error).
        location = /favicon.ico {
            try_files $uri =204;
            log_not_found off;
            access_log off;
        }
    
        ## Don't log robots.txt requests.
        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }
        ## Try the requested URI as files before handling it to PHP.
        location / {
    try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
            ## Regular PHP processing.
            location ~ \.php$ {
                try_files  $uri =404;
                fastcgi_pass   php_processes;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
    
            ## Static files
            location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
                expires max;
                log_not_found off;
                ## No need to bleed constant updates. Send the all shebang in one
                ## fell swoop.
                tcp_nodelay off;
                ## Set the OS file cache.
                open_file_cache max=1000 inactive=120s;
                open_file_cache_valid 45s;
                open_file_cache_min_uses 2;
                open_file_cache_errors off;
            }
    
            ## Keep a tab on the 'big' static files.
            location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
                expires 30d;
                ## No need to bleed constant updates. Send the all shebang in one
                ## fell swoop.
                tcp_nodelay off;
            }
            } # / location
    
    } 
    
    0 讨论(0)
  • 2020-12-24 02:55

    When I sent parameters by get I did not recognize them, I just have to activate the following: try_files $uri $uri/ /index.php$is_args$args;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    
    0 讨论(0)
提交回复
热议问题