加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

post – 带有Nginx参数的Laravel是空的

发布时间:2020-12-13 21:30:05 所属栏目:Nginx 来源:网络整理
导读:我刚刚设置了Nginx,我正在尝试使用它来托管一个Laravel应用程序,但我遇到了两个问题. 对于GET方法,我总是在输入中获得一个额外的参数. 使用PostMan(Chrome)进行测试,我设置目标网址和我想要的参数并发送请求.我得到的输出,它总是包括它不应该的REQUEST_URI.

我刚刚设置了Nginx,我正在尝试使用它来托管一个Laravel应用程序,但我遇到了两个问题.

>对于GET方法,我总是在输入中获得一个额外的参数.

>使用PostMan(Chrome)进行测试,我设置目标网址和我想要的参数并发送请求.我得到的输出,它总是包括它不应该的REQUEST_URI.示例输出:

.

Array (
  [/api/user] => // This shouldn't be here
  [test] => test
)

>我的参数(上面的)根本不会显示DELETE或PUT,而对于POST,我只会获得REQUEST_URI

Nginx vhost(关注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

所以我绝对不知道所有这一切都出错了.但我不得不提一下,在我拥有的另外两个环境中(一个使用Lighttpd,另一个使用Apache2),该应用程序运行良好.

从我注意到的,它全部缩减为以下代码:

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

这将使GET工作…并添加额外的参数

最佳答案
最好避免在nginx配置中不必要的重写(参见Nginx Pitfalls),特别是负责将请求传递给Laravel前端控制器的人:

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

至于意外的输入,它可能是你当前重写的方式,但可以肯定地说,你输出了什么?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读