【问题标题】:Config nginx for Laravel In a subfolder为 Laravel 配置 nginx 在子文件夹中
【发布时间】:2015-03-03 08:03:25
【问题描述】:

我有一个旧项目现在需要新功能,我将使用 laravel 来提供它, 使用 apache 在 xampp 中一切正常,但我的服务器 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;
    }
}

【问题讨论】:

    标签: php laravel nginx


    【解决方案1】:

    我在这个问题上花了几个小时后,终于用这样的子域地址解决了我的问题:

    如果你想把你的laravel 项目放在subfolder 的服务器上ngnix-ubuntu 16-php.7.2,那么这里是 ngnix 配置:

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

      /var/www/main: /var/www/嵌套:

    那么你的配置应该是:

    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;
    }
    
    1. 主文件夹内的laravel-test文件夹(子文件夹):

      /var/www/main: /var/www/main/嵌套:

    那么你的配置应该是:

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

    【讨论】:

    • 谢谢,第一个选项有效。为什么rewrite /nested/(.*)$ /nested/index.php?/$1 last; 而不是rewrite /nested/(.*)$ /nested/public/index.php?/$1 last;
    • 2个配置没有区别
    • 这就是答案。感谢您抽出宝贵时间。
    • 感谢您的解决方案!我一直在寻找这个问题的解决方案。
    • 我很高兴地报告我尝试了这个并且它有效。我正在运行使用 nginx 的 Homestead。
    【解决方案2】:

    这是解决我问题的解决方法 使用别名,Nginx 不会像使用 root 指令那样在 /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

    【讨论】:

    • 这是我在整个互联网上找到的唯一答案,而且超出了这个范围。最重要的部分是:删除?/$1。不要删除它,您的查询字符串将全部失败。非常感谢!
    • 在我的情况下,这行得通,因为其他 nginx.conf 建议使用 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;,但是使用 fastcgi_param SCRIPT_FILENAME $request_filename; 最终修复了我的服务器并让我安心。
    • 非常感谢您的评论。删除这 4 个字符 ?/$1 对我来说效果很好。在 Laravel 8 更新之前,我的系统可以很好地处理这些字符,然后 Laravel 将 $request-&gt;server-&gt;get('QUERY_STRING') 添加到签名路由的评估中,这让我的系统出现了问题。现在解决了。
    【解决方案3】:

    出于某种原因,别名导致了问题并且不起作用。 所以也许这会帮助其他人,所以这就是我为完成这项工作所做的工作。 如您所见,我去掉了“别名”并将 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;
        }
    

    【讨论】:

    • 要获得更清晰和建设性的答案,您可以添加更多详细信息,说明该解决方案如何以及为什么对您有效,以使其更易于理解。有了它,请继续努力,感谢您分享您对问题的可能解决方案:)
    • 我不确定为什么它不适用于别名我不完全理解 Nginx 配置的工作原理。我不确定它有没有别名有什么不同。
    【解决方案4】:

    位置 /stationery { 别名 /var/www/html/stationery/public; try_files $uri $uri/ @stationery;位置 ~ .php$ { 包括 sn-ps/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; }

    【讨论】:

    • 请在您提供的答案中添加一些描述。
    【解决方案5】:

    请使用这个:

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

    }

    【讨论】:

    • 这可能会解决OP问题,但它是一个完整的配置。它无助于找出 OP 的情况出了什么问题,以及究竟是什么解决了问题。你能把这些观点说得更清楚更明确吗?
    【解决方案6】:

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

    1. 我创建了一个“普通”的 laravel 域,指向 http://generic.laravel 根 /my/laravel/path/public

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

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

      }

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

    【讨论】:

      【解决方案7】:

      好吧,我找到了一个非常容易配置的解决方案,并将 Laravel 安装在 nginx 服务器的子目录中,在 /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;
      }
      

      瞧,您的路线将按照应有的方式正常运行。

      【讨论】:

      • 这解决了我几天前遇到的问题。谢谢
      • 这很好用,但如果我这样做:example.org/laravel/?var=12345 它会给我一个 404 错误。任何想法如何解决它,问候!
      • @Alberto 确保重新加载 nginx 和 php5-fpm
      • 那不行。它强制 php 文件下载而不是提供服务
      猜你喜欢
      • 1970-01-01
      • 2023-02-04
      • 2017-05-30
      • 2017-07-15
      • 2015-09-26
      • 2019-11-17
      • 1970-01-01
      相关资源
      最近更新 更多