Nginx 能否强制匹配多个 location?

2018-11-15 17:23:33 +08:00
 1119186082

比如

    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 了,只能重新写一个正则匹配,非常不优美。请问有好的解决方案么?

3614 次点击
所在节点    问与答
7 条回复
AngryPanda
2018-11-15 17:51:18 +08:00
location ~ /api

另外你的 add_header 行写错了。
1119186082
2018-11-15 18:07:40 +08:00
@AngryPanda 谢谢!不过这样写,add_header 有了,但 php 的设置又没有被执行,访问.php 会直接下载源代码#捂脸
wly19960911
2018-11-15 18:22:12 +08:00
location 可以嵌套 /t/490450 至于能不能起效果你自己试试
AngryPanda
2018-11-15 18:46:39 +08:00
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
2018-11-15 18:53:59 +08:00
@wly19960911 谢谢!我试试
1119186082
2018-11-15 18:54:10 +08:00
@AngryPanda 谢谢啦!
msg7086
2018-11-16 02:52:41 +08:00
这种情况下你可以选择用 include。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/508222

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX