403 Forbidden on nginx/1.4.6 (Ubuntu) - Laravel

2020-03-01 03:37发布

问题:

I keep getting 403 Forbidden


My settings:

/etc/nginx/sites-available/default

default

server {
        listen   80;


        root home/laravel-app/;

        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                try_files $uri =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;

        }

}

Update

I followed this instruction : here


Any hints/suggestions on this will be a huge help !

回答1:

You need to specify an absolute path for your root directive. Nginx uses the directory set at compile time using the --prefix switch. By default this is /usr/local/nginx.

What this means is that your root, which is currently set to root home/laravel-app/ causes nginx to look for files at /usr/local/nginx/home/laravel-app/ which presumably isn't where your files are.

If you set your root directive to an absolute path such as /var/www/laravel-app/public/ nginx will find the files.

Similarly you'll note that I added /public/ to the path above. This is because Laravel stores it's index.php file there. If you were to just point at /laravel-app/ there's no index file and it'd give you a 403.



回答2:

you need to have a rule for php files (in the default file)

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

        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}


回答3:

The error you added in your update indicates that nginx is attempting a directory index from your ssc-portal folder. Since you appear to be using a basic nginx installation, the only reason a directory index should fail here is if nginx is unable to find the listed index options.

In your server block, you are telling nginx to try the following locations in order when a directory listing is requested (a URI that ends with a trailing slash): index.php, index.html, then index.htm.

If none of those files is found, the directory index request fails.

My best guess is you have your index.php file in the wrong place. Did you move it up from the ssc-portal folder?