【问题标题】:Nginx behind a proxy: avoid auto-redirects to internal port代理后面的 Nginx:避免自动重定向到内部端口
【发布时间】:2014-12-09 15:28:45
【问题描述】:

我有一个设置,其中 haproxy 在 example.com:80 上侦听并将 HTTP 请求代理到在服务器 20080 上侦听的 nginx 实例。

nginx 所做的只是从/usr/share/nginx/html 提供静态文件。

例如,http://example.com/doc/ 映射到 /usr/share/nginx/html/doc/

但是,对http://example.com/doc(不带斜杠)的请求会导致 301 重定向到 http://example.com:20080/doc/

$ curl -i http://example.com/doc
HTTP/1.1 301 Moved Permanently
Server: nginx/1.0.15
Date: Tue, 09 Dec 2014 15:10:44 GMT
Content-Type: text/html
Content-Length: 185
Location: http://example.com:20080/doc/

请注意,nginx 已在 URL 中包含端口 20080。但是,这是不受欢迎的,因为该网站面向公众的 URL 是 http://example.com/,因此重定向应该是 http://example.com/doc/

向 nginx 解释这一点最简单的方法是什么?

/etc/nginx/nginx.conf

user              nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d/default.conf

server {
    listen       20080 default_server;
    include /etc/nginx/default.d/*.conf;

    root /usr/share/nginx/html;
    index index.html;

    location /doc/ {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd/doc;
    }
}

nginx.conf 文件是 CentOS nginx 软件包版本 1.0.15 中的默认文件。这是一个旧版本,但我无法控制 CentOS 或其软件包版本。

【问题讨论】:

  • 你的 HAProxy 配置是什么?
  • 配置你的HAProxy,或者使用port_in_redirect off指令
  • @AlexeyTen:这行得通;请将其发布为答案,我会接受:)

标签: nginx


【解决方案1】:

配置您的 HAProxy,或使用 port_in_redirect off; 指令。

【讨论】:

  • port_in_redirect off 完成了这项工作!
猜你喜欢
  • 1970-01-01
  • 2013-01-16
  • 2014-09-26
  • 2021-04-13
  • 2020-09-27
  • 1970-01-01
  • 2021-03-02
  • 2021-09-01
  • 1970-01-01
相关资源
最近更新 更多