【问题标题】:Nginx Server Block OrderingNginx 服务器块排序
【发布时间】:2016-11-20 01:04:56
【问题描述】:

我在尝试使用 nginx 服务的子域上有多个站点,其中一些子域使用 SSL 服务,而有些则没有。我在让非 SSL 网站正常服务时遇到了一些麻烦。每当我尝试访问它们时,它们都会立即(使用正确的主机)重定向到 SSL/HTTPS 版本。我在下面附上了我的位置块。我已经阅读了关于请求处理的 nginx 块,但不知道如何强制未加密的主机不被转发。 (http://nginx.org/en/docs/http/request_processing.html)

server {
    listen 80;
    server_name dev.example.ca dev.example.server2.example.tl;

    location = /favicon.ico { access_log off; log_not_found off; }
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/example/example/socket.sock;
    }
    location /static {
        autoindex on;
        alias /home/litobro/example/example/static/;
    }
}

server {
    listen 80;
    server_name dutyroster.example.ca;

    location = /favicon.ico { access_log off; log_not_found off; }
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/example2/dutyroster/socket.sock;
    }
    location /static {
        autoindex on;
        alias /home/example/example2/static/;
    }

    location /socket.io {
        proxy_pass http://unix:/home/example/example2/socket.sock;
        proxy_redirect off;
        proxy_buffering off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
}

server {
    listen 80;
    server_name ex3.server2.example.tl example.ca www.example.ca;

    location ~ .well-known/acme-challenge/ {
        root /var/www/letsencrypt;
        default_type text/plain;
    }

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.ca www.example.ca example.server2.example.tl;

    ssl_certificate /etc/letsencrypt/live/example.ca/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.ca/privkey.pem;
    include snippets/ssl-params.conf;

    location = /favicon.ico { access_log off; log_not_found off; }
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/example/example3/socket.sock;
    }
}

我订购了服务器块,以便端口 80 请求有望首先受到打击,但这似乎仍然无法正常工作。在此先感谢您的帮助! (服务器块大部分被剥离了实际域,尽管我认为我在域替换上保持一致)

【问题讨论】:

  • 做到了!我的 ssl-params 中有 includeSubdomains。非常感谢!您能否将其作为答案提交,以便我将其标记为已解决?

标签: ssl nginx


【解决方案1】:

问题的原因是HSTS 标头,它设置在snippets/ssl-params.conf 内部。此标头告诉浏览器该网站将仅通过 HTTPS 连接。以下是如何使用 Nginx 设置此标头的示例:

add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

如果标头值包含includeSubDomains 标志,如上例所示,则 HSTS 策略也将适用于主域的所有子域。这就是您的浏览器尝试通过 HTTPS 将所有请求发送到子域的原因。

请记住,现代浏览器将 HSTS 网站列表存储在一个特殊的缓存中,因此简单地删除或修改 Nginx 中的标头可能不会立即产生任何影响。您需要以特定于您的浏览器的方式手动清除 HSTS 缓存。

还值得一提的是,使用includeSubDomains 标志被认为是good practice,因此保留它并为您的子域颁发证书可能是一个好主意。目前有几个证书颁发机构,例如Let's Encrypt,提供免费且易于安装的证书。

【讨论】:

    【解决方案2】:

    抱歉,我没有太多使用 nginx 的经验,但是您的 conf 文件末尾有两个服务器块。第一次监听 80 端口你有“return 301 https://$host$request_uri;”需要这条线吗?

    尝试评论该行并查看您的非 SSL 服务器名称是否仍重定向到 https。

    【讨论】:

    • 我正在使用它来强制某些主机强制使用 ssl,当我拿出那条线时,这些主机不再被强制,但是其他主机也可以在没有 ssl 的情况下工作。由于某种原因,它优先于其他主机,我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2017-10-16
    • 2019-03-22
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多