Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在子文件夹中为Laravel配置nginx_Php_Laravel_Nginx - Fatal编程技术网

Php 在子文件夹中为Laravel配置nginx

Php 在子文件夹中为Laravel配置nginx,php,laravel,nginx,Php,Laravel,Nginx,我有一个新的和旧的项目,现在需要新的功能,我将使用laravel来提供它, 使用apache在xampp中一切正常,但我的服务器con nginx向我显示拒绝访问消息,无法访问我的路由,我的站点配置应如何如果在mysite.com/2015中安装了laravel,我的站点配置如下,我更改了什么? 我试过了 location /newsection/ { try_files $uri $uri/ /newsection/public/index.php$request_uri; } 但它

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

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;
    }
}
服务器{
听80;
服务器名称:am2.aminversiones.com;
root/home/forge/am2.aminversiones.com;
#伪造SSL(不要删除!)
#ssl_证书;
#ssl_证书_密钥;
ssl_协议TLSv1 TLSv1.1 TLSv1.2;
index.html index.htm index.php;
字符集utf-8;
客户的最大车身尺寸为300米;
地点/{
#try_files$uri$uri//index.php?$query_string;
try_files$uri$uri//index.php?q=$uri&$args;
}
location=/favicon.ico{access\u log off;log\u not\u found off;}
location=/robots.txt{access\u log off;log\u not\u found off;}
访问/注销;
error_log/var/log/nginx/am2.aminversiones.com-error.log error;
错误\u第404页/index.php;
位置~\.php${
fastcgi\u split\u path\u info^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index.php;
fastcgi\参数脚本\文件名$document\根$fastcgi\脚本\名称;
包括fastcgi_参数;
}
#删除尾随斜杠以使路由系统满意。
如果(!-d$request\u文件名){
重写^/(.+)/$/$1永久性;
}
#版本1
地点^ ~/2015{
别名/home/forge/am2.aminversiones.com/2015/public;
try_files$uri$uri/@2015;
位置~*\.php{
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi\u split\u path\u info^(+.+\.php)(.*);
包括/etc/nginx/fastcgi_参数;
}
}
地点@2015{
最后重写^/2015/(.*)$/2015/index.php/$1;#这是重要的一行
}
#结束版本1
#版本2
#这是带有'ln-s/home/tom/public_html/demos/demo1/public/demo1'的`
地点~/2015{
try_files/2015/$uri/2015/$uri//2015/index.php?q=$uri&$args;
}
#结束版本2
#PHP FPM配置。
位置~*\.php${
fastcgi_pass unix:/var/run/php5-fpm.sock;
包括/etc/nginx/fastcgi_参数;
fastcgi_index.php;
fastcgi\u split\u path\u info^(.+\.php)(/.+)$;
fastcgi_参数路径_信息$fastcgi_路径_信息;
fastcgi\参数路径\翻译的$document\根$fastcgi\路径\信息;
fastcgi\参数脚本\文件名$document\根$fastcgi\脚本\名称;
}
#我们不需要使用nginx的.ht文件。
位置~/\.ht{
否认一切;
}
}

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

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

瞧,你的路线将正常运行。

这对我不起作用,所以我找到了另一个解决方案

  • 我已经创建了一个“正常”的laravel域,指向 root/my/laravel/path/public

  • 之后,我在real domain上创建了一个位置,代理到我的通用Laravel:

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

  • 不幸的是,Laravel将使用url创建链接。您可以按照以下步骤来解决它

  • 请使用这个:

    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;
    }
    
    服务器{
    客户端\u体\u在\u文件中\u仅清洁;
    客户端\主体\缓冲区\大小32K;
    客户的最大车身尺寸为300米;
    发送文件到;
    发送超时300秒;
    #web服务器将侦听的端口。
    #听80;
    #将为该项目提供服务的主机。
    服务器名称tsolar.com;
    #用于调试的有用日志。
    access_log/var/log/nginx/tsolar.com-access.log;
    error_log/var/log/nginx/tsolar.com-error.log;
    重写你的登录;
    #项目公共目录的位置。
    root/home/tom/public_html/demos/;
    #指向Laravel前控制器的点索引。
    index.php;
    位置~*\(jpg | jpeg | gif | css | png | js | ico | html)${
    访问/注销;
    最大值;
    }
    地点/{
    #要尝试的URL,包括漂亮的URL。
    try_files$uri$uri//index.php?q=$uri&$args;
    }
    #删除尾随斜杠以使路由系统满意。
    如果(!-d$request\u文件名){
    重写^/(.+)/$/$1永久性;
    }
    #版本1
    地点^~/demo1{
    别名/home/tom/public_html/demos/demo1/public;
    试试看文件$uri$uri/@demo1;
    位置~*\.php{
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi\u split\u path\u info^(+.+\.php)(.*);
    包括/etc/nginx/fastcgi_参数;
    }
    }
    地点@demo1{
    最后重写^/demo1/(.*)$/demo1/index.php/$1;#这是重要的一行
    }
    #结束版本1
    #版本2
    #这是带有'ln-s/home/tom/public_html/demos/demo1/public/demo1'的`
    地点~/demo1{
    try_files/demo1/$uri/demo1/$uri//demo1/index.php?q=$uri&$args;
    }
    #结束版本2
    #PHP FPM配置。
    位置~*\.php${
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    包括/etc/nginx/fastcgi_参数;
    fastcgi_index.php;
    fastcgi\u split\u path\u info^(.+\.php)(/.+)$;
    fastcgi_参数路径_信息$fastcgi_路径_信息;
    fastcgi\参数路径\翻译的$document\根$fastcgi\路径\信息;
    fastcgi\参数脚本\文件名$document\根$fastcgi\脚本\名称;
    }
    #我们不需要使用nginx的.ht文件。
    位置~/\.ht{
    否认一切;
    }
    #根据每个项目设置标题过期时间
    位置~*\(?:ico | css | js | jpe | g | JPG | png | svg | woff)${
    过期365d;
    }
    

    }

    在我花了几个小时讨论这个问题之后,我终于用如下子域地址修复了我的问题:

    如果你想把你的
    laravel
    项目放在一个
    子文件夹中
    在一个使用
    ngnix ubuntu 16 php.7.2
    的服务器上,那么我
    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;
    }
    
    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;
    }
    
    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;
        }
    
    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
    }