-
Notifications
You must be signed in to change notification settings - Fork 202
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
Fix nginx redirect for no trailing slash #2848
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm slightly confused as to why nginx
was created as a separate service and not merged into the existing proxy
service. Also is this NGINX conf https://github.com/WordPress/openverse/blob/23d3972aad42aeed1e72fb604fe7e2b243f2a4c6/docker/nginx/templates/web.conf.template redundant?
My main concern is that two NGINX services with very similar names can be confusing, so if this is the only approach, a docs page explaining the differences and the rationale for having 2 of these might be a good idea.
At first I was going to merge the two, but the existing But now that you've mentioned it, one thing we could do is map the HTTPS config in the docker-compose, because Nginx supports concatenating multiple configuration files as long as they're in In short: we could try to combine the two, but it could take a bit of effort to make the two configurations work together without making the production configuration unnecessarily more complicated. Alternatively, we could take a different approach to testing HTTPS locally altogether. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks for the explanation @sarayourfriend. LGTM! Btw, we have #949 to switch to Caddy so we can deal with the removal/replacement of proxy
there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried running the testing instructions locally, and everything worked as described.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Thanks for the great testing instructions -- worked seamlessly for me 👍
Fixes
Fixes #728 by @rbadillap
Description
There are three main parts to this PR: one direct change, one related change, and another change to support testing.
docker-compose.yml
add a newnginx
service that uses thenginx
image we use in production created usingapi/Dockerfile
. This is necessary for testing the changes in this PR and indeed, for easily verifying any changes toapi/nginx.conf.template
.location /static
to add a trailing slash fixes a small issue and potential (though unlikely) security/misconfiguration issue. Adding a trailing slash/static/
prevents the rule from matching/static-somethingelsewedidnotintend
. Likewise, removing the unused/media
location also prevents potentially obscure security issues. Best to just not allow anything we don't intend. I learned about this while researching this issue and reading about how thealias
androot
directives work. A side effect of this change is that testing now needs to occur against/static/admin
because/static
won't match any configured locations and 404 instead of redirecting.absolute_redirects
directly fixes the problem described in the issue. The behaviour in the issue was caused by nginx attempting to redirect/static
with a missing trailing slash to/static/
. However, becauseport_in_redirect
was not off, it would inject the:8080
port into the location on the redirect. Hence,api.openverse.engineering/static
redirecting toapi.openverse.engineering:8080/static/
. Turning offport_in_redirect
would fix the issue in production, but introduces a difficulty with testing locally, because it would then strip the port, causing local testing requests to0.0.0.0:50270/static
to redirect to0.0.0.0/static/
without the original port. I couldn't find a good approach for this that didn't require heaps of additional configuration. Turning off absolute redirects does the trick perfectly in a way that is testable locally and will work exactly the same in production. Relative redirects used to not be supported but are fine these days, which is why the default of the absolute redirect directive preserves previous behaviour to use absolute redirects.Testing Instructions
Checkout this branch and run
just api/up
. This will cause the nginx image to build and be accessible as a proxy to Django atlocalhost:50270
. Test getting static files by going to/admin
and logging in and poking around. Next, test the redirect:curl --location -I http://localhost:50270/static/admin
. Confirm the redirected location is relative (does not include the port) and matches/static/admin/
with a trailing slash./static/admin/
will still give you a 403 forbidden because directory navigation is not enabled in nginx (intended). You can confirm the original behaviour by commenting out the newabsolute_redirect
rule inapi/nginx.conf.template
and runningjust dc build nginx && just api/up
to get the new configuration. Run thecurl
command above again and you should see a redirect tohttp://0.0.0.0:8080/static/admin/
instead of just the path. You can also go ahead and try toport_in_redirect off
approach to see the issues with it stripping all ports, rather than just not replacing the port with Nginx's (i.e., it won't preserve the port used for local requests against the docker-compose stack).Any time you make configuration changes to nginx, you must run
just dc build nginx
to get the new configuration: it does not auto-refresh the configuration. Runjust api/up
and Docker compose will replace the running service with the new image and preserve everything else. This is generally a pretty fast process.Checklist
Update index.md
).main
) or a parent feature branch.Developer Certificate of Origin
Developer Certificate of Origin