【问题标题】:Nginx doesn't execute php in specific directoryNginx 不在特定目录中执行 php
【发布时间】:2016-05-24 21:26:21
【问题描述】:

这是我的 nginx 配置文件

server {
    listen      90;
    server_name localhost;
    root        /var/www/wypok/;
    index       index.php index.html index.htm;

    location / {
        if ($request_uri = /) {
            return 301 /mirkoplusy;
        }
        try_files $uri $uri/ @rewriteToMirkoplusy;
    }

    location @rewriteToMirkoplusy {
        return 301 /mirkoplusy;
    }

    location ~* /mirkoplusy {
        rewrite (?i)^/mirkoplusy/wpis/(.*)/(css|js|fonts)/(.+)/?$ /mirkoplusy/$2/$3;
        rewrite (?i)^/mirkoplusy/wpis/(.*)/?$ /mirkoplusy/;
    }

    location ~* /api {
        rewrite (?i)^/api/v(\d+)/(.+)/?$ /mirkoplusy/api/v$1/main.php?_url=/$2;
    }

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

问题是api目录下没有执行php文件。当我连接到http://localhost:90/api/v2/init 时,我得到了重写规则调用的 main.php 文件的源代码

位置 ~* /api {

   rewrite (?i)^/api/v(\d+)/(.+)/?$ /mirkoplusy/api/v$1/main.php?_url=/$2;

}

我在主根目录下创建了 test.php 文件,它可以工作,所以该重写规则肯定有问题。通常我希望像 /api/v2/entry/23224 这样的每个 api 调用都被传递到 /api/v2/main.php 文件。

【问题讨论】:

    标签: nginx nginx-location


    【解决方案1】:

    http://nginx.org/en/docs/http/ngx_http_core_module.html#location
    ...
    然后检查正则表达式,按照它们在配置文件中出现的顺序。
    ...


    我认为重写的 url 被 location ~* /mirkoplusy 捕获并且未被 location ~ \.php$ 处理。
    尝试通过前缀定义位置mirkoplusyapi

    location /mirkoplusy {
        rewrite (?i)^/mirkoplusy/wpis/(.*)/(css|js|fonts)/(.+)/?$ /mirkoplusy/$2/$3;
        rewrite (?i)^/mirkoplusy/wpis/(.*)/?$ /mirkoplusy/;
    }
    
    location /api {
        rewrite (?i)^/api/v(\d+)/(.+)/?$ /mirkoplusy/api/v$1/main.php?_url=/$2;
    }
    

    或将位置php 添加到mirkoplusy

    location ~* /mirkoplusy {
        rewrite (?i)^/mirkoplusy/wpis/(.*)/(css|js|fonts)/(.+)/?$ /mirkoplusy/$2/$3;
        rewrite (?i)^/mirkoplusy/wpis/(.*)/?$ /mirkoplusy/;
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    【讨论】:

    • 我从两个规则中删除了 ~* 字符,一切正常,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多