nginx位置别名停止重定向

时间:2018-08-11 17:39:50

标签: html redirect nginx

我有以下nginx.conf

worker_processes      1;

events {
  worker_connections  1024;
}

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

  sendfile on;

  keepalive_timeout   65;

  server {
    listen            8080;
    server_name       localhost;
    index             index.html index.htm;

    location /docs {
      alias            /usr/share/nginx/html;

      if ($request_method = 'OPTIONS') {
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
          #
          # Custom headers and headers various browsers *should* be OK with but aren't
          #
          add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          #
          # Tell client that this pre-flight info is valid for 20 days
          #
          add_header 'Access-Control-Max-Age' 1728000;
          add_header 'Content-Type' 'text/plain charset=UTF-8';
          add_header 'Content-Length' 0;
          return 204;
      }
      if ($request_method = 'POST') {
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
          add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      }
      if ($request_method = 'GET') {
          add_header 'Access-Control-Allow-Origin' '*';
          add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
          add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
      }
    }
  }
}

nginx在docker中运行。 Traefik充当代理,并在/docs路径上重定向到nginx容器(到端口8080)。在这里,nginx容器应该只返回内容(静态内容)。

我的问题是nginx总是将我重定向到http://api.example.com:8080/docs/(这是不可访问的,因为我在traefik后面的docker中运行nginx,这就是为什么我需要路径)。我只是尝试从https://api.example.com/docs下的html目录中获取HTML内容。

其他输出:

10.0.5.16 - example [11/Aug/2018:17:30:45 +0000] "GET /docs HTTP/1.1" 301 185 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36"

如何仅在../docs网址下提供内容而没有这种重定向,这是错误的?

1 个答案:

答案 0 :(得分:0)

为避免外部重定向,可以使用从/docs/docs/index.html的内部重写。

例如:

location = /docs {
    rewrite ^ /docs/index.html last;
}
location /docs {
    ...
}