<div dir="ltr"><div dir="ltr"><div><div><div>If you don't like 301 redirects because permanent caching — can't blame you there, as I'm in same boat — an easier way would be to simply use what I call the exception handling mechanism of nginx to change all 301 replies to 302, and you don't need any patches to perform such a change, as a simple nginx.conf snippet would suffice:<br><br></div> error_page 301 =302 @200;<br> location @200 {</div><div> # <a href="https://serverfault.com/a/870911/110020">https://serverfault.com/a/870911/110020</a></div><div> default_type "";<br></div><div> return 200;<br> }<br><br></div></div><div>If you don't like the `200` part in the config, and lack of a body in the response, then the following is also an option:</div><div><br></div><div> error_page 301 = @302;<br> location @302 {</div><div> # <a href="https://serverfault.com/a/870722/110020">https://serverfault.com/a/870722/110020</a><br> return 302 $sent_http_location;<br> }</div><div><br></div><div>I've tested both of the above for directory access sans `/`, and both do the job just fine.<br></div><div><br></div><div>Cheers,</div><div>Constantine.SU. <a href="http://cm.su/">http://cm.su/</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, 31 Aug 2019 at 17:46, Blake Williams <<a href="mailto:nginx@qzxj.net">nginx@qzxj.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello!<br>
<br>
We ran into an issue where with the permanent redirects in ngx_http_static_module.c that occur when you omit a slash when requesting a folder, for example from "/foo" to the folder "/foo/". We changed some things around in our site so that "/foo" was actually a file, not a folder, but unfortunately, browsers aggressively cache 301 redirects so our clients were trying to hit the new URL, the browser used its permanent cache, and they'd be redirected to "/foo/" again, which no longer existed as it had been changed to a file.<br>
<br>
This patch adds an extra configuration directive that allows you to configure that redirect to issue a 302 instead:<br>
<br>
# HG changeset patch<br>
# User Blake Williams <<a href="mailto:code@shabbyrobe.org" target="_blank">code@shabbyrobe.org</a>><br>
# Date 1567294381 -36000<br>
# Sun Sep 01 09:33:01 2019 +1000<br>
# Node ID 85c36c3f5c349a83b1b397a8aad2d11bf6a0875a<br>
# Parent 9f1f9d6e056a4f85907957ef263f78a426ae4f9c<br>
Add slash_redirect_temporary directive to core<br>
<br>
diff -r 9f1f9d6e056a -r 85c36c3f5c34 contrib/vim/syntax/nginx.vim<br>
--- a/contrib/vim/syntax/nginx.vim Mon Aug 19 15:16:06 2019 +0300<br>
+++ b/contrib/vim/syntax/nginx.vim Sun Sep 01 09:33:01 2019 +1000<br>
@@ -571,6 +571,7 @@<br>
syn keyword ngxDirective contained session_log_format<br>
syn keyword ngxDirective contained session_log_zone<br>
syn keyword ngxDirective contained set_real_ip_from<br>
+syn keyword ngxDirective contained slash_redirect_temporary<br>
syn keyword ngxDirective contained slice<br>
syn keyword ngxDirective contained smtp_auth<br>
syn keyword ngxDirective contained smtp_capabilities<br>
diff -r 9f1f9d6e056a -r 85c36c3f5c34 src/http/modules/ngx_http_static_module.c<br>
--- a/src/http/modules/ngx_http_static_module.c Mon Aug 19 15:16:06 2019 +0300<br>
+++ b/src/http/modules/ngx_http_static_module.c Sun Sep 01 09:33:01 2019 +1000<br>
@@ -188,7 +188,11 @@<br>
r->headers_out.location->value.len = len;<br>
r->headers_out.location->value.data = location;<br>
<br>
- return NGX_HTTP_MOVED_PERMANENTLY;<br>
+ if (!clcf->slash_redirect_temporary) {<br>
+ return NGX_HTTP_MOVED_PERMANENTLY;<br>
+ } else {<br>
+ return NGX_HTTP_MOVED_TEMPORARILY;<br>
+ }<br>
}<br>
<br>
#if !(NGX_WIN32) /* the not regular files are probably Unix specific */<br>
diff -r 9f1f9d6e056a -r 85c36c3f5c34 src/http/ngx_http_core_module.c<br>
--- a/src/http/ngx_http_core_module.c Mon Aug 19 15:16:06 2019 +0300<br>
+++ b/src/http/ngx_http_core_module.c Sun Sep 01 09:33:01 2019 +1000<br>
@@ -520,6 +520,13 @@<br>
offsetof(ngx_http_core_loc_conf_t, satisfy),<br>
&ngx_http_core_satisfy },<br>
<br>
+ { ngx_string("slash_redirect_temporary"),<br>
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,<br>
+ ngx_conf_set_flag_slot,<br>
+ NGX_HTTP_LOC_CONF_OFFSET,<br>
+ offsetof(ngx_http_core_loc_conf_t, slash_redirect_temporary),<br>
+ NULL },<br>
+<br>
{ ngx_string("internal"),<br>
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,<br>
ngx_http_core_internal,<br>
@@ -3443,6 +3450,8 @@<br>
clcf->open_file_cache_errors = NGX_CONF_UNSET;<br>
clcf->open_file_cache_events = NGX_CONF_UNSET;<br>
<br>
+ clcf->slash_redirect_temporary = NGX_CONF_UNSET;<br>
+<br>
#if (NGX_HTTP_GZIP)<br>
clcf->gzip_vary = NGX_CONF_UNSET;<br>
clcf->gzip_http_version = NGX_CONF_UNSET_UINT;<br>
@@ -3727,6 +3736,9 @@<br>
<br>
ngx_conf_merge_sec_value(conf->open_file_cache_events,<br>
prev->open_file_cache_events, 0);<br>
+<br>
+ ngx_conf_merge_value(conf->slash_redirect_temporary,<br>
+ prev->slash_redirect_temporary, 0);<br>
#if (NGX_HTTP_GZIP)<br>
<br>
ngx_conf_merge_value(conf->gzip_vary, prev->gzip_vary, 0);<br>
diff -r 9f1f9d6e056a -r 85c36c3f5c34 src/http/ngx_http_core_module.h<br>
--- a/src/http/ngx_http_core_module.h Mon Aug 19 15:16:06 2019 +0300<br>
+++ b/src/http/ngx_http_core_module.h Sun Sep 01 09:33:01 2019 +1000<br>
@@ -433,6 +433,8 @@<br>
ngx_uint_t types_hash_max_size;<br>
ngx_uint_t types_hash_bucket_size;<br>
<br>
+ ngx_flag_t slash_redirect_temporary;<br>
+<br>
ngx_queue_t *locations;<br>
<br>
#if 0<br>
_______________________________________________<br>
nginx mailing list<br>
<a href="mailto:nginx@nginx.org" target="_blank">nginx@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx" rel="noreferrer" target="_blank">http://mailman.nginx.org/mailman/listinfo/nginx</a><br>
</blockquote></div><br></div>