为Laravel配置nginx在子文件夹中

Lu3*_*u32 13 php nginx laravel

我有旧项目,现在需要新的功能,我将使用laravel提供它,一切正常在xampp与apache工作,但我的服务器con nginx显示我访问被拒绝的消息,无法访问我的路线,我应该如何如果在mysite.com/2015中安装了laravel,我的网站配置如下,我改变了什么showld?我试过了

location /newsection/ { 
   try_files $uri $uri/ /newsection/public/index.php$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

但它会导致500错误

server {
    listen 80;
    server_name am2.aminversiones.com;
    root /home/forge/am2.aminversiones.com;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

    client_max_body_size 300M;

    location / {
        #try_files $uri $uri/ /index.php?$query_string;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/am2.aminversiones.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }

    # version 1
    location ^~ /2015 {
        alias /home/forge/am2.aminversiones.com/2015/public;
        try_files $uri $uri/ @2015;
        location ~* \.php {
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            include /etc/nginx/fastcgi_params;
        }
    }

    location @2015 {
        rewrite ^/2015/(.*)$ /2015/index.php/$1 last; # THIS IS THE IMPORTANT LINE
    }
    # end version 1

    # version 2
    # this is with `ln -s /home/tom/public_html/demos/demo1/public <document root>/demo1`
    location ~ /2015 {
        try_files /2015/$uri /2015/$uri/ /2015/index.php?q=$uri&$args;
    }
    # end version 2

    # PHP FPM configuration.
    location ~* \.php$ {
        fastcgi_pass                    unix:/var/run/php5-fpm.sock;
        include                             /etc/nginx/fastcgi_params;
        fastcgi_index                       index.php;
        fastcgi_split_path_info             ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO             $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED       $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME       $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
        deny all;
    }
}
Run Code Online (Sandbox Code Playgroud)

Lu3*_*u32 17

好吧,我找到了一个非常简单的配置解决方案,并在nginx服务器的子目录中安装Laravel,在/ etc/nginx/sites-available/yourSite配置文件中,添加:

location ^~ /laravel {
    alias /var/www/laravel/public;
    try_files $uri $uri/ @laravel;

    location ~ \.php {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }
}

location @laravel {
    rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
}
Run Code Online (Sandbox Code Playgroud)

瞧,你的路线将如何正常工作.


Ham*_*our 10

在这个问题上花了几个小时之后,终于可以用一个子域地址修复我的网站了:

如果要将laravel项目放在带有subfolder的服务器上ngnix-ubuntu 16-php.7.2,则这里是update ngnix config:

1)您的嵌套(子文件夹)不在主文件夹内

/var/www/main:
/var/www/nested:
Run Code Online (Sandbox Code Playgroud)

然后您的配置:

location /nested {

        alias /var/www/nested/public;

        try_files $uri $uri/ @nested;

               location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                                }
   }

location @nested {
        rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}
Run Code Online (Sandbox Code Playgroud)

2)主文件夹内的laravel-test文件夹(子文件夹):

/var/www/main:
/var/www/main/nested:
Run Code Online (Sandbox Code Playgroud)

然后您的配置:

location /laravel-test {

    alias /var/www/main/laravel-test/public;

    try_files $uri $uri/ @laravelTest;

           location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $request_filename;
                    fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                            }


  }

location @laravelTest {
        rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;
}
Run Code Online (Sandbox Code Playgroud)

  • 2个配置没有区别 (2认同)

lev*_*vis 10

这是解决我的问题的解决方法使用别名,Nginx 不会在 /var/www/portal/public/portal/foo 中查找文件,就像使用 root 指令一样

location /portal {
 alias /var/www/html/portal/public; #preferred over root

 # @portal is a named location
 try_files $uri $uri/ @portal;

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

location @portal {
rewrite /portal/(.*)$ /portal/index.php last; # Remove ?/$1 since fastcgi_params adds query string
}
Run Code Online (Sandbox Code Playgroud)

其他参考可以从这篇文章中找到:https://gist.github.com/tsolar/8d45ed05bcff8eb75404