代理后面的Nginx:避免自动重定向到内部端口

时间:2014-12-09 15:28:45

标签: nginx

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

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。但是,这是不受欢迎的,因为该网站的面向公众的网址为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或其软件包版本。

1 个答案:

答案 0 :(得分:0)

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