根据Nginx中的子目录将所有传入请求路由到index.php

时间:2019-10-08 20:40:17

标签: .htaccess nginx

我正在将FastRoute与PHP一起用于REST API。当我使用Apache运行服务器时,现在我已经使用Nginx切换到硬件。但是,尝试将.htaccess转换为Nginx位置块时遇到麻烦。 这是我的.htaccess的样子:

RewriteEngine On
RewriteBase /1_1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [QSA,L]

我基本上是在尝试将通过_1_1 /子目录传入的所有内容传递到/1_1/index.php文件,以便FastRoute可以使用该请求。将来我可能会使用更多子目录,因此/ 1_2 / *需要通过/1_2/index.php等传递。

这是我以前从未使用过定位块的人所尝试过的方法:

server {
    root /usr/share/nginx/sites/[...];
    index index.php index.html;

    server_name rest.[...];

    location /1_1/ {
            fastcgi_pass unix:/var/run/php/php7.3-fpm-w_rest.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            rewrite ^/1_1/(.*)$ /1_1/index.php break;
            include fastcgi_params;
    }

    location / {
            try_files $uri $uri/;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.3-fpm-w_rest.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/rest.[...]/fullchain.pem; $
    ssl_certificate_key /etc/letsencrypt/live/rest.[...]/privkey.pem$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = rest.[...]) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    listen 80;

    server_name rest.[...];
    return 404; # managed by Certbot
}

但是,这似乎不起作用。当我简单地访问/ 1_1 /(作为目录)时,它确实起作用,PHP开始运行。当我访问/1_1/index.php时也是如此。但是,一旦我尝试请求FastRoute路由(例如/ 1_1 / route),它只会下载PHP脚本,因此根本不会运行PHP。

这里有帮助吗?

0 个答案:

没有答案