post - 带有 Nginx 参数的 Laravel 为空

标签 post parameters nginx laravel

我刚刚设置了 Nginx,我试图用它来托管 Laravel 应用程序,但我遇到了两个问题。

  • 对于 GET 方法,我总是在我的输入中得到一个额外的参数。
  • 使用 PostMan (Chrome) 进行测试,设置目标 URL 和所需参数并发送请求。我得到的输出,它总是包括 REQUEST_URI它不应该。示例输出:

  • .
    Array (
      [/api/user] => // This shouldn't be here
      [test] => test
    )
    
  • 我的参数(以上)会不是 显示 DELETE 或 PUT,对于 POST 我只会得到 REQUEST_URI

  • Nginx 虚拟主机 (已关注 Setting up Laravel w/ Nginx)
    server {
        server_name local.test.com;
        root /var/www/test/public;
    
        location / {
            index index.php index.html index.htm;
        }
    
        # serve static files directly
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
            access_log off;
            expires max;
        }
    
        # removes trailing slashes (prevents SEO duplicate content issues)
        if (!-d $request_filename) {
            rewrite ^/(.+)/$ /$1 permanent;
        }
    
        # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?/$1 last;
            break;
        }
    
        # catch all
        error_page 404 /index.php;
    
        # The PHP Inclusion Block
        # include /etc/nginx/includes/php;
        location ~ \..*/.*\.php$ {
            # I'm pretty sure this stops people trying to traverse your site to get to other PHP files
            return 403;
        }
    
        #location ~ \.php$ {
        location ~ \.php(.*)$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;
        }
    
    # Deny Any Access to .htaccess Files That May Be Present (not usually in issue in Laravel)
    # include /etc/nginx/includes/deny_htaccess;
    location ~ /\.ht
    {
        deny all;
    }
    
        error_log  /var/www/logs/test-error.log;
    }
    

    fastcgi_params :
    fastcgi_param   QUERY_STRING            $query_string;
    fastcgi_param   REQUEST_METHOD          $request_method;
    fastcgi_param   CONTENT_TYPE            $content_type;
    fastcgi_param   CONTENT_LENGTH          $content_length;
    
    fastcgi_param   SCRIPT_FILENAME         $request_filename;
    fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
    fastcgi_param   REQUEST_URI             $request_uri;
    fastcgi_param   DOCUMENT_URI            $document_uri;
    fastcgi_param   DOCUMENT_ROOT           $document_root;
    fastcgi_param   SERVER_PROTOCOL         $server_protocol;
    
    fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
    fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;
    
    fastcgi_param   REMOTE_ADDR             $remote_addr;
    fastcgi_param   REMOTE_PORT             $remote_port;
    fastcgi_param   SERVER_ADDR             $server_addr;
    fastcgi_param   SERVER_PORT             $server_port;
    fastcgi_param   SERVER_NAME             $server_name;
    
    #fastcgi_param  HTTPS                   $https;
    
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param   REDIRECT_STATUS         200;
    
    fastcgi_connect_timeout                 60;
    fastcgi_send_timeout                    180;
    fastcgi_read_timeout                    180;
    fastcgi_buffer_size                     128k;
    fastcgi_buffers 4                       256k;
    fastcgi_busy_buffers_size               256k;
    fastcgi_temp_file_write_size            256k;
    fastcgi_intercept_errors                on;
    

    nginx.conf 只有一件事改变了,那就是 keepalive_timeout从 65 到 15

    所以我完全不知道,这一切哪里出了问题。但我不得不提一下,在我拥有的另外 2 个环境(一个使用 Lighttpd,另一个使用 Apache2)上,该应用程序运行良好。

    从我所注意到的,它都简化为以下代码:
    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }
    

    这将使 GET 工作......并添加附加参数

    最佳答案

    最好避免在您的 nginx 配置中进行不必要的重写(参见 Nginx Pitfalls),特别是负责将请求传递给 Laravel 前端 Controller 的那个:

    Laravel 所需要的只是:

    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ index.php?$query_string;
    }
    

    首先尝试直接访问一个文件,然后是一个目录,如果两者都不存在,它将请求传递给 index.php。 $query_string重要的是传递,因为它将包含 $_GET否则会丢失的数据。

    这是我自己的 FastCGI 配置部分:
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }
    

    至于意外输入,这可能是您当前重写的工作方式,但可以肯定地说,您输出的是什么?

    关于post - 带有 Nginx 参数的 Laravel 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14854050/

    相关文章:

    c# - 在 ASP.NET 中使用 C# 接受 URL 编码的 POST 请求

    spring - 如何使用 Spring Data REST 发布嵌套实体

    java - 如何在后台正确使用 Android 的 HTTP POST 方法

    PHP POST 不工作

    javascript - 使用 javascript 优雅地修改现有链接的参数

    powershell - 在Powershell中转义args [0]

    optimization - SAS 帮助优化有限制的线性方程

    nginx - Videojs - 流未运行时隐藏错误

    nginx - 拒绝在框架中显示 'https://example.com/app/appName',因为它在 Shopify 中将 'X-Frame-Options' 设置为 'deny'

    nginx - 如何在Nginx中重写或代理URL?