Nginx configuratie仅将http重定向到https,仅用于一个域

时间:2017-05-15 20:39:56

标签: nginx nginx-location

在我的Nginx .conf文件的开头,我添加了以下重定向:

server {
    listen 80;
    listen [::]:80;

    server_name *.a-domain.nl;
    return 301 https://$host$request_uri;
}
server {
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    listen 443;
    listen [::]:443 ipv6only=on;

    server_name *.a-domain.nl;

    ssl on;
    ssl_certificate /etc/ssl/b-domain.crt;
    ssl_certificate_key /etc/ssl/b-domain.key;

    location ~* \.(ogg|ogv|svgz|mp4|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|html|txt|htm)$ {
        root /var/www/html/mtcore/web;
        try_files $uri $uri/ $uri.html =404;
    }

    location / {
        proxy_pass http://127.0.0.1;
        proxy_http_version 1.1;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
    }
}
server {
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    listen 80;
    listen [::]:80 ipv6only=on;

    server_name _;

    location ~* \.(ogg|ogv|svgz|mp4|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|html|txt|htm)$ {
        root /var/www/html/mtcore/web;
        try_files $uri $uri/ $uri.html =404;
    }

    location / {
        proxy_pass http://127.0.0.1;
        proxy_http_version 1.1;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
    }
}

只有当我在浏览器中打开在同一服务器上提供的以下域www.b-domain.nl时,Nginx才会将其重定向到https。我希望Nginx只重定向www.a-domain.nl?

1 个答案:

答案 0 :(得分:0)

第一个服务器块是端口80的隐式默认服务器,因此无论服务器名称如何,它都可以处理所有http个请求。第三个服务器块只匹配服务器名称_,这可能是非法的或不太可能。

要使另一台服务器阻止默认设置,请在default_server指令上使用listen选项。

有关详情,请参阅this document