我刚刚设置了Nginx,我试图用它来托管Laravel应用程序,但我遇到了2个问题。
REQUEST_URI
。示例输出:
Array (
[/api/user] => // This shouldn't be here
[test] => test
)
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, js, css, etc.), send to bootstrap
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
这将使GET工作......并添加附加参数
答案 0 :(得分:5)
最好避免在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;
}
对于意外输入,它可能是您当前重写的方式,但可以肯定地说,您输出了什么?
答案 1 :(得分:1)
从你的配置:
rewrite ^/(.*)$ /index.php?/$1 last;
此处您可以重定向到/index.php?/$1
(例如/index.php?/some/path
)。
fastcgi_split_path_info ^(.+\.php)(/.+)$;
此处您通过^(.+\.php)(/.+)$
正则表达式(例如/index.php/some/path
)填充了路径。
您是否注意到了差异?
答案 2 :(得分:1)
这对我有用:
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;
}
答案 3 :(得分:0)
这是一个适用于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;
}
}
答案 4 :(得分:0)
我遇到了类似的问题,并通过以下配置对其进行了修复:
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;
}
}