Nginx - 重定向错误?

Nginx - Wrong redirect?

我正在使用 Nginx 作为网络服务器与 LetsEncrypt 一起加密我的连接。 我的主页有多个子域,例如 www.domain.com、mail.domain.com 等。 现在我想将所有“/.well-known”连接重定向到一个特殊的根文件夹。

我认为我的配置是正确的,但结果不是我想要的。

这是我的配置:

default.vhost

#=============================================
#== Server
#==============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 80;

        # Server name
        #server_name _;

        #=============================================
        #== Locations
        #=============================================

        location /.well-known {
                #default_type "text/plain";
                root /var/wwww/LetsEncrypt;
        }

        location / {
                return 301 https://www.example.com$request_uri;
        }
}


server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl;

        # Server name
        #server_name _;

        #=============================================
        #== Locations
        #=============================================

        location / {
                return 301 https://www.example.com$request_uri;
        }
}

www.domain.com

#=============================================
#== HTTP-Server
#=============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl default_server;

        # Server name
        server_name www.example.com;

        # Root folder
        root /var/www/www.example.com/web/;

        # Order of index files
        index index.php index.html index.htm;

        #=============================================
        #== Includes
        #=============================================

        # SSL configuration
        include /etc/nginx/ssl.conf;

        # PHP configuration
        include /etc/nginx/php.conf;

        #=============================================
        #== Locations
        #=============================================

        location /.well-known/acme-challenge {
                #default_type "text/plain";
                root /var/wwww/LetsEncrypt;
        }

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ /\.ht {
                deny all;
        }
}

mail.domain.com

#=============================================
#== HTTP-Server
#=============================================

server {

        #=============================================
        #== General
        #=============================================

        # Port
        listen 443 ssl;

        # Server name
        server_name mail.example.com;

        # Root folder
        root /var/www/mail.example.com/web/;

        # Order of index files
        index index.php index.html index.htm;

        #=============================================
        #== Includes
        #=============================================

        # SSL configuration
        include /etc/nginx/ssl.conf;

        # PHP configuration
        include /etc/nginx/php.conf;

        #=============================================
        #== Locations
        #=============================================

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ /\.ht {
                deny all;
        }
}

所以,发生了什么.. mail.domain.com 和 www.domain.com 效果很好。 问题是 .well-known 规则。 Nginx 忽略它,我总是被重定向,而不是从“/var/www/LetsEncrypt”获取文件。

我认为 default_server 是问题所在,但在将其移至 "default.vhost" 后没有任何效果(总是 404.. 听起来也有点奇怪...)。

希望大家能帮帮我:(

亲切的问候, 安德烈亚斯

首先,您需要使用 nginx 'location begins with' modifier ^~,它将捕获以指定目录开头的所有请求。

location ^~ /.well-known/acme-challenge {

其次你有 /var/wwww/LetsEncrypt (4 w's) 而不是 /var/www/LetsEncrypt.

您的位置块应如下所示:

location ^~ /.well-known/acme-challenge {
    #default_type "text/plain";
    root /var/www/LetsEncrypt;
}

还要注意 alias 和 root 之间的区别,因为 root 会将 uri 附加到路径中,例如:

alias /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt

root /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt/.well-known/acme-challenge

如果您尝试提供特定目录,这可能会使您出错。