我刚刚设置了 Nginx,我试图用它来托管 Laravel 应用程序,但我遇到了两个问题。
REQUEST_URI
它不应该。示例输出:.
Array (
[/api/user] => // This shouldn't be here
[test] => test
)
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/