Friendly URLs nginx virtual store

0

This is the url I have: link

But I wish it were like this: link does not work file not found

I'm trying on nginx with the following settings

server {
    listen 80;
    client_max_body_size 100M;
    server_name cursophp.test www.cursophp.test;
    root /home/rafaelejosi/www/projetos/cursophp.test/; 

    index index.html index.htm index.php;

    charset utf-8;

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

    location /lojavirtual {
        index index.php;
        try_files $uri $uri/ /lojavirtual/index.php?$query_string;
        if (!-e $request_filename){
        rewrite ^(.*)$ /index.php?pag=$1 last;
      }
    }


    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log on;
    error_log  /var/log/nginx/cursophp.test-error.log error ;
    rewrite_log on;

    error_page 404 /index.php;

    sendfile off;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.2-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;
    }

    location /phpmyadmin {
                  root /usr/share/;
                  index index.php index.html index.htm;
                  location ~ ^/phpmyadmin/(.+\.php)$ {
                          try_files $uri =404;
                          root /usr/share/;
                          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                          fastcgi_index index.php;
                          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                          include /etc/nginx/fastcgi_params;
                  }
                  location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                          root /usr/share/;
                  } 
           }
           location /phpMyAdmin {
                  rewrite ^/* /phpmyadmin last;
           }


    location ~ /\.ht{
        deny all;
    }
    location ~ /*.tpl {
      deny all;
    }
}

Is there anything wrong with the code snippet?

location /lojavirtual {
    index index.php;
    try_files $uri $uri/ /lojavirtual/index.php?$query_string;
    if (!-e $request_filename){
    rewrite ^(.*)$ /index.php?pag=$1 last;
  }
}

The weakness of my project is like this

Myphpfilessofarareinthecontrollerandmodeldirectoriesoutsidetheindexandapagesketchoferrorsthatareloose.Butfornowthosewithrelevantcodeareinthemodelfolder.InthemodelfolderIhaveRoutes.class.phpandTemplate.class.php.

belowisthecontentoftheroutes:

<?phpClassRotas{staticfunctionget_pagina(){if(isset($_GET['pag'])){$pagina='controller/'.$_GET['pag'].'.php';echo$pagina;if(file_exists($pagina)){include$pagina;}else{include'erro.php';}}}}

Templatefilecontents:

<?phpClassTemplateextendsSmarty{function__construct(){parent::__construct();$this->setTemplateDir('view/');$this->setCompileDir('view/compile/');$this->setCacheDir('view/cache/');}}

Contentsofmyindexfile

<?phprequire"./lib/autoload.php";

$smarty = new Template(); 
Rotas::get_pagina();

$smarty->assign('NOME','RAFAEL DIAS ZENDRON');

$smarty->display('index.tpl');

Ja the htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?pag=$1 [L,QSA]
</IfModule>
<Files *.tpl>
    Order deny,allow
    Deny from All
</Files>

As I do not have much knowledge in nginx I used a converter that converts apache to nginx

nginx error report cursophp.test-error.log

2018/05/31 23:11:41 [error] 20478#20478: *5 FastCGI sent in stderr: 
"Primary script unknown" while reading response header from upstream, 
client: 127.0.0.1, server: cursophp.test, request: "GET 
/lojavirtual/carrinho HTTP/1.1", upstream: 
"fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "www.cursophp.test"

cursophp.test.access.log

2018/05/31 23:11:41 [error] 20478#20478: *5 FastCGI sent in stderr: 
"Primary script unknown" while reading response header from upstream, 
client: 127.0.0.1, server: cursophp.test, request: "GET 
/lojavirtual/carrinho HTTP/1.1", upstream: 
"fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "www.cursophp.test"
    
asked by anonymous 29.05.2018 / 14:10

1 answer

0

If you understand, you have created a system of controllers that is solved (or should be solved) in Class Rotas , I assume that index.php is what will access all controllers, if this is correct remove this:

rewrite ^(.*)$ /index.php?pag=$1 last;

From:

location /lojavirtual {
    index index.php;
    try_files $uri $uri/ /lojavirtual/index.php?$query_string;
    if (!-e $request_filename){
    rewrite ^(.*)$ /index.php?pag=$1 last;
  }
}

Because it is possible in PHP itself with $_SERVER['REQUEST_URI'] to solve this, based on the framework I developed, I created this function: Utils.php (really not everything needs to be POO, use POO as needed, but that's my opinion of course), adapting to your script:

<?php
class Rotas
{
    private static $pathinfo;

    private static function current()
    {
        if (self::$pathinfo !== null) {
            return self::$pathinfo;
        }

        $requri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

        $sname = $_SERVER['SCRIPT_NAME'];

        if ($requri !== $sname && $sname !== '/index.php') {
            $pathinfo = rtrim(strtr(dirname($sname), '\', '/'), '/');
            $pathinfo = substr(urldecode($requri), strlen($pathinfo));
            $pathinfo = $pathinfo === false ? '/' : $pathinfo;
        } else {
            $pathinfo = urldecode($requri);
        }

        return $pathinfo;
    }

    public function get_pagina()
    {
        $pathinfo = self::current();

        if ($pathinfo === '/') {
             return null;
        } else {
             $controller = $pathinfo;
        }

        $pagina ='controller/' . $controller . '.php';

        var_dump($pagina);

        if (is_file($pagina)) {
            include_once $pagina;
        } else {
            include_once 'erro.php';
        }
    }
}

In ngnix just do this:

location /lojavirtual/ {
    autoindex on;
    index  index.html index.htm index.php;
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.2-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;
}

In this way, all treatment will go through index.php and will be solved by $_SERVER['REQUEST_URI']

    
31.05.2018 / 03:30