简体   繁体   English

Nginx在启用PHP的位置内重写规则

[英]Nginx rewrite rule inside location with php enabled

I need to have a php application inside my default server configuration. 我需要在默认服务器配置中包含一个php应用程序。

Also this application it was previously allocated on a apache server with rewrite rules on the .htaccess file that I translated to nginx. 同样,此应用程序以前是在我翻译为nginx的.htaccess文件上具有重写规则的情况下分配给apache服务器的。

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. 如果我访问localhost/admin/login则会收到403禁止错误。

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. 如果我将controller.php添加到索引文件中,它会正确响应php响应,因此我认为php配置正确。

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. 因此,很容易使其在路径上工作,但应用程序不适用于相对路径,因此在login内进行调用后,将在根路径内搜索auth方法。

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. 无论如何,我使登录页面正常工作,只是删除了重写规则并在索引参数上添加了controller.php 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;
    }
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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