我刚刚设置了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工作...并添加额外的参数
6条答案
按热度按时间e5nqia271#
最好避免在nginx配置中进行不必要的重写(参见Nginx Pitfalls),尤其是负责将请求传递到Laravel前端控制器的重写:
Laravel所需的全部内容是:
首先尝试直接访问文件,然后是目录,如果两者都不存在,则将请求传递给index.php。传递
$query_string
很重要,因为它将包含$_GET
数据,否则会丢失这些数据。这是我自己的FastCGI配置:
至于意外的输入,可能是当前重写的工作方式,但可以肯定的是,您输出的是什么?
ghg1uchk2#
这对我很有效:
dgiusagp3#
从您的配置:
此处您将重定向到
/index.php?/$1
(例如/index.php?/some/path
)。这里你用
^(.+\.php)(/.+)$
正则表达式分割路径(例如/index.php/some/path
)。你注意到区别了吗?
sq1bmfud4#
我遇到了类似的问题,我用以下配置修复了它:
vmpqdwk35#
这是一个适合我使用NGINX和Laravel的配置
cfh9epnr6#
修复了NGINX文档中描述的相同问题:嵌入变量:$是参数
在NGINX位置添加此内容: