Skip to content

Support custom return in the default server #1297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2021

Conversation

030
Copy link
Contributor

@030 030 commented Jan 4, 2021

This allows to redirect to another URL instead of returning a 404

Proposed changes

Describe the use case and detail of the change. If this PR addresses an issue on GitHub, make sure to include a link to that issue here in this description (not in the title of the PR).

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto master
  • I will ensure my PR is targeting the master branch and pulling from my branch from my own fork

@netlify
Copy link

netlify bot commented Jan 4, 2021

Deploy request for nginx-kubernetes-ingress rejected.

Rejected with commit 27e662653eef3a3ce4cc88d355cd0f8235dd5cff

https://docs.netlify.com/configure-builds/environment-variables/#sensitive-variable-policy

@030 030 force-pushed the 1295-redirect-if-does-not-exist-4 branch 4 times, most recently from d81e91f to e5c7682 Compare January 4, 2021 21:06
@pleshakov
Copy link
Contributor

Hi @030

Thanks for the PR! We'll look into it and get back to you shortly.

@pleshakov
Copy link
Contributor

Hi @030

Most of the global configuration that affects the generated NGINX config we implement through the ConfigMap. This allows changing that configuration without re-creating the IC pods and also minimizes the number of cli args (and as a result helm chart parameters).

If this feature was implemented through the ConfigMap, I think the following ConfigMap key would be enough:

default-server-redirect-url: https://nginx.org

At the same time, another way to solve this problem could be through a more general mechanism -- server snippets. We support snippets for Ingress resources via annotations and the ConfigMap. However, we don't support them for the default server.

For example, here we configure a redirect:

default-server-server-snippets: location / { return 302 https://nginx.org; }

More use cases could be supported. Here we tell NGINX to close the connection:

default-server-server-snippets: location / { return 444; }

Here we tell NGINX to return a static response:

default-server-server-snippets: location / { return 200 "welcome from the Ingress Controller!\n"; }

The default value would be location / { return 404; }, for backward compatibilty.

I wonder what you think about the both ConfigMap approaches?

@030 030 force-pushed the 1295-redirect-if-does-not-exist-4 branch 2 times, most recently from 1cf4b4d to 182722c Compare January 12, 2021 20:33
@030
Copy link
Contributor Author

030 commented Jan 12, 2021

@pleshakov Thank you for the feedback. Changes have been applied and the redirect works in conjunction with a configMap as well. Could you review again?

Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @030

Thanks for addressing the feedback.

Because in the implementation default-server-redirect-url configures the return directive, could you rename the key to default-server-return ? Note that the supported use cases of the return directive are broader than redirecting. That's why default-server-return suites better. Could you also make the corresponding changes to the identifiers in the code?

In my original suggestion, I assumed that default-server-redirect-url would only configures the redirect. For example, return 302 {{.DefaultServerRedirectURL}};. However, current the implementation with return {{.DefaultServerRedirectURL}; makes sense and covers more use cases. So I think it is better to keep it, although with the mentioned renaming.

There are also a few additional comments below.

By default a 404 is returned if none of the hosts match.
One could define default-server-redirect-url in a configMap
to redirect to another URL
@030 030 force-pushed the 1295-redirect-if-does-not-exist-4 branch from 60bee26 to 332dee1 Compare January 16, 2021 15:48
@030
Copy link
Contributor Author

030 commented Jan 16, 2021

@pleshakov Variable has been renamed and the documentation has been moved to the general customization paragraph

Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @030

This looks good to me 👍 (with this comment applied https://github.com/nginxinc/kubernetes-ingress/pull/1297/files#r560548874 )

If you could also update the commit message, as it still refers to the redirect feature. Also, for consistency, could you remove the [#1295] from the beginning of a commit message? we typically don't put the issue number in the title -- https://github.com/nginxinc/kubernetes-ingress/commits/master

I will ask for one more approval from our project maintainers.

Copy link
Contributor

@Dean-Coakley Dean-Coakley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for your continued efforts on this @030 !

LGTM.

This looks ready to be merged once the feedback from @jputrino is resolved.

Co-authored-by: Jodie Putrino <j.putrino@f5.com>
@Dean-Coakley Dean-Coakley merged commit 65d74b1 into nginx:master Jan 22, 2021
@pleshakov pleshakov added the enhancement Pull requests for new features/feature enhancements label Mar 16, 2021
@pleshakov pleshakov changed the title [#1295] Add feature redirect if host does not exist Support custom return in the default server Mar 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Pull requests for new features/feature enhancements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants