V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
JohnChiu
V2EX  ›  问与答

这个 Nginx 伪静态规则该怎么写: index. PHP ->api

  •  
  •   JohnChiu · 2018-05-19 15:16:30 +08:00 · 1202 次点击
    这是一个创建于 2169 天前的主题,其中的信息可能已经有所发展或是发生改变。

    动态链接是:www.domain.com/index.php/admin/login

    伪静态是:www.domain.com/api/admin/login

    没有接触过 Nginx,请问这个规则该怎么写呢

    标题自动大写 php,改不过来。。。

    8 条回复    2018-05-19 16:23:39 +08:00
    JohnChiu
        1
    JohnChiu  
    OP
       2018-05-19 15:58:47 +08:00
    没有大佬帮一下吗~~
    chinvo
        2
    chinvo  
       2018-05-19 16:05:57 +08:00   ❤️ 1
    简单点,你给放到 /api 里面,然后

    try_files $uri $uri/ /index.php/$uri?$args;
    Lax
        3
    Lax  
       2018-05-19 16:06:43 +08:00   ❤️ 1
    类似这样,不保证能用:
    rewrite /api/admin/login /index.php/admin/login;

    如果需要匹配
    rewrite ^/api/(.*) /index.php/$1;
    JohnChiu
        4
    JohnChiu  
    OP
       2018-05-19 16:12:18 +08:00
    @chinvo #2 把 index 放到 /api 文件夹然后这样吗?没有成功,另外 index.php 后面还有其他的比如 /admin/logout
    if (!-e $request_filename) {
    try_files $uri $uri/ /index.php/$uri?$args;
    }
    chinvo
        5
    chinvo  
       2018-05-19 16:13:29 +08:00   ❤️ 1
    @JohnChiu #4 要写在 location /api 里面
    JohnChiu
        6
    JohnChiu  
    OP
       2018-05-19 16:16:27 +08:00
    @Lax #3 感谢,不过还是没有成功,依然是要用 index.php 才能访问,api 不能访问
    请问我写的有问题吗
    if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php$1 last;
    }
    JohnChiu
        7
    JohnChiu  
    OP
       2018-05-19 16:19:04 +08:00
    @Lax #3 发错代码了
    if (!-e $request_filename) {
    rewrite ^/api/(.*) /index.php/$1;
    }
    JohnChiu
        8
    JohnChiu  
    OP
       2018-05-19 16:23:39 +08:00
    @chinvo #5 感谢,按你说的搞好了
    @Lax #3 感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1034 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 19:37 · PVG 03:37 · LAX 12:37 · JFK 15:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.