简体   繁体   English

Nginx将所有请求从子目录发送到php-fpm?

[英]Nginx send all requests from subdirectory to php-fpm?

how can i redirect all requests going to /api/v1/*ANYTHING* to /api/v1/router.php ? 我如何将所有转到/api/v1/*ANYTHING*请求重定向到/api/v1/router.php my current nginx config is: 我当前的nginx配置是:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /srv/http/default/www;
    autoindex on;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

and things i have tried: 和我尝试过的事情:

location /api/v1 {
    try_files FIXME_DO_NOT_MAKE_FILE_WITH_THIS_NAME /api/v1/router.php$is_args$args;
}
  • this almost works, for example /api/v1/get/123 works.. but it does not work if the url ends with .php, for example /api/v1/get/123.php will not be redirected, resulting in a 404 Not Found if get/123.php does not exist. 这几乎可以正常工作,例如/api/v1/get/123可以工作..但是,如果网址以.php结尾,则无法正常工作,例如/api/v1/get/123.php不会被重定向,从而导致如果get / 123.php不存在,则找不到404。 (and yeah, the dummy is apparently mandatory, with 1 argument i get an error that try_files requires a minimum of 2 arguments, and with try_files /api/v1/router.php =500; , it just prints the php source code rather than sending it to php-fpm) (是的,假人显然是强制性的,有1个参数我得到一个错误,即try_files至少需要2个参数,而try_files /api/v1/router.php =500;它只是打印php源代码而不是发送到php-fpm)

have also tried: 也尝试过:

location /api/v1 {
    rewrite ^/api/v1/(.*)$ /api/v1/router.php last;
}

.. which seems to suffer from exactly the same issue as above, as long as the url does not end in .php it works, otherwise i get 404's... i guess it has something to do with the location ~ \\.php$ block taking precedence, but it doesn't matter if i put my rewrite rule above or below the .php$ block... so now i'm kindof stumped ..这似乎遭受与上述完全相同的问题,只要url不以.php就可以了,否则我得到404的...我想这与location ~ \\.php$块优先,但是我将重写规则放在.php $块之上还是之下都没有关系...所以现在我有点困惑

(also not sure if this question belongs on SO or serverfault or superuser, but given that similar questions like Nginx redirect all requests from subdirectory to another subdirectory root exists on SO, i decided on trying here) (也不确定这个问题是否属于SO或serverfault或超级用户,但是鉴于类似的问题,例如Nginx将所有请求从子目录重定向到 SO上存在的另一个子目录根 ,我决定尝试在这里进行)

The most straightforward solution would be to define a "regex" location, and directly specify script name that will be the one processing all requests, like so: 最直接的解决方案是定义一个“ regex”位置,并直接指定将用于处理所有请求的脚本名称,如下所示:

location ~ ^/api/v1/ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root/api/v1/router.php;
}

Make sure to put this above the location ~ \\.php$ { because NGINX checks regex locations in order of appearance in configuration files. 确保将其放在location ~ \\.php$ { 上方 ,因为NGINX会根据配置文件中出现的顺序检查正则表达式的位置。

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

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