nginx跳转和chrome 301跳转缓存问题

使用CodeIgniter建站,站点的使用方式是:手机扫描二维码访问某网址,获取设备信息。

因前期投放二维码时未考虑充分,构造了http://xxx.com/440390000/123456789这样的形式(其中123456789随着设备变化),而CodeIgniter的控制器是不能使用纯数字命名的。

最终设计了一个product的控制器,用http://xxx.com/product/123456789与http://xxx.com/440390000/123456789一一对应,当用户访问http://xxx.com/440390000/123456789使用nginx301跳转来访问http://xxx.com/product/123456789。

(#http://codeigniter.org.cn/user_guide/general/controllers.html    重要:类名必须以大写字母开头)

nginx配置文件:

rewrite ^/\d*/(.*)$ http://xxx.com/product/$1 permanent;

因多次配置nginx,完成配置后,发现使用chrome浏览器跳转失败,http://xxxx.com/product/product/product/product/product/product/product/product/product/product/product/product/product/product/product/product/product/product/product/440390000/123456789

但使用火狐浏览器访问跳转正常。打开chrome调试,preserve log功能,访问http://xxx.com/440390000/123456789,出现多个跳转

    1. Request URL:
      http://gegushi.com/440390000/123456789
    2. Request Method:
      GET
    3. Status Code:
      301 Moved Permanently (from disk cache)
    4. Remote Address:
      127.0.0.1:80
    5. Referrer Policy:
      no-referrer-when-downgrade
  1. Response Headersview source
    1. Content-Length:
      162
    2. Content-Type:
      text/html
    3. Date:
      Fri, 03 Jan 2020 07:15:47 GMT
    4. Location:
      http://gegushi.com/product/440390000/123456789
    5. Server:
      nginx

    1. Request URL:
      http://gegushi.com/product/440390000/123456789
    2. Request Method:
      GET
    3. Status Code:
      301 Moved Permanently (from disk cache)
    4. Remote Address:
      127.0.0.1:80
    5. Referrer Policy:
      no-referrer-when-downgrade
  1. Response Headersview source
    1. Content-Length:
      162
    2. Content-Type:
      text/html
    3. Date:
      Fri, 03 Jan 2020 07:15:47 GMT
    4. Location:
      http://gegushi.com/product/product/440390000/123456789
    5. Server:
      nginx

使用curl -I 测试,证明跳转是正常的,可以确定是chrome浏览器问题,使用chrome浏览器隐身模式(相当于清除缓存)访问发现跳转正常了。

$ > curl -I http://gegushi.com/440390000/123456789
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 08 Jan 2020 09:06:08 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: http://gegushi.com/product/123456789

谷歌:nginx chrome 301 location

有相关网页提到chrome浏览器会保留301跳转缓存记录。问题解决。

# https://superuser.com/questions/304589/how-can-i-make-chrome-stop-caching-redirects/485311

ASK:
I am working on a web application that is using redirects (for pretty URLs).

I am trying to debug logic in the way the redirects are working. However Google Chrome keeps remembering the redirects and even after I change the code, redirecting the same way.

This is making it very hard to troubleshoot.

For example if I redirect /this to /that, then change my code to NOT redirect /this anymore. Google Chrome is still redirecting to /that. Like the redirect is cached or something. Is there a way to turn this off?

Answer:

Google Chrome will cache your 301 redirects. To get around this, and to keep the tabs open, you’ll just need to clear your browser cache.

Chrome Menu Chrome Menu > Settings > Show advanced settings… > Privacy > Click Clear browsing data…

Whatever else you select, make sure “Cached images and files” is a checked option.

Then click Clear browsing data and you should be able to retest again.

If you’ve just followed the redirect, you only need to delete data from the past hour.

Alternatively, test and develop in incognito mode. There the cache is flushed after the browser is closed.

Answer2:

Chrome caches HTTP redirects and stop checking with the site if the redirect has changed. This can be frustating, since the easiest way to fix (visiting the site and forcing a hard refresh) can’t be used because the redirect will happen before you access the link. This is a won’t fix issue.

To workaround this, you can clear your browsing data, as explained here or you can follow the steps below and avoid losing your history.

  1. Open the Chrome Developer Tools dev-tools
  2. Click in Settings settings
  3. Check Disable cache (while DevTools is open) disable
  4. Visit the site that you wanted and the cache problem will be solved.