URL rewrite Novice to Ninja PHP book nginx

Hi

I am using nginx for the book PHP novice to ninja url rewrites.

The code route = ltrim(strtok(_Server[‘Request_Uri’], ‘?’), ‘/‘);

Works with Apache but not with nginx? Is there something different we do with ngknx?

Thx!
Karen

I use NGiNX all the time and have no problem with the pattern you’re using.
What does your NGiNX config look like?

server {
listen 80;
server_name localhost;

root /var/www/html/test/html/public/;
index index.php index.html index.htm;

location / {
#try_files $uri $uri/ =404;
try_files $uri $uri / /index.php?args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ .php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

try_files $uri =404

should be removed from the location ~ .php$ { block.

I did - but how do I change my root folder to serve as /var/www/html/test/public;

I thought that this was managed with the root directive?

… even when I change the root directly as above… I still need to enter localhost/test/public to reach — so the URL rewrites create an error like this http://localhost/joke/list where localhost/joke/list does not exist… only /var/www/html/test/public exists…

confused as to why the root directive is not working

thx!
Karen

Yes that should work. It seems the default NGiNX server is picking this up. Basically because localhost is a bit of an odd one out.

Could you maybe try and change server_name localhost to something like server_name karentutor1.local and then add the following to your /etc/hosts file:

127.0.0.1  karentutor1.local

then restart nginx (service nginx reload) and go to http://karentutor1.local/ ?

Hi I ran nginx -t and saw another *.conf file in the conf.d folder … it was that which was messing it up.

We are ok now with the root .

I will investigate why the URL rewrites are not working (they aren’t yet) by looking at the error logs and write back if still confusing.

Thx for all your help!

Sure thing, you know where to find us :slight_smile:

So, I have my server working for the URL rewirtes with a config file as follows

server {
listen 80;
server_name localhost;
root /var/www/php/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$query_string;
#try_files $uri $uri/ /index.php?args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ .php$ {
try_files $uri =404;
fastcgi_pass unix:var/run/php-fpm/php-fpm.sock;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

However, the URL rewrite rule seems to obliterate the $_GET array.

For example, if you download php noviced to ninja final script from git as follows:
/var/www/ and clone with git

git clone https://github.com/spbooks/phpmysql6.git
git checkout Final-Website
I renamed diretory to php as per the root directive above (easier)
Then go here:
http://localhost/joke/list?category=2

If you go to class Joke line (20)
19 public function list() {
20 var_dump($_GET);

and var_dump($_GET) it is an empty arry?

… why is my array empty? Why is any $_GET array empty?

thanks!
Karen

Could you remove the try_files $uri =404; line from the location ~.php$ { block? I think that may be throwing it off.

Thanks, I tried reinstalling nginx and upgrading to Centos 8

My config file is /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;
        root         /var/www/html;
        #root         /var/www/html/eloorn/html/public;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

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

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

/etc/nginx/default.d/php.conf is

# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html index.htm;

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
}

However, I still can’t get my $_GET query read by the function?

thanks!
Karen

To pass get variables as well you need to use:

try_files $uri $uri/ /index.php?$query_string;

… nailed it.

Thanks ever so much for your time

1 Like