简体   繁体   中英

Nginx rewrite rule inside location with php enabled

I need to have a php application inside my default server configuration.

Also this application it was previously allocated on a apache server with rewrite rules on the .htaccess file that I translated to nginx.

I'm trying to configure it all like that:

server {
        listen [::]:443;
        server_name _;

        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;

        location /admin {
                alias /var/www/sefoanco/html;
                index index.php index.html index.htm;

                try_files $uri $uri/ @admin;

                rewrite ^(/admin/.*)/login/ /login/controller.php break;
                rewrite ^(/admin/.*)/observacions/ /observacions/controller.php break;
                rewrite ^(/admin/.*)/usuari/ /usuari/controller.php break;
                rewrite ^(/admin/.*)/llistat/ /llistat/controller.php break;

                location ~ /admin/.+\.php$ {
                        include snippets/fastcgi-php.conf;

                        fastcgi_param SCRIPT_FILENAME $request_filename;    

                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                        include fastcgi_params;
                }

            }

    location @admin {
        rewrite /admin/(.*)$ /admin/$1 last;
    }

}

If I access to localhost/admin/login I get a 403 forbidden error.

If I add the controller.php to the index files, it answer correctly with the php response, so I think that the php is well configured.

So I think that I forget something to config.

Finally the problem was that the application are not made to work inside a path. If I see the application configuration I saw the following:

$_DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];

So, is easy to make it working on paths but the application doesn't work with relative paths so after make a call inside login , will search the auth method inside the root path.

So is needed to rewrite some parts of the application before it can work inside a path.

Anyway, I get the login page working just deleting rewrite rules and adding controller.php on the index param. Something like should work:

server {
        listen [::]:443 ;
        server_name _;

        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html controller.php;

        location /admin {
         try_files $uri $uri/ =404; 
    }

    location ~ \.php($|/) {
        include snippets/fastcgi-php.conf;

        fastcgi_param SCRIPT_FILENAME $request_filename;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        include fastcgi_params;
    }
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM