2

这是我的 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 文件。

4

1 回答 1

1

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


我认为重写的 urllocation ~* /mirkoplusylocation ~ \.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;
}

或将位置添加phpmirkoplusy

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;
    }
}
于 2016-05-25T05:44:08.867 回答