为Laravel配置nginx在子文件夹中

时间:2015-01-05 18:12:21

标签: php laravel nginx

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

location /newsection/ { 
   try_files $uri $uri/ /newsection/public/index.php$request_uri;
}

但它会导致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;
    }
}

7 个答案:

答案 0 :(得分:12)

好吧,我找到了一个非常简单的配置解决方案,并在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;
}

瞧,您的路线将如何正常运作。

答案 1 :(得分:3)

花了几个小时终于可以修复网站和子域网站了:

如果您想将laravel项目放在具有subfolder的服务器上的ngnix-ubuntu 16-php.7.2中,那么这里是update ngnix config:

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

/var/www/main:
/var/www/nested:

然后配置:

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;
}

2)您主目录中的laravel-test文件夹(子文件夹):

/var/www/main:
/var/www/main/nested:

然后配置:

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;
}

答案 2 :(得分:1)

出于某种原因,别名导致了该问题,并且无法正常工作。 因此,这可能会对其他人有所帮助,所以这就是我所做的工作。 如您所见,我从中删除了“别名”,并在方程式中添加了laravel / public。

location ^~ /laravel/public {
            index home.php home.html home.htm index.html index.htm index.php;
            try_files $uri $uri/ @laravel;

            add_header X-Frame-Options "SAMEORIGIN";
            add_header X-XSS-Protection "1; mode=block";
            add_header X-Content-Type-Options "nosniff";
            charset utf-8;

            location ~ \.php {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_buffers 16 16k;
                fastcgi_buffer_size 32k;
                include fastcgi_params;
            }
        }
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

    location @laravel {
       rewrite /laravel/public/(.*)$ /laravel/public/index.php?/$1 last;
    }

答案 3 :(得分:1)

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

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
}

其他参考资料可以从本文https://gist.github.com/tsolar/8d45ed05bcff8eb75404

中找到

答案 4 :(得分:0)

它对我不起作用,所以我得到了另一个解决方案。

  1. 我创造了一个正常的&#34; laravel domain,指向http://generic.laravel root / my / laravel / path / public

  2. 之后,我在真实域上创建了一个代理我的通用Laravel的位置:

    location /laravel {
        rewrite /laravel/?(.*)$ /$1 break;
        proxy_pass http://generic.laravel;
    

    }

  3. 不幸的是,Laravel将使用网址http://generic.laravel来创建链接。您可以按照以下步骤Laravel: Change base URL?

  4. 解决此问题

答案 5 :(得分:-1)

location / stationery {别名/ var / www / html / stationery / public; try_files $ uri $ uri / @stationery;位置〜.php $ {包括snippets / fastcgi-php.conf; fastcgi_pass Unix:/run/php/php7.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $ request_filename; }

location @stationery {重写/stationery/(.*)$ /stationery/index.php?/ last; }

答案 6 :(得分:-2)

请使用此:

server {
client_body_in_file_only clean;
client_body_buffer_size 32K;

client_max_body_size 300M;

sendfile on;
send_timeout 300s;
# Port that the web server will listen on.
#listen          80;

# Host that will serve this project.
server_name     tsolar.com;

# Useful logs for debug.
access_log      /var/log/nginx/tsolar.com-access.log;
error_log       /var/log/nginx/tsolar.com-error.log;
rewrite_log     on;

# The location of our projects public directory.
root            /home/tom/public_html/demos/;

# Point index to the Laravel front controller.
index           index.php;

location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

location / {

    # URLs to attempt, including pretty ones.
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

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

# version 1
location ^~ /demo1 {
    alias /home/tom/public_html/demos/demo1/public;
    try_files $uri $uri/ @demo1;

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

location @demo1 {
    rewrite ^/demo1/(.*)$ /demo1/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 ~ /demo1 {
    try_files /demo1/$uri /demo1/$uri/ /demo1/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;
}

# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
    expires 365d;
}

}