I'm trying to create rewrite rules for nginx ingress for directing the following URLs to the same service:
example/ws?list=a,b,c
example/ws/?list=a,b,c
Pasting in the regexps bellow from my helm chart in the context with the other rules that need to work.
So basically my question is: How to I write a nginx compatible regexp that contains a questionmark?
I thought this would solve it:
- path: /api/(.*) <----- THIS IS FOR ANOTHER SERVICE
pathType: ImplementationSpecific
component: "{{ .Values.backendponent }}"
port: "{{ .Values.backend.service.port }}"
- path: /ws/(.*) <------- WORKS
pathType: ImplementationSpecific
component: "{{ .Values.broadcasterponent }}"
port: "{{ .Values.broadcaster.service.port }}"
- path: /ws(\?.*) <-------- NOT WORKING
pathType: ImplementationSpecific
component: "{{ .Values.broadcasterponent }}"
port: "{{ .Values.broadcaster.service.port }}"
- path: /(.*)
pathType: ImplementationSpecific
component: "{{ .Values.frontendponent }}"
port: "{{ .Values.frontend.service.port }}"
I'm trying to create rewrite rules for nginx ingress for directing the following URLs to the same service:
example.com/ws?list=a,b,c
example.com/ws/?list=a,b,c
Pasting in the regexps bellow from my helm chart in the context with the other rules that need to work.
So basically my question is: How to I write a nginx compatible regexp that contains a questionmark?
I thought this would solve it:
- path: /api/(.*) <----- THIS IS FOR ANOTHER SERVICE
pathType: ImplementationSpecific
component: "{{ .Values.backend.component }}"
port: "{{ .Values.backend.service.port }}"
- path: /ws/(.*) <------- WORKS
pathType: ImplementationSpecific
component: "{{ .Values.broadcaster.component }}"
port: "{{ .Values.broadcaster.service.port }}"
- path: /ws(\?.*) <-------- NOT WORKING
pathType: ImplementationSpecific
component: "{{ .Values.broadcaster.component }}"
port: "{{ .Values.broadcaster.service.port }}"
- path: /(.*)
pathType: ImplementationSpecific
component: "{{ .Values.frontend.component }}"
port: "{{ .Values.frontend.service.port }}"
Share
Improve this question
asked Feb 6 at 9:58
user1716970user1716970
7651 gold badge8 silver badges19 bronze badges
0
1 Answer
Reset to default 0The question mark in the URL is part of the query string and not the path but however you can use the * modifier in regex to make the ? optional. Since the query string (anything starting with?) is not a part of the normalised URI, you cannot match it in location and rewrite expressions. Refer to official document on Module ngx_http_core_module for more details.
You could test for the presence of a query string then use rewrite to remove them:
if ($args) {
rewrite ^/$ /? permanent;
}
And also If you have a number of URIs to redirect, you should consider using a map.To process many URIs, use a map directive, for example:
map $request_uri $redirect {
default 0;
/somepath/somearticle.html?p1=v1&p2=v2 /some-other-path-a;
/somepath/somearticle.html /some-other-path-b;
}
server {
...
if ($redirect) {
return 301 $redirect;
}
...
}
Note: You can also use regular expressions in the map, for example, if the URIs also contain optional unmatched parameters. See this official Nginx document for more info.