You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/05/19 06:40:21 UTC
[GitHub] [apisix] endison1986 opened a new issue, #7082: help request: CORS does not work on OPTIONS
endison1986 opened a new issue, #7082:
URL: https://github.com/apache/apisix/issues/7082
### Description
When I configured the advanced matching condition `env=dev` on Route and added the CORS plugin. Then I send a request through axios with the request header `env:dev`, then the browser initiates a preflight request `OPTIONS`, and I get a 404 return.
当我在Route上配置了高级匹配条件`env=dev`,并且添加了CORS插件后。然后我通过axios发送请求,并携带请求头`env:dev`,这时浏览器发起了一个预检请求`OPTIONS`,而我得到了一个404的返回。

### Environment
- APISIX version (run `apisix version`):2.12.1
- Operating system (run `uname -a`):Linux apisix-server-cd8564555-2njxg 3.10.0-514.26.2.el7.x86_64 # 1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
- OpenResty / Nginx version (run `openresty -V` or `nginx -V`):openresty/1.19.9.1
- etcd version, if relevant (run `curl http://127.0.0.1:9090/v1/server_info`):
- APISIX Dashboard version, if relevant:
- Plugin runner version, for issues related to plugin runners:
- LuaRocks version, for installation issues (run `luarocks --version`):
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] endison1986 closed issue #7082: help request: CORS does not work on OPTIONS
Posted by "endison1986 (via GitHub)" <gi...@apache.org>.
endison1986 closed issue #7082: help request: CORS does not work on OPTIONS
URL: https://github.com/apache/apisix/issues/7082
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on issue #7082: help request: CORS does not work on OPTIONS
Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #7082:
URL: https://github.com/apache/apisix/issues/7082#issuecomment-1131387722
Let's add OPTIONS to the route's methods when adding the CORS plugin.
> OPTIONS shouldn't apply advanced match conditions
We can't do it by default. Because if CORS plugin is not set, there is no way to handle OPTIONS requests in APISIX. And it doesn't work well in the case that requests in different methods will be routed to different routes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] endison1986 commented on issue #7082: help request: CORS does not work on OPTIONS
Posted by GitBox <gi...@apache.org>.
endison1986 commented on issue #7082:
URL: https://github.com/apache/apisix/issues/7082#issuecomment-1131449164
`OPTIONS` is already in Route, but OPTIONS request without env responds with 404 due to adding advanced match condition `env=dev`.
I use HTTP request headers to distinguish different environments in the same context, but if OPTIONS cannot carry custom request headers, then I have to be careful with advanced match conditions?
Because this will cause APISIX to not be able to correctly distinguish the request target`OPTIONS`已经在Route中,但是由于添加了高级匹配条件`env=dev`导致没有env的OPTIONS请求响应了404.
我使用HTTP请求头在相同的上下文下区分不同的环境,但如果OPTIONS无法携带自定义请求头,那我不得不谨慎使用高级匹配条件?
因为这样将导致APISIX无法正确的分辨请求目标
------------------------------------------------------------------------------------------
I have an idea, I use APISIX+NACOS to discover services in my microservice environment, dev is actually the namespace in NACOS, the current namespace is fixed, if namesapce can pass the env in the HTTP request header as a parameter, Then I don't need to match different routes through env, so the number of routes is also reduced, and there is no need to configure a route for a namespace separately.
我有一个想法,我使用APISIX+NACOS在我的微服务环境中发现服务,dev实际上是NACOS中的namespace,目前的namespace是固定的,如果namesapce可以通过HTTP请求头中的env作为参数的话,那我就不需要通过env来匹配不同的路由了,这样路由的数量也变少了,不需要单独为一个namespace配置一个路由了
For example, configure like this:
比如象这样配置:
```json
{
"timeout": {
"connect": 30,
"send": 30,
"read": 30
},
"type": "roundrobin",
"scheme": "http",
"discovery_type": "nacos",
"discovery_args": {
"group_name": "DEFAULT_GROUP",
"namespace_id": "${headers.env}"
},
"pass_host": "pass",
"name": "user",
"service_name": "user",
"keepalive_pool": {
"idle_timeout": 60,
"requests": 1000,
"size": 320
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] mfractal commented on issue #7082: help request: CORS does not work on OPTIONS
Posted by "mfractal (via GitHub)" <gi...@apache.org>.
mfractal commented on issue #7082:
URL: https://github.com/apache/apisix/issues/7082#issuecomment-1561145265
Encountered the same issue myself.
Thought i'd dump the solution here in case anybody else needs it.
We have a few routes with the following to allow routing the the right graphql server according to a header called graphqlName.
```
match:
paths:
- /api/graphql/*
- /api/graphql
exprs:
- subject:
scope: Header
name: graphqlName
op: In
set: {{- trim (toYaml $.Values.routes.graphqlOperations) | nindent 12 }}
```
So when the client calls `/api/graphql` and sends an `OPTIONS` request it'll not get routed anywhere since match conditions are skipped for OPTIONS and there was no route to serve the request.
I ended up creating a new route just for OPTIONS requests
```
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: options-graphql
namespace: ${var.namespace_name}
spec:
http:
- backends:
- serviceName: webserver
servicePort: http
match:
methods:
- OPTIONS
paths:
- /api/graphql/*
- /api/graphql
name: options-graphql
plugin_config_name: routes-default
plugins:
- config:
regex_uri:
- ^/api/?(.*)
- /$1
enable: true
name: proxy-rewrite
```
and the plugin config
```
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
name: routes-default
namespace: ${var.namespace_name}
spec:
plugins:
- config:
allow_credential: true
allow_headers: keep-alive,authorization,user-agent,cache-control,content-type,content-transfer-encoding,accountId,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout,graphqlName
allow_methods: GET, POST, PUT, DELETE, PATCH, OPTIONS
allow_origins: ${var.cors_allow_origins}
expose_headers: grpc-status,grpc-message
enable: true
name: cors
- config:
conf:
- name: webserver.${var.namespace_name}
value: "9090"
enable: true
name: ext-plugin-post-req
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] endison1986 commented on issue #7082: help request: CORS does not work on OPTIONS
Posted by GitBox <gi...@apache.org>.
endison1986 commented on issue #7082:
URL: https://github.com/apache/apisix/issues/7082#issuecomment-1131303981
My understanding is that OPTIONS shouldn't apply advanced match conditions? But if the advanced matching conditions do not take effect, the same context cannot distinguish between routes.
我的理解是OPTIONS不应该应用高级匹配条件?但如果高级匹配条件不生效,相同的上下文就又无法区分路由了。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander commented on issue #7082: help request: CORS does not work on OPTIONS
Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #7082:
URL: https://github.com/apache/apisix/issues/7082#issuecomment-1132432813
> OPTIONS is already in Route, but OPTIONS request without env responds with 404 due to adding advanced match condition env=dev.
You can use `OR` syntax to include OPTIONS request without header: https://github.com/api7/lua-resty-expr#synopsis
> I have an idea, I use APISIX+NACOS to discover services in my microservice environment, dev is actually the namespace in NACOS, the current namespace is fixed, if namesapce can pass the env in the HTTP request header as a parameter, Then I don't need to match different routes through env, so the number of routes is also reduced, and there is no need to configure a route for a namespace separately.
It seems you can use the traffic-split plugin to select upstream dynamically instead of putting them to different routes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] endison1986 commented on issue #7082: help request: CORS does not work on OPTIONS
Posted by GitBox <gi...@apache.org>.
endison1986 commented on issue #7082:
URL: https://github.com/apache/apisix/issues/7082#issuecomment-1132531290
`OR` syntax may not meet my needs, because I have three environments, namely dev\test\prod, which are distinguished by the HTTP request header env
> `OR`可能无法满足我的需求,因为我有三个环境,分别是dev\test\prod,通过HTTP请求头env来区分
Thanks for your reply, I will try to implement dynamic selection of upstream using plugin `traffic-split`
> 谢谢你的答复,我将尝试使用插件`traffic-split`实现动态选择上游
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org