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

Nginx http to https redirects to the first server_name

· 52CE 我爱测

I have a these 2 server blocks in my nginx/sites-enabled/application.conf : upstream myapplication { server 127.0.0.1:8081; } server { listen 80; server_name app1.example.net app2.example.net app3.example.net; return 301 https://…

问题原文

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

I have a these 2 server blocks in my nginx/sites-enabled/application.conf:

upstream myapplication {
    server 127.0.0.1:8081;
}

server {
    listen 80;
    server_name app1.example.net
                app2.example.net
                app3.example.net;
    return 301 https://$server_name$request_uri;
}

server {
    listen 80;
    server_name app1.example.net
                app2.example.net
                app3.example.net;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_certificate /etc/ssl/certs/example_net.crt;
    ssl_certificate_key /etc/ssl/certs/example_net.key;
    ssl_verify_client off;
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    proxy_ssl_session_reuse off;


    location /apply {
        rewrite ^/apply(.*) $scheme://$server_name/?$query_string? permanent;
    }

    location / {
        proxy_pass http://myapplication;
    }
}

and this is my nginx/nginx.conf: user www-data; worker_processes auto; pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    disable_symlinks off;
    # server_tokens off;


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

    ##
    # Proxy redirect
    ##

    proxy_redirect              off;
    proxy_set_header            Host            $host;
    proxy_set_header            X-Real-IP       $remote_addr;
    proxy_set_header            X-Forwared-For  $proxy_add_x_forwarded_for;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

The problem that I am having is that if I visit http app2.example.net I am redirected to https app1.example.net when it should redirect me to https app2.example.net, however if I go straight to https app2.example.net then that works fine.

What am I missing out in this config?

已采纳解答

原帖链接 · Matt The Ninja · CC BY-SA 3.0

Using $server_name will always use the first in the block.

You need to use $http_host which will take the requests host.

Alternatively just make 3 blocks for each domain will do the trick.

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

资料来源与适用范围

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

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

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