带有 Nginx 参数的 Laravel 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14854050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:33:47  来源:igfitidea点击:

Laravel with Nginx parameters are empty

postparametersnginxlaravel

提问by Alex

I just set up Nginx, and I'm trying to use it to host a Laravel app, but I ran into 2 problems.

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

  1. For GET method, I always get an extra parameter in my inputs.
    • Using PostMan (Chrome) to do my testings, I set the destination URL and my desired parameters and send the request. The output that I get, it always includes the REQUEST_URIwhich it shouldn't. Example output:
  1. 对于 GET 方法,我总是在我的输入中得到一个额外的参数。
    • 使用 PostMan (Chrome) 进行测试,设置目标 URL 和所需参数并发送请求。我得到的输出,它总是包括REQUEST_URI它不应该的。示例输出:

.

.

Array (
  [/api/user] => // This shouldn't be here
  [test] => test
)
  1. My parameters (the above) will NOTshow for DELETE or PUT, at all, and for POST I'll only get the REQUEST_URI
  1. 我的参数(上面的)根本不会显示 DELETE 或 PUT,对于 POST 我只会得到REQUEST_URI

Nginx vhost(Followed Setting up Laravel w/ Nginx)

Nginx vhost(遵循使用Nginx设置 Laravel

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 ^/(.+)/$ / permanent;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?/ 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_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.confHas only 1 thing changed, and that is keepalive_timeoutfrom 65 to 15

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

So I absolutely have no clue, where all this thing goes wrong. But I do have to mention, that on another 2 environments that I have (One with Lighttpd and the other with Apache2) the app works perfectly.

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

From what I've noticed, its all reduced to the following code:

从我所注意到的,它都简化为以下代码:

# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename) {
    rewrite ^/(.*)$ /index.php?/ last;
    break;
}

Which will make the GET work... and add the additional parameter

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

回答by Gargron

It is best to avoid unneccessary rewrites in your nginx configuration (See Nginx Pitfalls), one in particular is the one responsible for passing the request to the Laravel front controller:

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

All you need for Laravel is:

Laravel 所需要的只是:

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

First that tries to access a file directly, then a directory, and if neither exists it passes the request to index.php. $query_stringis important to pass along as that will contain the $_GETdata that otherwise gets lost.

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

And here is my own FastCGI configuration piece:

这是我自己的 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;
}

As for unexpected input, it could be the way your current rewrite works, but to say for sure, what are you outputting?

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

回答by VBart

From your config:

从您的配置:

rewrite ^/(.*)$ /index.php?/ last;

here you have a redirect to /index.php?/$1(e.g. /index.php?/some/path).

在这里,您可以重定向到/index.php?/$1(例如/index.php?/some/path)。

fastcgi_split_path_info ^(.+\.php)(/.+)$;

and here you spilt path by ^(.+\.php)(/.+)$regex (e.g. /index.php/some/path).

在这里,您通过^(.+\.php)(/.+)$正则表达式(例如/index.php/some/path)溢出了路径。

Have you noticed the difference?

你注意到区别了吗?

回答by Jason Prawn

This works for me:

这对我有用:

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

location ~ \.php$ {

    include     fastcgi_params;
    fastcgi_pass   127.0.0.1:9000;
    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;

}

回答by Ravi Sharma

I was facing similar issue and I fixed it with following configs:

我遇到了类似的问题,我使用以下配置修复了它:

server {
    listen 80;
    server_name subdomain.domain.com;
    root /var/www/dir/public;

    charset utf-8;

    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/registration.app-error.log error;
    error_page 404 /index.php;
    sendfile off;

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

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

    location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }



    location ~ /\.ht {
        #deny all;
    }
}

回答by ajtrichards

This is a configuration that works for me with NGINX and Laravel

这是一个适用于 NGINX 和 Laravel 的配置

server {

    listen  80;
    server_name sub.domain.com;
    set $root_path '/var/www/html/application_name/public';
    root $root_path;

    index index.php index.html index.htm;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/;
    }

    location ~ \.php {

        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index /index.php;

        include /etc/nginx/fastcgi_params;

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

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }

}