首页 > 解决方案 > 带和不带通配符的 nginx 域

问题描述

我有 nginx 配置文件,它必须服务器 example.com 和 www.example.com。

server {
    listen       80;
    server_name  example.com;

    return       301 http://www.example.com$request_uri;
}

server {
    listen 80;
    server_name www.example.com;


    auth_basic "example Login";
    auth_basic_user_file /etc/nginx/.htpasswd;

    root   /projects/www/example;

    index   index.html;

    location ~* \.(html|js|jpg|png|gif|css|perfumes|imgs|map|fonts|otf)$ {
        index  index.do index.html index.htm;
        access_log off;
    }



    location /.protected {
            access_log off;
            auth_basic off;
    }

    location /health {
        access_log off;
        auth_basic off;
    }

    location / {
          try_files $uri $uri/index.html;
    }

    location /hello {
          proxy_pass              http://localhost:8282;
          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;
    }
}

我现在正在尝试保护这个域,因为我们正在准备发布。但是,如果链接中包含“.protected”,则只有一个链接可以让请求在没有密码的情况下通过。

例如,

应该允许 www.example.com/.protected/file1.txt 不输入密码。

它工作正常,但问题是 example.com/.protected/file1.txt(没有“www”)。

如果我只输入域名“example.com”(没有“www”),它会按照配置自动重定向到 www.example.com,但“example.com/.protected/file1.txt”不会重定向到“www” .example.com/.protected/file1.txt'。似乎如果域名有一些路径,域名(没有“www”)不会重定向到“www.example.com”

我得到'卷曲:(6)无法解析主机:'

我的配置文件有什么问题吗?

标签: nginxnginx-config

解决方案


推荐阅读