Nginx不会在特定目录中执行php

时间:2016-05-24 21:26:21

标签: nginx nginx-location

这是我的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;
    }
}

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

  

location~ * / 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文件。

1 个答案:

答案 0 :(得分:1)

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

我认为重写的网址由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;
    }
}