为什么nginx将我的请求重定向到www。<upstream> .com?

时间:2017-10-18 01:15:44

标签: redirect nginx

nginx version: nginx/1.10.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-file-aio --with-threads --with-ipv6 --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

下面是常见的配置(nginx.conf,我截断了一些行)。

user  nginx;

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

    charset  utf-8;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 16k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    open_file_cache max=204800 inactive=20s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 30s;

    sendfile on;
    tcp_nopush     on;

    keepalive_timeout 65;
    tcp_nodelay on;

    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    gzip_buffers 16 8k;

    include /etc/nginx/conf.d/*.conf;
    server_tokens off;
}

以下是子会议。

upstream wls_abc_pool {
    ip_hash;
    server wls_abc_1:8006 weight=1 max_fails=3 fail_timeout=10s;
    server wls_abc_2:8006 weight=1 max_fails=3 fail_timeout=10s;
}
server {
    listen       80;
    server_name abc.foo.com;

    location /wls_abc {
        proxy_pass http://wls_abc_pool;
        proxy_set_header X-Real-IP       $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Accept-Encoding "";
    }
}

当我尝试访问http://hostname/wls_abc/http://abc.foo.com/wls_abc/时,浏览器会重新启动我想要的页面。 当我尝试访问http://hostname/wls_abchttp://abc.foo.com/wls_abc时,浏览器会重定向到http://www.wls_abc_pool.com/wls_abc/,这很奇怪。

  • http://abc.foo.com/wls_abc - &gt;奇怪的重定向
  • http://abc.foo.com/wls_abc/ - &gt;正常
  • http://hostname/wls_abc - &gt;奇怪的重定向
  • http://hostname/wls_abc/ - &gt;正常

1 个答案:

答案 0 :(得分:0)

如果您希望它匹配或不匹配斜杠,那么您需要将您的位置规则更改为正则表达式,例如:

位置〜^ / wls_abc /?$ {

简要说明:

- ^ - start of input
- /wls_abc - what to match
- /? - one or zero / characters
- $ - end of input.