我的.htaccess 是:
# nginx configuration
location / {
if (!-e $request_filename){
rewrite ^(.+)$ /index.php?url=$1 break;
}
}
我的 nginx 配置如下:
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 命令:echo $_GET['url´] it必须返回: : a/b/c 但目前它返回的是:空的。
PHP的:
echo "\n".'$_GET'."\n"; var_dump($_GET);
如果我使用 apache 进行测试,它运行完美:
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.service 的 nginx.serviceJob 失败,因为控制进程退出并显示错误代码。有关详细信息,请参阅“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'.
答案1
我认为你至少有两个问题。
第一的:
# nginx configuration
location / {
if (!-e $request_filename){
rewrite ^(.+)$ /index.php?url=$1 break;
}
}
location
这是多余的,并且与中先前定义的相冲突nginx.conf
。应将其禁用或删除。
第二:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
与 中显示的内容不同.htaccess
,try_files
这是执行此操作的正确方法。但是,您没有以所需格式提供 URL,您只需修复此问题。
location / {
try_files $uri $uri/ /index.php?url=$uri;
}
当然,你应该像大多数人一样,不在 nginx 中不做任何特殊的预处理。相反,您可以从 中的环境中获取请求 URI $_SERVER['REQUEST_URI']
。这当然是最佳实践。
location / {
try_files $uri $uri/ /index.php;
}
答案2
解决方案是:
重新放置:
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