nginx + php 7 + $ _GET params + Homestead

我的.htaccess是:

# nginx configuration location / { if (!-e $request_filename){ rewrite ^(.+)$ /index.php?url=$1 break; } } 

我有像这样configurationnginx:

 server { listen 80; listen 443 ssl http2; server_name prueba_nginx.net; root "/home/vagrant/Code/prueba_nginx/public"; index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } 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/prueba_nginx.net-error.log error; sendfile off; client_max_body_size 100m; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors off; fastcgi_buffer_size 16k; fastcgi_buffers 4 16k; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; } 

如果我打电话: http : //prueba_nginx.net/a/b/c

我运行php commant:echo $ _GET ['url']它必须返回::a / b / c,但是目前它返回:empty。

PHP:

 echo "\n".'$_GET'."\n"; var_dump($_GET); 

如果我用apache做了testing,它是完美的:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 

问题是什么? 是否有从apache转换到nginx的错误?


感谢迈克尔·汉普姆的快速回答。 我检查了$ _SERVER ['REQUEST_URI'],它返回正确的值:a / b / c,但我想知道为什么不使用$ _Get。 我做了你所取得的改变:.htaccess:

 # nginx configuration #location / { #if (!-e $request_filename){ #rewrite ^(.+)$ /index.php?url=$1 break; #} #} 

和我改变了nginx文件:

 location / { try_files $uri $uri/ /index.php?$query_string; } 

 location / { try_files $uri $uri/ /index.php?url=$uri; } 

但是,当我重新启动nginx我收到此错误消息:

[….]重新启动nginx(通过systemctl):nginx.serviceJob for nginx.service失败,因为控制进程退出错误代码。 有关详细信息,请参阅“systemctl status nginx.service”和“journalctl -xe”。 失败!

systemctl状态nginx.service

 status nginx.service ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Fri 2016-09-16 04:00:27 UTC; 5s ago Process: 2645 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 2559 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 2696 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE) Main PID: 2562 (code=exited, status=0/SUCCESS) Sep 16 04:00:26 homestead systemd[1]: Starting A high performance web server and a reverse proxy server... Sep 16 04:00:27 homestead nginx[2696]: nginx: [emerg] unknown "url" variable Sep 16 04:00:27 homestead nginx[2696]: nginx: configuration file /etc/nginx/nginx.conf test failed Sep 16 04:00:27 homestead systemd[1]: nginx.service: Control process exited, code=exited status=1 Sep 16 04:00:27 homestead systemd[1]: Failed to start A high performance web server and a reverse proxy server. Sep 16 04:00:27 homestead systemd[1]: nginx.service: Unit entered failed state. Sep 16 04:00:27 homestead systemd[1]: nginx.service: Failed with result 'exit-code'. 

journalctl -xe

 -- Unit nginx.service has begun starting up. Sep 16 04:00:27 homestead nginx[2696]: nginx: [emerg] unknown "url" variable Sep 16 04:00:27 homestead nginx[2696]: nginx: configuration file /etc/nginx/nginx.conf test failed Sep 16 04:00:27 homestead systemd[1]: nginx.service: Control process exited, code=exited status=1 Sep 16 04:00:27 homestead systemd[1]: Failed to start A high performance web server and a reverse proxy server. -- Subject: Unit nginx.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit nginx.service has failed. -- -- The result is failed. Sep 16 04:00:27 homestead systemd[1]: nginx.service: Unit entered failed state. Sep 16 04:00:27 homestead systemd[1]: nginx.service: Failed with result 'exit-code'. 

你至less有两个问题我可以看到。

第一:

 # nginx configuration location / { if (!-e $request_filename){ rewrite ^(.+)$ /index.php?url=$1 break; } } 

这是多余的,与nginx.conf定义的location冲突。 它应该被禁用或删除。

第二:

 location / { try_files $uri $uri/ /index.php?$query_string; } 

与你在.htaccess看起来不同, try_files是正确的方法。 但是,你没有提供你想要的格式的URL,你只需要解决这个问题。

 location / { try_files $uri $uri/ /index.php?url=$uri; } 

当然,你应该做大多数人做的事情, 不要在nginx中做任何特殊的预处理。 而是从$_SERVER['REQUEST_URI']的环境获取请求URI。 这当然是最好的做法。

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

解决scheme是:

重温:

 location / { try_files $uri $uri/ /index.php?$query_string; } 

通过:

 location / { try_files $uri $uri/ /index.php?$query_string; rewrite ^/(.*)$ /index.php?url=$1 last;} 

并做了一个:

sudo /etc/init.d/nginx重新启动

参考: https : //www.codecourse.com/forum/topics/nginx-config-url-rewrite-on-laravel-homestead/4696