Nginx - 是否可以根据服务器名称重定向到不同的https服务器?

时间:2016-04-22 13:49:47

标签: nginx http-redirect

目前,我正在以这种方式重定向http请求:

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name emberhub.me www.emberhub.me test.emberhub.me;
    return 301 https://$host$request_uri;
}


# HTTPS server static html
#
server {
    listen 443 ssl;
    server_name emberhub.me www.emberhub.me;

    root html;
    index index.html index.htm;

    ssl on;
    ....
    location / {
            try_files $uri $uri/ =404;


# HTTPS server proxy Node.js app
#
server {
    listen 444 ssl;
    server_name test.emberhub.me;

    ssl on;
    ...
    location / {
        proxy_pass http://127.0.0.1:8080;

当用户请求时: http://emberhub.mehttp://www.emberhub.me,他被正确地重定向到https://emberhub.mehttps://www.emberhub.me(默认端口443)

但是当用户请求时: http://test.emberhub.me,他被重定向到端口443并获取静态html页面,而不是node.js应用程序

如果用户使用端口444请求它 http://test.emberhub.me:444,然后他被正确代理到node.js app ...

有没有办法根据server_name过滤请求并使用正确的SSL端口重定向?还是有另一种更好的方法来执行这些重定向?

感谢您的反馈

更新1:

我尝试更改服务器声明,如下所示,但我仍然遇到同样的问题... http://test.emberhub.me,始终重定向到端口443

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name emberhub.me;
    return 301 https://$host$request_uri;
}

server {
    listen 80;
#        listen [::]:80 ipv6only=on;
    server_name www.emberhub.me;
    return 301 https://$host:443$request_uri;
}

server {
    listen 80;
#        listen [::]:80 ipv6only=on;
    server_name test.emberhub.me;
    return 301 https://$host:444$request_uri;
}

更新2

我将默认配置更新为

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name test.emberhub.me;
    return 301 https://$host:444$request_uri;
}

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    server_name  emberhub.me  www.emberhub.me;
    return 301 https://$host$request_uri;
}

使用curl -v一切正常......

 $ curl -v http://emberhub.me
 * Rebuilt URL to: http://emberhub.me/
 ...
 < Connection: keep-alive
 < Location: https://emberhub.me/
 ...
 Connection #0 to host emberhub.me left intact

$ curl -v http://www.emberhub.me
...
< Connection: keep-alive
< Location: https://www.emberhub.me/
Connection #0 to host www.emberhub.me left intact

curl -v http://test.emberhub.me
Rebuilt URL to: http://test.emberhub.me/
...
< Location: https://test.emberhub.me:444/
Connection #0 to host test.emberhub.me left intact

请求浏览器中的http://test.emberhub.me在端口443上显示https://test.emberhub.me ...

1 个答案:

答案 0 :(得分:0)

如果您需要在该服务器上使用不同的行为,请为其单独配置:

server {
  listen 80;
  server_name test.emberhub.me;
  return 301 https://$host:444$request_uri;
}