大网数据广告活动
服务器运维问答

Nginx rewrite on docker machine when host port != container port

· 52CE 我爱测

I'm trying to run multiple docker containers all running nginx listening on port 80, but with different host ports mapping to the containers port 80. For the most part this works, except for when nginx does a redirect due to miss…

问题原文

原帖链接 · Ibasa · CC BY-SA 3.0

I'm trying to run multiple docker containers all running nginx listening on port 80, but with different host ports mapping to the containers port 80.

For the most part this works, except for when nginx does a redirect due to missing a trailing slash.

server {
    listen 80;
    root /var/www;
    index index.html;
    location /docs {}
}

Given the above nginx config and a docker container running it with host port 8080 mapped to container port 80 I can get localhost:8080/docs/ via curl ok:

> GET /docs/ HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.9.5 is not blacklisted
< Server: nginx/1.9.5
< Date: Sat, 28 Nov 2015 17:27:05 GMT
< Content-Type: text/html
< Content-Length: 6431
< Last-Modified: Sat, 28 Nov 2015 17:17:06 GMT
< Connection: keep-alive
< ETag: "5659e192-191f"
< Accept-Ranges: bytes
<
... html page ...

but if I request localhost:8080/docs I get a redirect to localhost/docs/

> GET /docs HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
* Server nginx/1.9.5 is not blacklisted
< Server: nginx/1.9.5
< Date: Sat, 28 Nov 2015 17:29:40 GMT
< Content-Type: text/html
< Content-Length: 184
< Location: http://localhost/docs/
< Connection: keep-alive
<
... html redirect page ...

How can I get nginx to preserve the original port when doing the redirect? I've tried looking at port_in_redirect and server_name_in_redirect but they didn't help.


EDIT

Based on https://forum.nginx.org/read.php?2,261216,261216#msg-261216 this doesn't look possible right now.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

The simplest solution is to remove the index directive and not rely on explicit or implicit $uri/ redirects. For example:

server {
  listen 80;
  root /var/www;
  location /docs {
    try_files $uri $uri/index.html =404;
  }
}

This isn't identical behaviour as it avoids the redirect altogether. If you wanted a trailing slash redirect like the index module gives, then a more complex solution is required. For example:

server {
  listen 80;
  root /var/www;
  location /docs {
    try_files $uri @redirect;
  }
  location @redirect {
    if ($uri ~* ^(.+)/$) { rewrite ^ $uri/index.html last; }
    if (-d $document_root$uri) { return $scheme://$host:8080$uri/; }
    return 404;
  }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-11-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。