带有Nginx参数的Laravel为空

ulmd4ohb  于 2023-01-08  发布在  Nginx
关注(0)|答案(6)|浏览(178)

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

  • 对于GET方法,我总是在输入中得到一个额外的参数。
  • 我使用PostMan(Chrome)进行测试,设置目的地URL和所需参数,然后发送请求。我得到的输出总是包含REQUEST_URI,而这是不应该的。

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

1.对于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;
}

快速cgi参数

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, js, css, etc.), send to bootstrap
if (!-e $request_filename) {
    rewrite ^/(.*)$ /index.php?/$1 last;
    break;
}

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

e5nqia27

e5nqia271#

最好避免在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;
}

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

ghg1uchk

ghg1uchk2#

这对我很有效:

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;

}
dgiusagp

dgiusagp3#

从您的配置:

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

此处您将重定向到/index.php?/$1(例如/index.php?/some/path)。

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

这里你用^(.+\.php)(/.+)$正则表达式分割路径(例如/index.php/some/path)。
你注意到区别了吗?

sq1bmfud

sq1bmfud4#

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

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

vmpqdwk35#

这是一个适合我使用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=/$1;
    }

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

}
cfh9epnr

cfh9epnr6#

修复了NGINX文档中描述的相同问题:嵌入变量:$是参数
在NGINX位置添加此内容:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

相关问题