nginx redirect for urls without trailing slash doesn't work in docker when not using default port 80

Viewed 688

Here is a website based on gatsby.

When running with -p 80:80, it's accessible to http://localhost/docs/infrastructure/components, which has no trailing slash.

docker run -itd -p 80:80 --env-file env.list --name docs.kubesphere.io kubesphere/docs.kubesphere.io:v2.1

When running with -p 81:80, it's not accessible to http://localhost:81/docs/infrastructure/components. The browser received a redirect to http://localhost/docs/infrastructure/components/, not http://localhost:81/docs/infrastructure/components/.

docker run -itd -p 81:80 --env-file env.list --name docs.kubesphere.io kubesphere/docs.kubesphere.io:v2.1

error

nginx conf is as follows:

daemon              off;
worker_processes    1;
user                root;

events {
  worker_connections 1024;
}

http {
  include       mime.types;
  default_type  application/octet-stream;

  keepalive_timeout  15;
  autoindex          off;
  server_tokens      off;
  port_in_redirect   off;
  sendfile           off;
  tcp_nopush         on;
  tcp_nodelay        on;

  client_max_body_size 64k;
  client_header_buffer_size 16k;
  large_client_header_buffers 4 16k;

  ## Cache open FD
  open_file_cache max=10000 inactive=3600s;
  open_file_cache_valid 7200s;
  open_file_cache_min_uses 2;

  ## Gzipping is an easy way to reduce page weight
  gzip                on;
  gzip_vary           on;
  gzip_proxied        any;
  gzip_types          application/javascript application/x-javascript application/rss+xml text/javascript text/css image/svg+xml;
  gzip_buffers        16 8k;
  gzip_comp_level     6;

  access_log         /dev/stdout;
  error_log          /dev/stderr error;

  server {
    listen 80;
    root /pub;

    index index.html;
    autoindex off;
    charset utf-8;

    error_page 404 /404.html;

    location ~* \.(html)$ {
      add_header Cache-Control "no-store";
      expires    off;
    }
    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|jsx|css|less|swf|eot|ttf|otf|woff|woff2)$ {
      add_header Cache-Control "public";
      expires +1y;
    }

    rewrite ^([^.]*[^/])$ $1/ permanent;

    try_files $uri $uri/ $uri/index.html =404;


  }
}

Without rewrite ^([^.]*[^/])$ $1/ permanent;, the browser still can reveive 301 Moved Permanently (from disk cache) and then appended a trailing slash when using -p 80:80. Changing to -p 81:81 found some pages can receive 81 port in its response header, but some can't.
I'm wondering why the browser can recevie 301 status code?
Is it sent by nginx?
How can I verify it?

0 Answers
Related