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

Nginx 能否强制匹配多个 location?

  •  
  •   1119186082 · 2018-11-15 17:23:33 +08:00 · 3603 次点击
    这是一个创建于 1980 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如

        location /api {
          add_header "Access-Control-Allow-Origin: *";
        }
        location ~ [^/]\.php(/|$) {
          fastcgi_pass unix:/dev/shm/php-cgi.sock;
          fastcgi_index index.php;
          include fastcgi.conf;
        }
    

    这时,如果访问 api 下的.php ,就没有 add_header 了,只能重新写一个正则匹配,非常不优美。请问有好的解决方案么?

    7 条回复    2018-11-16 02:52:41 +08:00
    AngryPanda
        1
    AngryPanda  
       2018-11-15 17:51:18 +08:00   ❤️ 1
    location ~ /api

    另外你的 add_header 行写错了。
    1119186082
        2
    1119186082  
    OP
       2018-11-15 18:07:40 +08:00
    @AngryPanda 谢谢!不过这样写,add_header 有了,但 php 的设置又没有被执行,访问.php 会直接下载源代码#捂脸
    wly19960911
        3
    wly19960911  
       2018-11-15 18:22:12 +08:00 via Android   ❤️ 1
    location 可以嵌套 /t/490450 至于能不能起效果你自己试试
    AngryPanda
        4
    AngryPanda  
       2018-11-15 18:46:39 +08:00   ❤️ 1
    location ~ [^/]\.php(/|$) {
    if ($request_uri ~ ^/api/) {
    add_header Access-Control-Allow-Origin *;
    }
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
    1119186082
        5
    1119186082  
    OP
       2018-11-15 18:53:59 +08:00
    @wly19960911 谢谢!我试试
    1119186082
        6
    1119186082  
    OP
       2018-11-15 18:54:10 +08:00
    @AngryPanda 谢谢啦!
    msg7086
        7
    msg7086  
       2018-11-16 02:52:41 +08:00
    这种情况下你可以选择用 include。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3670 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 04:55 · PVG 12:55 · LAX 21:55 · JFK 00:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.