My .htaccess is:
# nginx configuration
location / {
if (!-e $request_filename){
rewrite ^(.+)$ /index.php?url=$1 break;
}
}
I have nginx configured like this:
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;
}
If I call: http://prueba_nginx.net/a/b/c
and I run the php commant: echo $_GET['url´] it must return: : a/b/c
but currently it is returning: empty.
PHP:
echo "\n".'$_GET'."\n"; var_dump($_GET);
If I made the test with apache it works perfect:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
what is the problem? Are there into the transformation from apache to nginx an error?
Thanks Michael Hamptom for your fast answer.
I checked $_SERVER['REQUEST_URI'] and it is returning the right value: a/b/c
but I am wondering why is not working with $_Get.
I made the changes that you sugested:
.htaccess:
# nginx configuration
#location / {
#if (!-e $request_filename){
#rewrite ^(.+)$ /index.php?url=$1 break;
#}
#}
and I changed nginx file from:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
to
location / {
try_files $uri $uri/ /index.php?url=$uri;
}
But when I restarted nginx I received this error message:
[….] Restarting nginx (via systemctl): nginx.serviceJob for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
failed!
systemctl status 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'.
Best Answer
You've got at least two problems I can see.
First:
This is redundant and conflicts with the earlier defined
location
innginx.conf
. It should be disabled or removed.Second:
Unlike what you appear to have in
.htaccess
,try_files
is the correct way to do this. But, you aren't providing the URL in the format that you want, and you only need to fix that.Of course, you should instead do what most people do, and don't do any special preprocessing in nginx. Instead, you get the request URI from the environment in
$_SERVER['REQUEST_URI']
. Which is of course best practice.