我刚刚设置了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;
}
至于意外的输入,它可能是你当前重写的方式,但可以肯定地说,你输出了什么?